Download the PHP package robuust/craft-fixtures without Composer
On this page you can find all versions of the php package robuust/craft-fixtures. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package craft-fixtures
Fixtures (for Craft 3)
This plugin provides all the necessary means to run Craft Elements as Yii2 fixtures. If you're not familiar with Yii2 fixtures it is advised to read their manual first.
Installation
This tool can be installed using Composer. Run the following command from the root of your project:
This will add robuust/craft-fixtures
as a requirement to your project's composer.json
file and install the source-code into the vendor/robuust/craft-fixtures
directory.
Fixtures is now available as an installable plugin in Craft. You can install it in the cp or use ./craft install/plugin fixtures
Usage
This plugin provides abstract fixtures that you will have to extend and implement.
Creating fixtures
Create a folder called "fixtures" in your project root. In this folder we will put our fixture classes, fixture data and the (optional) fixture migrations.
In the Yii2 docs you can read about fixture classes and fixture data.
This plugin provides a base for fixtures for Craft elements. The main logic is defined in robuust\fixtures\base\ElementFixture
. Our fixture classes can contain the following methods (next to all the features yii\test\ActiveFixture
has to offer:
ElementFixture::isPrimaryKey(string $key): bool
to define which model attributes can be used to identify a unique Element
ElementFixture::getElement(array $data): Element?
to get an element which is identified by the given data. The defined primary keys are used to identify the given data.
All fixtures
ElementFixture will define $this->siteIds
with all site handles as keys.
Asset
Extend robuust\fixtures\base\AssetFixture
to add assets. Its datafile could look like this:
This will upload and link product.jpg as an asset.
AssetFixture will define $this->volumeIds
and $this->folderIds
with their handles as key.
Its primary keys are: volumeId
, folderId
, filename
and title
.
Category
Extend robuust\fixtures\base\CategoryFixture
to add categories. Its datafile could look like this:
CategoryFixture will define $this->groupIds
with all category group handles as key.
Its primary keys are: siteId
, groupId
and title
.
Entry
Extend robuust\fixtures\base\EntryFixture
to add entries. Its datafile could look like this:
EntryFixture will define $this->sectionIds
with all section handles as key. It will also define $this->typeIds
with all section handles as the first key and the entry type handles as the second key.
Its primary keys are: siteId
, sectionId
, typeId
and title
.
GlobalSet
Extend robuust\fixtures\base\GlobalSetFixture
to update (!) globals (they must already exist). Its datafile could look like this:
Its primary keys are: siteId
and handle
, the handle being the one of the (already existing) global set.
Tag
Extend robuust\fixtures\base\TagFixture
to add tags. Its datafile could look like this:
TagFixture will define $this->groupIds
with all tag group handles as key.
Its primary keys are: siteId
, groupId
and title
.
User
Extend robuust\fixtures\base\UserFixture
to add users. Its datafile could look like this:
Its primary keys are: siteId
, username
and email
.
Product
Extend robuust\fixtures\base\Product
to add products. Its datafile could look like this:
ProductFixture will define $this->productTypeIds
with all product type handles as key.
Its primary keys are: siteId
, typeId
and title
.
Running fixtures
Once you install the Plugin in Craft, you can run some command line actions. When you run ./craft
in the command line, you will now see these actions:
The fixtures/fixture
actions are extended from Yii2 and will take the same arguments. Run all your fixtures with ./craft fixtures/fixture/load "*"
Running fixture migrations
Next to the fixtures/fixture
actions in the command line, you will also find:
The fixtures/migrate
actions are extended from Craft and will take the same arguments. Run all your migrations with ./craft fixtures/migrate/up
Using fixtures in tests
To use these fixtures in Codeception tests with Craft 3 you can use the robuust\fixtures\test\Craft
module in stead of using Codeception's Yii2 module.
You can load fixtures in tests the way the Yii2 docs describe it.
Example
This is an example of a fixture for a fictional "Contact Page"
fixtures/ContactPageEntryFixture.php
fixtures/data/contact-page-entry.php
License
This project has been licensed under the MIT License (MIT). Please see License File for more information.
Changelog
CHANGELOG.md