Skip to content

Commit b74022c

Browse files
committed
Add re-usable ComponentContextMock for unit tests
1 parent 157894e commit b74022c

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Test\Unit\Mock;
4+
5+
use BadMethodCallException;
6+
use Loki\Components\Component\AbstractComponentContext;
7+
8+
trait ComponentContextMock
9+
{
10+
protected function getComponentContextMock(array $contextDependencies = []): AbstractComponentContext
11+
{
12+
$context = $this->getMockBuilder(AbstractComponentContext::class)
13+
->disableOriginalConstructor()
14+
->onlyMethods(['__call'])
15+
->getMock();
16+
17+
$context->method('__call')
18+
->willReturnCallback(function (string $name, array $args) use ($contextDependencies) {
19+
if ($args !== []) {
20+
throw new BadMethodCallException("Unexpected args for $name");
21+
}
22+
23+
if (array_key_exists($name, $contextDependencies)) {
24+
return $contextDependencies[$name];
25+
}
26+
27+
throw new BadMethodCallException("Unexpected method: $name");
28+
});
29+
30+
return $context;
31+
}
32+
}

0 commit comments

Comments
 (0)