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