Create fixtures for your tests by constructing array:
<?php
require __DIR__.'/vendor/autoload.php';
$fixtures = array(
'tags' => array(
'news' => array(
'id' => 1,
'name' => 'news',
'articles' => array(
'@introducing_phpixtures',
'@phpixtures_v0_2_0',
'@phpixtures_v0_3_0',
),
),
),
'articles' => array(
'introducing_phpixtures' => array(
'id' => 1,
'title' => 'Introducing Phpixture v0.1.0',
'content' => 'Yet another fixture library!',
'tag' => '@news',
),
'phpixtures_v0_2_0' => array(
'id' => 2,
'title' => 'Phpixture v0.2.0',
'content' => 'Added ToOne relationship management',
'tag' => '@news',
),
'phpixtures_v0_3_0' => array(
'id' => 3,
'title' => 'Phpixture v0.3.0',
'content' => 'Added ToMany relationship management',
'tag' => '@news',
),
),
);
$repository = new Gnugat\Phpixture\Repository($fixtures);
$articles = $repository->findAll('articles');
// array(
// array(
// 'id' => 1,
// 'title' => 'Introducing Phpixture v0.1.0',
// 'content' => 'Yet another fixture library!',
// 'tag' => array(
// 'id' => 1,
// 'name' => 'news',
// ),
// ),
// ...
// )
$tags = $repository->findAll('tags');
// array(
// 'news' => array(
// 'id' => 1,
// 'name' => 'news',
// 'articles' => array(
// array(
// 'id' => 1,
// 'title' => 'Introducing Phpixture v0.1.0',
// 'content' => 'Yet another fixture library!',
// ),
// ...
// ),
// ),
// )
Use Composer to install this library in your projects:
composer require gnugat/phpixture:~0.1.0@dev
PHP Fixture.
No reason, Phpixture is more a pet project / proof of concept than a library you'd use in production. But I'd be happy if you find it useful!
Say you store them like this:
<?php
// File: fixtures.php
return $fixtures = array(
'articles' => array(
'introducing_phpixtures' => array(
'id' => 1,
'title' => 'Introducing Phpixture',
'content' => 'Yet another fixture library!',
),
),
);
Then you can get them like this:
<?php
require __DIR__.'/vendor/autoload.php';
$fixtures = include __DIR__.'/fixtures.php';
$repository = new Gnugat\Phpixture\Repository($fixtures);
Say you store them like this:
// File: fixtures.yaml
articles:
introducing_phpixtures:
id: 1
title: 'Introducing Phpixture'
content: 'Yet another fixture library!'
Then you can get them like this:
<?php
require __DIR__.'/vendor/autoload.php';
$fixtures = Symfony\Component\Yaml\Yaml::parse(file_get_contents(__DIR__.'/fixtures.yaml'));
$repository = new Gnugat\Phpixture\Repository($fixtures);
Note: You'd need to install Symfony Yaml Component.
Say you store them like this (file: fixtures.json
):
{
'articles': {
'introducing_phpixtures': {
'id': 1,
'title': 'Introducing Phpixture',
'content': 'Yet another fixture library!'
}
}
}
Then you can get them like this:
<?php
require __DIR__.'/vendor/autoload.php';
$fixtures = json_decode(file_get_contents(__DIR__.'/fixtures.json'), true);
$repository = new Gnugat\Phpixture\Repository($fixtures);
I'm sure you start to get the picture. Find a XML library, parse the file and transform it in an array.
Errr... I'm sure you'll find a way (if you do please tell me :) ).
Loop over each fixture and instanciate your object:
<?php
require __DIR__.'/vendor/autoload.php';
$fixtures = Symfony\Component\Yaml\Yaml::parse(file_get_contents(__DIR__.'/fixtures.yaml'));
$repository = new Gnugat\Phpixture\Repository($fixtures);
$articles = array();
foreach ($repository->findAll('articles') as $articleFixture) {
$article = new Article($articleFixture['title'], $articleFixture['content']);
// Assuming Article#id is private, doesn't have setter and cannot be set from constructor
$reflectedArticle = new ReflectionClass($article);
$id = $reflectedArticle->getProperty('id');
$id->setAccessible(true);
$id->setValue($articleFixture['id']);
}
Well, same as above:
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Gnugat\Phpixture\Repository;
use Symfony\Component\Yaml\Yaml;
class LoadUserData implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$fixtures = Yaml::parse(file_get_contents(__DIR__.'/fixtures.yaml'));
$repository = new Repository($fixtures);
foreach ($repository->findAll('articles') as $articleFixture) {
$article = new Article($articleFixture['title'], $articleFixture['content']);
// Assuming Article#id is private, doesn't have setter and cannot be set from constructor
$reflectedArticle = new ReflectionClass($article);
$id = $reflectedArticle->getProperty('id');
$id->setAccessible(true);
$id->setValue($articleFixture['id']);
$manager->persist($article);
}
$manager->flush();
}
}
You can see the current and past versions using one of the following:
- the
git tag
command - the releases page on Github
- the file listing the changes between versions
And finally some meta documentation: