Download the PHP package adrhumphreys/silverstripe-fixtures without Composer
On this page you can find all versions of the php package adrhumphreys/silverstripe-fixtures. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adrhumphreys/silverstripe-fixtures
More information about adrhumphreys/silverstripe-fixtures
Files in adrhumphreys/silverstripe-fixtures
Package silverstripe-fixtures
Short Description Fixtures for Silverstripe.
License BSD-3-Clause
Informations about the package silverstripe-fixtures
Fixtures for Silverstripe
Fixtures for all mixtures 👋
Requirements
- SilverStripe ^4.0 || ^5.0
- PHP ^7.4 || ^8.0
Dev requirements
phpunit/phpunit
^9.5squizlabs/php_codesniffer
^3.0
Installation
Installing as a dev only module:
When running dev/build
Silverstripe framework will try to load all classes into it's ClassManifest to cache them and allow for functionality such as dependency injection. During that process it will try to load your Fixture class which will then try to load AdrHumphreys\Fixtures\AbstractFixture
which doesn't exist. This will throw and exception and stop the dev/build
process.
You have some options to remedy this, ranked from best to worst:
Option 1: Place your fixtures in the tests
directory for your project:
These are designed to be run on a test/dev environment only and the code is more reference than implementation specific. It therefore makes sense to move these files into this directory. Why? It's explicitly ignored when finding files via ManifestFileFinder
Option 2: Add _manifest_exclude
to the fixture directory:
This will ensure that ManifestFileFinder
will ignore files in the directory. This is option 2 because it makes it easier for code that is test only to end up being relied upon by production code which should never be the case
Option 3: Add implements TestOnly
to all fixtures
If you are installing this as a dev dependency then all your fixtures will need to implement \SilverStripe\Dev\TestOnly
this is specifically excluded from Silverstripes class manifest loader
How to use
The default setup is to run this as a task like so:
You'll need to create your fixtures in the directory specified. Or you can implement your own task, look at the task LoadFixtures
as an example. You can change ->loadFromDirectory
to multiple calls of ->loadFixture($fixtureClassName)
A basic fixture looks like the following:
load
is called when creating the fixture and then getClassesToClear
is called when purging the fixture. You can also implement unload
which is function in which you can choose what to do and getTablesToClear
which is a function similar to getClassesToClear
but just tables.
Load order is first dependencies with no order requirement and no dependencies. Then ordered fixtures followed lastly by fixtures with dependencies.
Dependant fixtures
If a fixture depends on another fixture you can implement DependentFixtureInterface
and the function getDependencies
returns an array of classes that the fixture depends on.
An example from would be:
Ordered fixtures
You can also specify the order fixtures are loaded in by implementing the interface OrderedFixtureInterface
the method getOrder
returns an number which represents the order.
Referencing other fixtures
During the load you can store a reference to a fixture by adding it to the internal reference storage with $this->addReference(string $identifier, object $reference);
. An example is:
You'd then use it by calling $this->getByReference(string $identifier)
. Example:
Only running creation/purging
Run the task without purging:
Purge the data:
Do literally nothing:
Quality of life functionality:
Filtering
You can selectively run fixtures using the filter param:
This will run any fixture matching the filter pattern and any ordered fixtures and these won't be automatically resolved like dependencies.
The filter pattern must be a valid pattern for preg_match including a delimiter. The pattern is matched against the fully qualified class name (eg App\My\Fixture
).
Creating assets
You can create assets really easily like so:
This will create the image, and store it with a reference of my-asset-id
. You can also pass through as a third argument an array of params e.g. ['Title' => 'my asset title']
this will be translated to $image->Title = 'my asset title'
so is case-sensitive. The function will also return the stored image. The fourth argument allows you to specify another Asset type eg SilverStripe\Assets\File.
You can then access the asset through $this->getByReference('my-asset-id')
Creating other DataObject
's can be done like so (these are only written through ->write
):
Maintainers
- Adrian [email protected]
Development and contribution
Smash that pull request button 🥰
All versions of silverstripe-fixtures with dependencies
silverstripe/framework Version ^4.0 || ^5.0
silverstripe/admin Version ^1.0 || ^2.0