TableRegressionTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. * Tear down
  33. *
  34. * @return void
  35. */
  36. public function tearDown()
  37. {
  38. parent::tearDown();
  39. $this->getTableLocator()->clear();
  40. }
  41. /**
  42. * Tests that an exception is thrown if the transaction is aborted
  43. * in the afterSave callback
  44. *
  45. * @see https://github.com/cakephp/cakephp/issues/9079
  46. * @return void
  47. */
  48. public function testAfterSaveRollbackTransaction()
  49. {
  50. $this->expectException(\Cake\ORM\Exception\RolledbackTransactionException::class);
  51. $table = $this->getTableLocator()->get('Authors');
  52. $table->getEventManager()->on(
  53. 'Model.afterSave',
  54. function () use ($table) {
  55. $table->getConnection()->rollback();
  56. }
  57. );
  58. $entity = $table->newEntity(['name' => 'Jon']);
  59. $table->save($entity);
  60. }
  61. /**
  62. * Ensure that saving to a table with no primary key fails.
  63. *
  64. * @return void
  65. */
  66. public function testSaveNoPrimaryKeyException()
  67. {
  68. $this->expectException(InvalidArgumentException::class);
  69. $this->expectExceptionMessage('primary key');
  70. $table = $this->getTableLocator()->get('Authors');
  71. $table->getSchema()->dropConstraint('primary');
  72. $entity = $table->find()->first();
  73. $entity->name = 'new name';
  74. $table->save($entity);
  75. }
  76. }