- Fixtures for Lithium
- Installation
$ cd /path/to/app/libraries $ git clone [email protected]:daschl/li3_fixtures.git $ mkdir /path/to/app/tests/fixtures
Now, in your `app/config/bootstrap/libraries.php` add this to the bottom (you may optionally place an environment check around this, to avoid loading the fixtures plugin in a production environment):
Libraries::add('li3_fixtures');
Don't forget to include the Fixtures with `use li3_fixtures\test\Fixture;` at the top of your test files.
- Examples
- Loading Collections
- Examples
// store the first post fixture in the database $post = Posts::create(); $post->save($fixtures->first());
By default, `Fixture::load()` gets array data from the source file and returns a `Collection` object (or a derivate of it), which provides you convenience methods like `first()`, `next()`, `current()`, `prev()`, `last()` and so on. For more information, see the documentation for the `lithium\util\Collection` class.
- Saving Fixtures
// let's assume $request exists and is a Request object // the url is "/posts" $file = 'requests/' . Inflector::slug(trim($request->url, '/')); Fixtures::save($request, $file, array('type' => 'php'));
Using the php source, you can take a real `Request` object, save it to a fixture and then use it later in your tests. This reduces the potential for human error when creating mock classes in tests.
- Loading Custom Objects
// loads app/tests/fixtures/requests/posts.php $request = Fixture::load('requests/posts', array('type' => 'php'));
The save example above writes an exported version of the `Request` object to the fixture file. The load example here loads the `Request` object.
- JSON Fixtures
{ "pearl": { "name": "The Black Pearl", "captain": "Jack Sparrow", "type": "East Indiaman", "appearances": [ "The Course of the Black Pearl", "Dead Man's Chest At World's End" ] } }
- PHP Fixtures
<?php
?>
- Custom Adapters
associative array.
- encode(): converts data into the format of the fixture.
You also need to define a static var `$extension` which contains which file extension to use when finding fixture files.
Custom adapters should be placed in `extensions/adapters/test/fixture` inside your app directory.
namespace app\extensions\adapters\test\fixture; class Yaml { public static $extension = "yml"; public static function parse($file) { return yaml_parse($file); } public static function encode($data) { return yaml_emit($data); } } ?>