Get support for mnapoli/mockup
If you're new to LTH, please see our FAQ for more information on what it is we do.
Support Options
Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:
View Open IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck out the page and see if there are any options to sponsor this project or it's developers directly.
mnapoli/mockup
Mockup
Concise mock library for PHP tests.
Why?
This mock library is meant to be a simple yet powerful alternative to existing solutions.
- Mockup
$mock = mock(Foo::class, [
'foo' => 'Hello',
]);
- PHPUnit
$mock = $this->getMock(Foo::class, [], [], '', false);
$mock->expect($this->any())
->method('foo')
->willReturn('Hello');
- Prophecy
$prophet = new \Prophecy\Prophet();
$mock = $prophet->prophesize(Foo::class);
$mock->foo()->willReturn('Hello');
$mock = $mock->reveal();
- Mockery
$mock = Mockery::mock(Foo::class);
$mock->shouldReceive('foo')
->andReturn('Hello');
Additionally, Mockup doesn't include assertions. Instead of forcing you to learn a specific assertion syntax, complex enough to cover all cases, it lets you use the assertions you already know (PHPUnit, phpspec, …). Here is an example in a PHPUnit test:
// The mock method was called once
$this->assertEquals(1, inspect($mock)->foo()->invokationCount());
Read more about spying method calls below.
Installation
composer require --dev mnapoli/mockup
Usage
Mocks
You can mock a class or an interface:
use function Mockup\mock;
interface Foo
{
public function foo($bar);
}
$mock = mock(Foo::class);
$mock->foo();
All its methods will do nothing and return null
(null object pattern). The mock will implement the interface or extend the class given, as such it will work fine with any type-hint.
You can make some methods return values other than null:
$mock = mock(Foo::class, [
'foo' => 'hello',
]);
$mock->foo('john'); // hello
You can also use a closure to define the new method's body:
$mock = mock(Foo::class, [
'foo' => function ($bar) {
return strtoupper('hello ' . $bar);
}
]);
$mock->foo('john'); // HELLO JOHN
Spies
You can spy calls to an object:
use function Mockup\{spy, inspect};
$spy = spy($cache);
$foo->doSomething($spy);
inspect($spy)->set()->invokationCount(); // number of calls to $spy->set()
inspect($spy)->set()->parameters(0); // parameters provided to the first call to $spy->set()
inspect($spy)->set()->returnValue(0); // value returned by the first call to $spy->set()
The difference with a mock is that you are spying real calls to a real object. A mock is a null object.
Mockup does not provide assertions or expectations so that you can use the assertion library you prefer.
Every mock object is also a spy, so you can create a mock and spy its method calls:
use function Mockup\{mock, inspect};
$mock = mock(CacheInterface::class);
$foo->doSomething($mock);
inspect($spy)->set()->invokationCount();
inspect($spy)->set()->parameters(0);
inspect($spy)->set()->returnValue(0);
Our Mission
We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2