PHP code example of gnugat / phpixture

1. Go to this page and download the library: Download gnugat/phpixture library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gnugat / phpixture example snippets




ures = 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!',
//             ),
//             ...
//         ),
//     ),
// )


// File: fixtures.php

return $fixtures = array(
    'articles' => array(
        'introducing_phpixtures' => array(
            'id' => 1,
            'title' => 'Introducing Phpixture',
            'content' => 'Yet another fixture library!',
        ),
    ),
);



ures =  new Gnugat\Phpixture\Repository($fixtures);



ures = Symfony\Component\Yaml\Yaml::parse(file_get_contents(__DIR__.'/fixtures.yaml'));
$repository = new Gnugat\Phpixture\Repository($fixtures);



ures = json_decode(file_get_contents(__DIR__.'/fixtures.json'), true);
$repository = new Gnugat\Phpixture\Repository($fixtures);



ures = 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']);
}

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