TableRegressionTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\TestSuite\TestCase;
  17. use InvalidArgumentException;
  18. /**
  19. * Contains regression test for the Table class
  20. */
  21. class TableRegressionTest extends TestCase
  22. {
  23. /**
  24. * Fixture to be used
  25. *
  26. * @var array
  27. */
  28. public $fixtures = [
  29. 'core.Authors',
  30. ];
  31. /**
  32. * Tests that an exception is thrown if the transaction is aborted
  33. * in the afterSave callback
  34. *
  35. * @see https://github.com/cakephp/cakephp/issues/9079
  36. * @return void
  37. */
  38. public function testAfterSaveRollbackTransaction()
  39. {
  40. $this->expectException(\Cake\ORM\Exception\RolledbackTransactionException::class);
  41. $table = $this->getTableLocator()->get('Authors');
  42. $table->getEventManager()->on(
  43. 'Model.afterSave',
  44. function () use ($table) {
  45. $table->getConnection()->rollback();
  46. }
  47. );
  48. $entity = $table->newEntity(['name' => 'Jon']);
  49. $table->save($entity);
  50. }
  51. /**
  52. * Ensure that saving to a table with no primary key fails.
  53. *
  54. * @return void
  55. */
  56. public function testSaveNoPrimaryKeyException()
  57. {
  58. $this->expectException(InvalidArgumentException::class);
  59. $this->expectExceptionMessage('primary key');
  60. $table = $this->getTableLocator()->get('Authors');
  61. $table->getSchema()->dropConstraint('primary');
  62. $entity = $table->find()->first();
  63. $entity->name = 'new name';
  64. $table->save($entity);
  65. }
  66. }