2017 © Pedro Peláez
 

lithium-library li3_fixtures

The fixtures library for the li3 PHP framework

image

unionofrad/li3_fixtures

The fixtures library for the li3 PHP framework

  • Sunday, April 29, 2018
  • by davidpersson
  • Repository
  • 7 Watchers
  • 6 Stars
  • 11,092 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 12 Versions
  • 13 % Grown

The README.md

Fixtures Management for the li₃ framework.

This plugin provide fixtures managment. Should work with any kind of Source adapters. The fixture class support the following datasource's hints:, (*1)

  • If Source::enabled('schema') returns true, the Fixture manage schema (i.e create/drop) via Source::createSchema() & Source::dropSchema()., (*2)

  • If Source::enabled('sources') returns true, the Fixture allow soft drop (i.e safe options)., (*3)

Installation

The preferred installation method is via composer. You can add the library as a dependency via:, (*4)

composer require unionofrad/li3_fixtures

li₃ plugins must be registered within your application bootstrap phase as they use a different (faster) autoloader., (*5)

Libraries::add('li3_fixtures')

The official manual has more information on how to register the plugin with the app or use alternative installation methods (i.e. via GIT)., (*6)

API

The Fixture class

Methods:

  • Fixture::create($safe); //Create the source only
  • Fixture::save($safe); //Create the source + save the fixture's records in.
  • Fixture::drop($safe); //Drop the source.
  • Fixture::populate($records); //Insert a record in the database
  • Fixture::alter($mode, $fieldname, $value); //Altering the schema before ::create()/::save().

Simple example of unit test:, (*7)

 'lithium_mysql_test',
            'source' => 'contacts',
            'fields' => array(
                'id' => array('type' => 'id'),
                'name' => array('type' => 'string')
            ),
            'records' => array(
                array('id' => 1, 'name' => 'Nate'),
                array('id' => 2, 'name' => 'Gwoo')
            )
        ]);

        $fixture->save();

        $fixture->populate(['id' => 3, 'name' => 'Mehlah']);

        $fixture->drop();
    }
}
?>

The Fixtures class

Fixture is a kind of Schema which contain records and a source name or a reference to a model. So let save the above fixture in a class., (*8)

 ['type' => 'id'],
        'name' => ['type' => 'string']
    );

    protected $_records = [
        array['id' => 1, 'name' => 'Nate'],
        array['id' => 2, 'name' => 'Gwoo']
    ];
}
?>

If you have numbers of fixtures, it will be interesting to use the Fixtures class., (*9)

Example of use case:, (*10)

 [
                'adapter' => 'Connection',
                'connection' => 'lithium_mysql_test',
                'fixtures' => array(
                    'contacts' => 'app\tests\fixture\ContactsFixture',
                    'images' => 'app\tests\fixture\ImagesFixture'
                    // and so on...
                )
            ]
        ]);
        Fixtures::save('db');
    }

    public function tearDown() {
        Fixtures::clear('db');
    }

    public function testFixture() {
        var_export(Contacts::find('all')->data());
        var_export(Images::find('all')->data());
    }
}
?>

Ok so why it's better to set the Fixture::_model instead of Fixture::_source ? Long story short, models had their own meta 'connection' value. If a fixture is "linked" with a model, it will automagically configure its meta 'connection' to the fixture's connection when is created or saved., (*11)

Example:, (*12)

<?php
Fixtures::save('db', array('contacts'));
//The line bellow is not needed since Contacts have been configured by ContactsFixture.
Contacts::config(['meta' => ['connection' => 'lithium_mysql_test']]);
var_export(Contacts::find('all')->data());
?>

Advanced use case

For interoperability, sometimes it's usefull to adjust fixtures according a datasources., (*13)

You can alter Fixture's instance before creating it like the following use case:, (*14)

alter('add', [
    'name' => 'enabled',
    'type' => 'boolean'
]); //Add a field

$fixture->alter('change', [
    'name' => 'published',
    'value' => function ($val) {
        return new MongoDate(strtotime($val));
    }
]); //Simple cast for fixture's values according the closure

$fixture->alter('change', [
    'name' => 'id',
    'to' => '_id',
    'value' => function ($val) {
        return new MongoId('4c3628558ead0e594' . (string) ($val + 1000000));
    }
]); //Renaming the field 'id' to '_id' + cast fixture's values according the closure

$fixture->alter('change', [
    'name' => 'bigintger',
    'type' => 'integer',
    'use' => 'bigint' //use db specific type
]); //Modifing a field type

$fixture->alter('drop', 'bigintger'); //Simply dropping a field
?>

Note :, (*15)

You can recover a specific fixture's instance from Fixtures using:, (*16)

<?php
$fixture = Fixtures::get('db', 'contacts');
?>

The Versions

29/04 2018

1.2.x-dev

1.2.9999999.9999999-dev http://li3.me

The fixtures library for the li3 PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

testing fixtures lithium li3

08/04 2017

1.1.x-dev

1.1.9999999.9999999-dev http://li3.me

The fixtures library for the li3 PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

testing fixtures lithium li3

08/04 2017

dev-master

9999999-dev http://li3.me

The fixtures library for the li3 PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

testing fixtures lithium li3

08/04 2017

v1.1.1

1.1.1.0 http://li3.me

The fixtures library for the li3 PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

testing fixtures lithium li3

26/03 2017

v1.1.0

1.1.0.0 http://li3.me

The fixtures library for the li3 PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

testing fixtures lithium li3

31/10 2016

v1.1.0-beta

1.1.0.0-beta http://li3.me

The fixtures library for the li3 PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

testing fixtures lithium li3

01/06 2016

1.0.x-dev

1.0.9999999.9999999-dev http://li3.me

The fixtures library for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

testing fixtures lithium li3

01/06 2016

v1.0.0

1.0.0.0 http://li3.me

The fixtures library for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

testing fixtures lithium li3

05/09 2015

v1.0.0-rc3

1.0.0.0-RC3 http://li3.me

The fixtures library for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

testing fixtures lithium li3

13/08 2015

v1.0.0-rc2

1.0.0.0-RC2 http://li3.me

The fixtures library for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

testing fixtures lithium li3

13/08 2015

v1.0.0-rc1

1.0.0.0-RC1 http://li3.me

The fixtures library for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

testing fixtures lithium li3

08/04 2015

v1.0.0-beta

1.0.0.0-beta http://li3.me

The fixtures library for the Lithium PHP framework

  Sources   Download

BSD-3-Clause

The Requires

 

testing fixtures lithium li3