Sure!
Let’s say you have the following delegator factory registering a single route:
namespace Acme;
use Psr\Container\ContainerInterface;
class HomeRouteDelegatorFactory
{
public function __invoke(ContainerInterface $container, $serviceName, callable $callback)
{
$app = $callback();
$app->get('/', Action\HomePageAction::class, 'home');
return $app;
}
}
You could write a test like the following:
<?php
namespace AcmeTest;
use Acme\Action\HomePageAction;
use Acme\HomeRouteDelegatorFactory;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Application;
class HomeRouteDelegatorFactoryTest extends TestCase
{
public function testRegistersHomePageRoute()
{
$container = $this->prophesize(ContainerInterface::class)->reveal();
$app = $this->prophesize(Application::class);
$app->get('/', HomePageAction::class, 'home')->shouldBeCalled();
$callback = function () use ($app) {
return $app->reveal();
};
$delegator = new HomeRouteDelegatorFactory();
$result = $delegator($container, Application::class, $callback);
$this->assertSame($app->reveal(), $result);
}
}
The above mocks the Application
class, writes expectations against it, and
then creates a callback that will return it. This is passed to the delegator,
and we can then verify the Application
is what is returned.