Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make keyed data accessible with ActiveFixture #11445

Merged
merged 8 commits into from
Jun 20, 2022
Merged
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
- Assets fields now reject uploaded files which don’t pass their “Selectable Assets Condition” setting. ([#11433](https://github.com/craftcms/cms/issues/11433))
- It’s now possible to save new assets without setting their `filename` or `kind` attributes, as long as `newLocation` or `newFilename` is set. ([#11439](https://github.com/craftcms/cms/issues/11439))
- The `searchindex` table is now uses the InnoDB storage engine by default for MySQL installs. ([#11374](https://github.com/craftcms/cms/discussions/11374))
- `craft\test\ActiveFixture::$data` is now populated with the active record instances, making them accessible to tests via `$this->tester->grabFixture('my-fixture', 'data-key')`. ([#11445](https://github.com/craftcms/cms/pull/11445))
- `Garnish.DELETE_KEY` now refers to the actual <kbd>Delete</kbd> key code, and the <kbd>Backspace</kbd> key code is now referenced by `Garnish.BACKSPACE_KEY`.

### Deprecated
Expand Down
26 changes: 12 additions & 14 deletions src/test/ActiveFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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();
}

/**
Expand Down