danharper / stem
A simple fixtures library for PHP
Requires
- php: >=5.3.0
Requires (Dev)
- mockery/mockery: dev-master
This package is auto-updated.
Last update: 2020-01-10 14:49:05 UTC
README
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)
Example
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');
Installation
Laravel
- 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');
Native
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');
Provided Handlers
: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
Registering your own Handlers
With a Class
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
With a Closure
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