Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SchemaFixture.php
Go to the documentation of this file.
1 <?php
11 
18 {
24  protected $fixtureBaseDir;
25 
31  private $appliedFixtures = [];
32 
39  public function __construct($fixtureBaseDir)
40  {
41  if (!is_dir($fixtureBaseDir)) {
42  throw new \Magento\Framework\Exception\LocalizedException(
43  new \Magento\Framework\Phrase("Fixture base directory '%1' does not exist.", [$fixtureBaseDir])
44  );
45  }
46  $this->fixtureBaseDir = realpath($fixtureBaseDir);
47  }
48 
55  public function startTest(\PHPUnit\Framework\TestCase $test)
56  {
57  if ($this->_getFixtures($test)) {
58  $this->_applyFixtures($this->_getFixtures($test));
59  }
60  }
61 
67  public function endTest(\PHPUnit\Framework\TestCase $test)
68  {
69  if ($this->_getFixtures($test)) {
70  $this->_revertFixtures();
71  }
72  }
73 
82  protected function _getFixtures(\PHPUnit\Framework\TestCase $test, $scope = null)
83  {
84  if ($scope === null) {
85  $annotations = $this->getAnnotations($test);
86  } else {
87  $annotations = $test->getAnnotations()[$scope];
88  }
89  $result = [];
90  if (!empty($annotations['magentoSchemaFixture'])) {
91  foreach ($annotations['magentoSchemaFixture'] as $fixture) {
92  if (strpos($fixture, '\\') !== false) {
93  // usage of a single directory separator symbol streamlines search across the source code
94  throw new \Magento\Framework\Exception\LocalizedException(
95  new \Magento\Framework\Phrase('Directory separator "\\" is prohibited in fixture declaration.')
96  );
97  }
98  $fixtureMethod = [get_class($test), $fixture];
99  if (is_callable($fixtureMethod)) {
100  $result[] = $fixtureMethod;
101  } else {
102  $result[] = $this->fixtureBaseDir . '/' . $fixture;
103  }
104  }
105  }
106  return $result;
107  }
108 
115  private function getAnnotations(\PHPUnit\Framework\TestCase $test)
116  {
117  $annotations = $test->getAnnotations();
118  return array_replace($annotations['class'], $annotations['method']);
119  }
120 
127  protected function _applyOneFixture($fixture)
128  {
129  try {
130  if (is_callable($fixture)) {
131  call_user_func($fixture);
132  } else {
133  include $fixture;
134  }
135  } catch (\Exception $e) {
136  throw new \Exception(
137  sprintf("Error in fixture: %s.\n %s", json_encode($fixture), $e->getMessage()),
138  500,
139  $e
140  );
141  }
142  }
143 
150  protected function _applyFixtures(array $fixtures)
151  {
152  /* Execute fixture scripts */
153  foreach ($fixtures as $oneFixture) {
154  /* Skip already applied fixtures */
155  if (in_array($oneFixture, $this->appliedFixtures, true)) {
156  continue;
157  }
158  $this->_applyOneFixture($oneFixture);
159  $this->appliedFixtures[] = $oneFixture;
160  }
161  }
162 
166  protected function _revertFixtures()
167  {
168  foreach ($this->appliedFixtures as $fixture) {
169  if (is_callable($fixture)) {
170  $fixture[1] .= 'Rollback';
171  if (is_callable($fixture)) {
172  $this->_applyOneFixture($fixture);
173  }
174  } else {
175  $fileInfo = pathinfo($fixture);
176  $extension = '';
177  if (isset($fileInfo['extension'])) {
178  $extension = '.' . $fileInfo['extension'];
179  }
180  $rollbackScript = $fileInfo['dirname'] . '/' . $fileInfo['filename'] . '_rollback' . $extension;
181  if (file_exists($rollbackScript)) {
182  $this->_applyOneFixture($rollbackScript);
183  }
184  }
185  }
186  $this->appliedFixtures = [];
187  }
188 }
_getFixtures(\PHPUnit\Framework\TestCase $test, $scope=null)
startTest(\PHPUnit\Framework\TestCase $test)
endTest(\PHPUnit\Framework\TestCase $test)