-
Notifications
You must be signed in to change notification settings - Fork 642
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11445 from craftcms/feature/active-fixture-keyed-…
…data Make keyed data accessible with `ActiveFixture`
- Loading branch information
Showing
2 changed files
with
13 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,16 +11,18 @@ | |
use yii\base\InvalidArgumentException; | ||
use yii\db\ActiveRecord; | ||
use yii\db\TableSchema; | ||
use yii\test\ActiveFixture as BaseActiveFixture; | ||
use yii\test\ActiveFixture as YiiActiveFixture; | ||
use yii\test\BaseActiveFixture; | ||
|
||
/** | ||
* Class Fixture. | ||
* | ||
* @property ActiveRecord[] $data | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @author Global Network Group | Giel Tettelaar <[email protected]> | ||
* @since 3.6.0 | ||
*/ | ||
class ActiveFixture extends BaseActiveFixture | ||
class ActiveFixture extends YiiActiveFixture | ||
{ | ||
/** | ||
* @var array | ||
|
@@ -34,7 +36,7 @@ public function load(): void | |
{ | ||
$tableSchema = $this->getTableSchema(); | ||
$this->data = []; | ||
foreach ($this->getData() as $row) { | ||
foreach ($this->getData() as $key => $row) { | ||
$modelClass = $this->modelClass; | ||
|
||
// Fixture data may pass in props that are not for the db. We thus run an extra check to ensure | ||
|
@@ -63,7 +65,8 @@ public function load(): void | |
throw new InvalidArgumentException('Unable to save fixture data'); | ||
} | ||
|
||
$this->ids[] = $arInstance->id; | ||
$this->data[$key] = $arInstance; | ||
$this->ids[$key] = $arInstance->id; | ||
} | ||
} | ||
|
||
|
@@ -72,17 +75,12 @@ public function load(): void | |
*/ | ||
public function unload(): void | ||
{ | ||
/** @var ActiveRecord $modelClass */ | ||
$modelClass = $this->modelClass; | ||
foreach ($this->ids as $id) { | ||
$arInstance = $modelClass::find() | ||
->where(['id' => $id]) | ||
->one(); | ||
|
||
if ($arInstance && !$arInstance->delete()) { | ||
throw new InvalidArgumentException('Unable to delete AR instance'); | ||
} | ||
foreach ($this->data as $arInstance) { | ||
$arInstance->delete(); | ||
} | ||
|
||
$this->ids = []; | ||
BaseActiveFixture::unload(); | ||
} | ||
|
||
/** | ||
|