TableRegressionTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.2.13
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\ORM\TableRegistry;
  17. use Cake\TestSuite\TestCase;
  18. use InvalidArgumentException;
  19. /**
  20. * Contains regression test for the Table class
  21. */
  22. class TableRegressionTest extends TestCase
  23. {
  24. /**
  25. * Fixture to be used
  26. *
  27. * @var array
  28. */
  29. public $fixtures = [
  30. 'core.authors',
  31. ];
  32. /**
  33. * Tear down
  34. *
  35. * @return void
  36. */
  37. public function tearDown()
  38. {
  39. parent::tearDown();
  40. $this->getTableLocator()->clear();
  41. }
  42. /**
  43. * Tests that an exception is thrown if the transaction is aborted
  44. * in the afterSave callback
  45. *
  46. * @see https://github.com/cakephp/cakephp/issues/9079
  47. * @return void
  48. */
  49. public function testAfterSaveRollbackTransaction()
  50. {
  51. $this->expectException(\Cake\ORM\Exception\RolledbackTransactionException::class);
  52. $table = $this->getTableLocator()->get('Authors');
  53. $table->getEventManager()->on(
  54. 'Model.afterSave',
  55. function () use ($table) {
  56. $table->getConnection()->rollback();
  57. }
  58. );
  59. $entity = $table->newEntity(['name' => 'Jon']);
  60. $table->save($entity);
  61. }
  62. /**
  63. * Ensure that saving to a table with no primary key fails.
  64. *
  65. * @return void
  66. */
  67. public function testSaveNoPrimaryKeyException()
  68. {
  69. $this->expectException(InvalidArgumentException::class);
  70. $this->expectExceptionMessage('primary key');
  71. $table = $this->getTableLocator()->get('Authors');
  72. $table->getSchema()->dropConstraint('primary');
  73. $entity = $table->find()->first();
  74. $entity->name = 'new name';
  75. $table->save($entity);
  76. }
  77. }