Skip to content

Commit 7c2d867

Browse files
author
Adam Benson
committed
Initial commit
0 parents  commit 7c2d867

12 files changed

Lines changed: 2218 additions & 0 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
vendor/

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: php
2+
php:
3+
- 7.0
4+
5+
install:
6+
- composer install
7+
8+
script:
9+
- vendor/bin/phpunit

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# bigcommerce/test-injector
2+
3+
Auto-mocking test Dependency Injector component
4+
5+
Will automatically mock all injected dependencies of an object using the BigCommerce Injector. This allows objects to be
6+
refactored to have dependencies removed or new ones added without needing to rebuild all the mock definitions or modify
7+
many tests, making decoupled DI driven development much easier.
8+
9+
Mocks can be configured with expectations by calling getMock(CLASS_NAME) on the TestInjector within your tests. This
10+
will return an ObjectProphecy object (a mock generated by Prophecy - the mocking library included in PHPUnit).
11+
12+
A single mock instance is created per Fully Qualified Class Name, so if the object you're testing takes two of the same
13+
typed parameter, the same mock will be passed to both (so ensure your expectations are set appropriately).
14+
15+
Construction Usage:
16+
````
17+
// All constructor dependencies resolved through mocking
18+
$testObject = $testInjector->create(My\Test\Object::class);
19+
20+
// Parameter injection - will provide parameters either by type, name or position - otherwise resolve via mocking
21+
$testObject = $testInjector->create(My\Test\Object::class, [
22+
"enabled" => true, // Named
23+
7 => "fish", // Positional
24+
Logger::class => new Logger(), // Type
25+
]);
26+
````
27+
28+
Invocation usage
29+
````
30+
// Call the method 'createUser' on an existing object
31+
$testObject = $testInjector->invoke($instanceOfThing, "createUser");
32+
33+
// Parameter injection - will provide parameters either by type, name or position - otherwise resolve via mocking
34+
$testObject = $testInjector->invoke($instanceOfThing, "createUser", [
35+
"enabled" => true, // Named
36+
7 => "fish", // Positional
37+
Logger::class => new Logger(), // Type
38+
]);
39+
````
40+
41+
42+
For integration with your PHPUnit tests, see: [AutoMockingTest.php](src/AutoMockingTest.php)

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "bigcommerce/test-injector",
3+
"type": "library",
4+
"description": "Auto-mocking injector for testing DI components",
5+
"keywords": [
6+
"di",
7+
"pimple",
8+
"injector",
9+
"test"
10+
],
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Adam Benson",
15+
"email": "adam.benson@bigcommerce.com"
16+
}
17+
],
18+
"minimum-stability": "dev",
19+
"repositories": [
20+
{
21+
"type": "vcs",
22+
"url": "ssh://git@github.com/bigcommerce-labs/pimple-injector.git"
23+
}
24+
],
25+
"require": {
26+
"php": "^7.0",
27+
"bigcommerce/injector": "@stable",
28+
"phpunit/phpunit": "^5.6"
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"Bigcommerce\\TestInjector\\": "src/"
33+
}
34+
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"Tests\\": "tests/"
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)