dev-master
9999999-dev https://github.com/danharper/StemA simple fixtures library for PHP
MIT
The Requires
- php >=5.3.0
The Development Requires
by Dan Harper
Wallogit.com
2017 © Pedro Peláez
A simple fixtures library for PHP
A simple fixtures library for PHP. (This is mostly a practice for me to learn writing TDD with, but I think this would be useful), (*1)
Declare what a fixture should look like:, (*3)
Stem::fixture('User', array(
'id' => ':int'
'name' => '2:words',
'email' => ':email',
'bio' => ':string',
));
Then use it:, (*4)
$fixture = Stem::attributes('User');
// array(
// 'id' => 29,
// 'name' => 'foo banana',
// 'email' => 'mascot28384@bread.example.com',
// 'bio' => 'dawn chat grandpa ballplayer cell Jill wing brainstorm chill Jills hunk ache'
// )
Or even create a real object directly from it:, (*5)
$obj = Stem::make('User');
// this calls:
// new User(array( ... ))
In simpler cases you may just need a couple of random words:, (*6)
Stem::run('3:words');
"danharper/stem": "dev-master" to your composer.json and update/install'danharper\Stem\Facades\Laravel\StemServiceProvider' to the providers array in app/config/app.php
'Stem' => 'danharper\Stem\Facades\Laravel\Stem' to the aliases array in app/config/app.php
Use it:, (*7)
Stem::run('3:words');
Get it from Composer with "danharper\stem": "dev-master"., (*8)
Then you have two ways you can use it:, (*9)
use danharper\Stem\Facades\Native\Stem as Stem;
Stem::run('3:words');
use danharper\Stem\Stem as Stem;
$stem = new Stem;
$stem->run('3:words');
:string and :words -- prefix with a number for that many words, eg. 3:words
:word -- for a single word, for clarity in your code you could even write 1:word (1:words would also work):int and :number -- prefix with a number for a number from 0 up to the given number:emailProvide Stem::register() with an object which responds to register with what it wishes to be known as, and when told run (with an optional modifier) returns something to display. Implent danharper\Stem\Handlers\HandlerInterface for clarity., (*10)
class CustomHandler {
public function register() {
return 'custom';
}
public function run($modifier) {
if ($modifier)
return "something $modifier";
else
return "something else";
}
}
Stem::register(new CustomHandler);
Stem::run('lorem:custom'); // something lorem
Stem::run(':custom'); // something else
Provide Stem::register() with a Closure behaving as the run method in the class above, and with the second argument what it wishes to be known as., (*11)
Stem::register(function($modifier) {
if ($modifier)
return "something $modifier";
else
return "something else";
}, 'foobar');
Stem::run('baz:foobar'); // something baz
Stem::run(':foobar'); // something else
A simple fixtures library for PHP
MIT