ondrejmirtes / mocktainer
Create objects of classes with mocked dependencies more easily!
0.8
2019-02-03 21:55 UTC
Requires
- php: ^7.2
- phpunit/phpunit: ^8.0
Requires (Dev)
- consistence/coding-standard: ~0.13.0
- jakub-onderka/php-parallel-lint: ^0.9
- php-coveralls/php-coveralls: 2.1.0
- phpstan/phpstan: ^0.9
- slevomat/coding-standard: ^2.0
README
Tired of passing mocked dependencies you don't care about to classes under test?
Is this code familiar to you?
$foo1 = $this->getMockBuilder(Foo1::class) ->disableOriginalConstructor() ->getMock(); $foo2 = $this->getMockBuilder(Foo2::class) ->disableOriginalConstructor() ->getMock(); $foo3 = $this->getMockBuilder(Foo3::class) ->disableOriginalConstructor() ->getMock(); $foo4 = $this->getMockBuilder(Foo4::class) ->disableOriginalConstructor() ->getMock(); $interestingDependency = $this->getMock(Foo5::class); $interestingDependency->expects($this->once()) ->method('getAwesome') ->getMock(); // public function __construct(Foo1 $foo1, Foo2 $foo2, Foo3 $foo3, Foo4 $foo4, Foo5 $foo5) $bar = new Bar($foo1, $foo2, $foo3, $foo4, $interestingDependency);
With Mocktainer, you can reduce the above code to this:
$interestingDependency = $this->getMock(Foo5::class); $interestingDependency->expects($this->once()) ->method('getAwesome') ->getMock(); $bar = $this->getMocktainer()->create(Bar::class, ['foo5' => $interestingDependency]);
Other mockable constructor arguments will be mocked using MockBuilder
and disableOriginalConstructor()
.