Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ApiDataFixture.php
Go to the documentation of this file.
1 <?php
14 
16 {
20  protected $_fixtureBaseDir;
21 
27  private $_appliedFixtures = [];
28 
35  public function __construct($fixtureBaseDir)
36  {
37  if (!is_dir($fixtureBaseDir)) {
38  throw new \Magento\Framework\Exception\LocalizedException(
39  __("Fixture base directory '%1' does not exist.", $fixtureBaseDir)
40  );
41  }
42  $this->_fixtureBaseDir = realpath($fixtureBaseDir);
43  }
44 
50  public function startTest(\PHPUnit\Framework\TestCase $test)
51  {
54  $this->_applyFixtures($this->_getFixtures('method', $test) ?: $this->_getFixtures('class', $test));
55  }
56 
60  public function endTest()
61  {
62  $this->_revertFixtures();
65  $objectManager->get(\Magento\Customer\Model\Metadata\AttributeMetadataCache::class)->clean();
66  }
67 
76  protected function _getFixtures($scope, \PHPUnit\Framework\TestCase $test)
77  {
78  $annotations = $test->getAnnotations();
79  $result = [];
80  if (!empty($annotations[$scope]['magentoApiDataFixture'])) {
81  foreach ($annotations[$scope]['magentoApiDataFixture'] as $fixture) {
82  if (strpos($fixture, '\\') !== false) {
83  // usage of a single directory separator symbol streamlines search across the source code
84  throw new \Magento\Framework\Exception\LocalizedException(
85  __('Directory separator "\\" is prohibited in fixture declaration.')
86  );
87  }
88  $fixtureMethod = [get_class($test), $fixture];
89  if (is_callable($fixtureMethod)) {
90  $result[] = $fixtureMethod;
91  } else {
92  $result[] = $this->_fixtureBaseDir . '/' . $fixture;
93  }
94  }
95  }
96  return $result;
97  }
98 
105  protected function _applyOneFixture($fixture)
106  {
107  try {
108  if (is_callable($fixture)) {
109  call_user_func($fixture);
110  } else {
111  require $fixture;
112  }
113  } catch (\Exception $e) {
114  throw new \Exception(
115  sprintf(
116  "Exception occurred when running the %s fixture: \n%s",
117  (\is_array($fixture) || is_scalar($fixture) ? json_encode($fixture) : 'callback'),
118  $e->getMessage()
119  )
120  );
121  }
122  $this->_appliedFixtures[] = $fixture;
123  }
124 
131  protected function _applyFixtures(array $fixtures)
132  {
133  /* Execute fixture scripts */
134  foreach ($fixtures as $oneFixture) {
135  /* Skip already applied fixtures */
136  if (!in_array($oneFixture, $this->_appliedFixtures, true)) {
137  $this->_applyOneFixture($oneFixture);
138  }
139  }
140  }
141 
145  protected function _revertFixtures()
146  {
147  $appliedFixtures = array_reverse($this->_appliedFixtures);
148  foreach ($appliedFixtures as $fixture) {
149  if (is_callable($fixture)) {
150  $fixture[1] .= 'Rollback';
151  if (is_callable($fixture)) {
152  $this->_applyOneFixture($fixture);
153  }
154  } else {
155  $fileInfo = pathinfo($fixture);
156  $extension = '';
157  if (isset($fileInfo['extension'])) {
158  $extension = '.' . $fileInfo['extension'];
159  }
160  $rollbackScript = $fileInfo['dirname'] . '/' . $fileInfo['filename'] . '_rollback' . $extension;
161  if (file_exists($rollbackScript)) {
162  $this->_applyOneFixture($rollbackScript);
163  }
164  }
165  }
166  $this->_appliedFixtures = [];
167  }
168 }
$fixtureBaseDir
Definition: bootstrap.php:15
$objectManager
Definition: bootstrap.php:17
_getFixtures($scope, \PHPUnit\Framework\TestCase $test)
__()
Definition: __.php:13
startTest(\PHPUnit\Framework\TestCase $test)