jaschweder / mock
Mock help classes and functions
This package's canonical repository appears to be gone and the package has been frozen as a result.
v1.7.1
2016-09-27 17:24 UTC
Requires
- php: >=5.5.9
- jaschweder/helpers: ~1.0
Requires (Dev)
- phpunit/phpunit: ~4.8
This package is not auto-updated.
Last update: 2024-01-20 15:45:01 UTC
README
Classes and functions to help you with tests
Install
composer require jaschweder/mock
Getting Start
Factory
// Create your mock container $container = new Jaschweder\Mock\Container\ArrayContainer; // Create a new instance of Jaschweder\Mock\Factory $factory = new Jaschweder\Mock\Factory($container); // or use the global factory() function factory();
Using with internal types:
// Strings $factory->register('key', 'value'); $factory->mock('key'); // 'value' // Numbers $factory->register('key', 123); $factory->mock('key'); // 123 // Booleans $factory->register('key', true); $factory->mock('key'); // true // Arrays $factory->register('key', ["foo" => "bar"]); $factory->mock('key'); // ["foo" => "bar"]
Using with objects:
// Push User instance generator to key 'User' $factory->register('User', function() { $user = new User; $user->setEmail('user@example.com'); $user->setPassword('secret'); return $user; }); // Get new instance of class User $user = $factory->mock('User'); // Get 3 instances of class User $users = $factory->mock('User', 3);
You can make variances of existing mocks:
// Register a new mock who receive has parameter a mock of 'User' instance $factory->register('User@admin', function($user) { $user->isAdmin = true; return $user; }); // Get new instance of class User who is admin $admin = $factory->mock('User@admin');
Using with Faker
// Create a new Faker\Factory instance $faker = Faker\Factory::create(); $factory->register('User', function() { $user = new User; $user->setEmail($faker->freeEmail); $user->setPassword($faker->md5); return $user; }); // Get new instance of class User with random data $user = $factory->mock('User');
Mocking objects and methods
// Create a mock object $mock = new Jaschweder\Mock\Object; $mock->method('foo')->result('bar'); echo $mock->foo(); //'bar'
Author
Jonathan A. Schweder jonathanschweder@gmail.com