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)
Declare what a fixture should look like:
Stem::fixture('User', array(
'id' => ':int'
'name' => '2:words',
'email' => ':email',
'bio' => ':string',
));
Then use it:
$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:
$obj = Stem::make('User');
// this calls:
// new User(array( ... ))
In simpler cases you may just need a couple of random words:
Stem::run('3:words');
- Add
"danharper/stem": "dev-master"
to yourcomposer.json
and update/install - Add
'danharper\Stem\Facades\Laravel\StemServiceProvider'
to the providers array inapp/config/app.php
- Add
'Stem' => 'danharper\Stem\Facades\Laravel\Stem'
to the aliases array inapp/config/app.php
Use it:
Stem::run('3:words');
Get it from Composer with "danharper\stem": "dev-master"
.
Then you have two ways you can use it:
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 write1:word
(1:words
would also work):int
and:number
-- prefix with a number for a number from 0 up to the given number:email
Provide 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.
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.
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