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.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\TestSuite\Fixture;
16:
17: loadPHPUnitAliases();
18:
19: use Cake\TestSuite\TestCase;
20: use PHPUnit\Framework\BaseTestListener;
21: use PHPUnit\Framework\Test;
22: use PHPUnit\Framework\TestSuite;
23:
24: /**
25: * Test listener used to inject a fixture manager in all tests that
26: * are composed inside a Test Suite
27: */
28: class FixtureInjector extends BaseTestListener
29: {
30: /**
31: * The instance of the fixture manager to use
32: *
33: * @var \Cake\TestSuite\Fixture\FixtureManager
34: */
35: protected $_fixtureManager;
36:
37: /**
38: * Holds a reference to the container test suite
39: *
40: * @var \PHPUnit\Framework\TestSuite
41: */
42: protected $_first;
43:
44: /**
45: * Constructor. Save internally the reference to the passed fixture manager
46: *
47: * @param \Cake\TestSuite\Fixture\FixtureManager $manager The fixture manager
48: */
49: public function __construct(FixtureManager $manager)
50: {
51: if (isset($_SERVER['argv'])) {
52: $manager->setDebug(in_array('--debug', $_SERVER['argv'], true));
53: }
54: $this->_fixtureManager = $manager;
55: $this->_fixtureManager->shutDown();
56: }
57:
58: /**
59: * Iterates the tests inside a test suite and creates the required fixtures as
60: * they were expressed inside each test case.
61: *
62: * @param \PHPUnit\Framework\TestSuite $suite The test suite
63: * @return void
64: */
65: public function startTestSuite(TestSuite $suite)
66: {
67: if (empty($this->_first)) {
68: $this->_first = $suite;
69: }
70: }
71:
72: /**
73: * Destroys the fixtures created by the fixture manager at the end of the test
74: * suite run
75: *
76: * @param \PHPUnit\Framework\TestSuite $suite The test suite
77: * @return void
78: */
79: public function endTestSuite(TestSuite $suite)
80: {
81: if ($this->_first === $suite) {
82: $this->_fixtureManager->shutDown();
83: }
84: }
85:
86: /**
87: * Adds fixtures to a test case when it starts.
88: *
89: * @param \PHPUnit\Framework\Test $test The test case
90: * @return void
91: */
92: public function startTest(Test $test)
93: {
94: $test->fixtureManager = $this->_fixtureManager;
95: if ($test instanceof TestCase) {
96: $this->_fixtureManager->fixturize($test);
97: $this->_fixtureManager->load($test);
98: }
99: }
100:
101: /**
102: * Unloads fixtures from the test case.
103: *
104: * @param \PHPUnit\Framework\Test $test The test case
105: * @param float $time current time
106: * @return void
107: */
108: public function endTest(Test $test, $time)
109: {
110: if ($test instanceof TestCase) {
111: $this->_fixtureManager->unload($test);
112: }
113: }
114: }
115: