TransactionStrategyTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.3.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\TestSuite;
  17. use Cake\ORM\TableRegistry;
  18. use Cake\TestSuite\Fixture\TransactionStrategy;
  19. use Cake\TestSuite\TestCase;
  20. class TransactionStrategyTest extends TestCase
  21. {
  22. public $fixtures = ['core.Users'];
  23. /**
  24. * Test that beforeTest starts a transaction that afterTest closes.
  25. *
  26. * @return void
  27. */
  28. public function testTransactionWrapping()
  29. {
  30. $users = TableRegistry::get('Users');
  31. $strategy = new TransactionStrategy();
  32. $strategy->beforeTest();
  33. $user = $users->newEntity(['username' => 'testing', 'password' => 'secrets']);
  34. $users->save($user, ['atomic' => true]);
  35. $this->assertNotEmpty($users->get($user->id), 'User should exist.');
  36. // Rollback and the user should be gone.
  37. $strategy->afterTest();
  38. $this->assertNull($users->findById($user->id)->first(), 'No user expected.');
  39. }
  40. }