Commit d9cd1a85 by Mark Committed by Qiang Xue

added append option to fixture controller

parent 85466f17
...@@ -49,6 +49,11 @@ class FixtureController extends Controller ...@@ -49,6 +49,11 @@ class FixtureController extends Controller
*/ */
public $namespace = 'tests\unit\fixtures'; public $namespace = 'tests\unit\fixtures';
/** /**
* @var bool whether to clean fixtures storage before apply or load them
* in the end of already existed fixtures
*/
public $append = false;
/**
* @var array global fixtures that should be applied when loading and unloading. By default it is set to `InitDbFixture` * @var array global fixtures that should be applied when loading and unloading. By default it is set to `InitDbFixture`
* that disables and enables integrity check, so your data can be safely loaded. * that disables and enables integrity check, so your data can be safely loaded.
*/ */
...@@ -63,7 +68,7 @@ class FixtureController extends Controller ...@@ -63,7 +68,7 @@ class FixtureController extends Controller
public function options($actionId) public function options($actionId)
{ {
return array_merge(parent::options($actionId), [ return array_merge(parent::options($actionId), [
'namespace', 'globalFixtures' 'namespace', 'globalFixtures', 'append'
]); ]);
} }
...@@ -78,14 +83,17 @@ class FixtureController extends Controller ...@@ -78,14 +83,17 @@ class FixtureController extends Controller
*/ */
public function actionLoad(array $fixtures, array $except = []) public function actionLoad(array $fixtures, array $except = [])
{ {
$foundFixtures = $this->findFixtures($fixtures);
if (!$this->needToApplyAll($fixtures[0])) { if (!$this->needToApplyAll($fixtures[0])) {
$foundFixtures = $this->findFixtures($fixtures);
$notFoundFixtures = array_diff($fixtures, $foundFixtures); $notFoundFixtures = array_diff($fixtures, $foundFixtures);
if ($notFoundFixtures) { if ($notFoundFixtures) {
$this->notifyNotFound($notFoundFixtures); $this->notifyNotFound($notFoundFixtures);
} }
} else {
$foundFixtures = $this->findFixtures();
} }
if (!$foundFixtures) { if (!$foundFixtures) {
...@@ -107,7 +115,11 @@ class FixtureController extends Controller ...@@ -107,7 +115,11 @@ class FixtureController extends Controller
} }
$fixturesObjects = $this->createFixtures($fixtures); $fixturesObjects = $this->createFixtures($fixtures);
$this->unloadFixtures($fixturesObjects);
if (!$this->append) {
$this->unloadFixtures($fixturesObjects);
}
$this->loadFixtures($fixturesObjects); $this->loadFixtures($fixturesObjects);
$this->notifyLoaded($fixtures); $this->notifyLoaded($fixtures);
} }
...@@ -122,14 +134,15 @@ class FixtureController extends Controller ...@@ -122,14 +134,15 @@ class FixtureController extends Controller
*/ */
public function actionUnload(array $fixtures, array $except = []) public function actionUnload(array $fixtures, array $except = [])
{ {
$foundFixtures = $this->findFixtures($fixtures);
if (!$this->needToApplyAll($fixtures[0])) { if (!$this->needToApplyAll($fixtures[0])) {
$foundFixtures = $this->findFixtures($fixtures);
$notFoundFixtures = array_diff($fixtures, $foundFixtures); $notFoundFixtures = array_diff($fixtures, $foundFixtures);
if ($notFoundFixtures) { if ($notFoundFixtures) {
$this->notifyNotFound($notFoundFixtures); $this->notifyNotFound($notFoundFixtures);
} }
} else {
$foundFixtures = $this->findFixtures();
} }
if (!$foundFixtures) { if (!$foundFixtures) {
...@@ -265,16 +278,22 @@ class FixtureController extends Controller ...@@ -265,16 +278,22 @@ class FixtureController extends Controller
} }
/** /**
* @param array $fixtures * Finds fixtures to be loaded, for example "User", if no fixtures were specified then all of them
* will be searching by suffix "Fixture.php".
* @param array $fixtures fixtures to be loaded
* @return array Array of found fixtures. These may differ from input parameter as not all fixtures may exists. * @return array Array of found fixtures. These may differ from input parameter as not all fixtures may exists.
*/ */
private function findFixtures(array $fixtures) private function findFixtures(array $fixtures = [])
{ {
$fixturesPath = $this->getFixturePath(); $fixturesPath = $this->getFixturePath();
$filesToSearch = ['*Fixture.php']; $filesToSearch = ['*Fixture.php'];
if (!$this->needToApplyAll($fixtures[0])) { $findAll = ($fixtures == []);
if (!$findAll) {
$filesToSearch = []; $filesToSearch = [];
foreach ($fixtures as $fileName) { foreach ($fixtures as $fileName) {
$filesToSearch[] = $fileName . 'Fixture.php'; $filesToSearch[] = $fileName . 'Fixture.php';
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment