Passed
Push — master ( 4826f7...743cc2 )
by Oleg
04:24
created

RoutesTest::loadDummyControllers()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 27
nc 2
nop 0
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Generator\Factory;
5
6
use Interop\Container\ContainerInterface;
7
use org\bovigo\vfs\vfsStream;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\Prophecy\ObjectProphecy;
10
use SlayerBirden\DFCodeGeneration\PrintFileTrait;
11
use Zend\Expressive\Application;
12
use Zend\Expressive\Router\RouterInterface;
13
use Zend\ServiceManager\Factory\DelegatorFactoryInterface;
14
15
class RoutesTest extends TestCase
16
{
17
    use PrintFileTrait;
18
    /**
19
     * @var ObjectProphecy
20
     */
21
    private $provider;
22
    private $router;
23
    /**
24
     * @var Application
25
     */
26
    private $app;
27
28
    protected function setUp()
29
    {
30
        $this->provider = $this->prophesize(DataProviderInterface::class);
31
        $this->provider->getRoutesDelegatorNameSpace()->willReturn('Dummy\\Factory');
32
        $this->provider->getControllerNameSpace()->willReturn('Dummy\\Controller');
33
        $this->provider->getShortName()->willReturn('Dummy');
34
35
        $this->router = $this->prophesize(RouterInterface::class);
36
37
        $this->app = new Application(
38
            $this->router->reveal()
39
        );
40
    }
41
42
    public function testAddRoutesDelegator()
43
    {
44
        $routesGenerator = new Routes($this->provider->reveal());
45
        $body = $routesGenerator->generate();
46
        $this->loadDummyControllers();
47
48
        $root = vfsStream::setup();
49
        file_put_contents($root->url() . '/dummyRoutesDelegator.php', $body);
50
        try {
51
            include $root->url() . '/dummyRoutesDelegator.php';
52
        } catch (\ParseError $exception) {
53
            echo 'File', PHP_EOL, $this->getPrintableFile($body), PHP_EOL;
54
            throw $exception;
55
        }
56
57
        $class = $routesGenerator->getClassName();
58
        /** @var DelegatorFactoryInterface $delegator */
59
        $delegator = new $class();
60
61
        $container = $this->prophesize(ContainerInterface::class);
62
63
        /** @var Application $app */
64
        $app = $delegator($container->reveal(), 'dummy', function () {
65
            return $this->app;
66
        });
67
68
        $routes = $app->getRoutes();
69
70
        $this->assertCount(5, $routes);
71
    }
72
73
    private function loadDummyControllers()
74
    {
75
        $template = <<<'CONTROLLER'
76
<?php
77
namespace Dummy\Controller;
78
        
79
use Interop\Http\ServerMiddleware\DelegateInterface;
80
use Interop\Http\ServerMiddleware\MiddlewareInterface;
81
use Psr\Http\Message\ServerRequestInterface;
82
83
class %s implements MiddlewareInterface
84
{
85
    public function process(ServerRequestInterface $request, DelegateInterface $handler): ResponseInterface
86
    {
87
        return $handler->process($request);
88
    }
89
}
90
CONTROLLER;
91
92
        $actions = [
93
            'GetDummyAction',
94
            'GetDummysAction',
95
            'AddDummyAction',
96
            'DeleteDummyAction',
97
            'UpdateDummyAction'
98
        ];
99
        $root = vfsStream::setup();
100
        foreach ($actions as $action) {
101
            $body = sprintf($template, $action);
102
            file_put_contents($root->url() . "/$action.php", $body);
103
            include $root->url() . "/$action.php";
104
        }
105
    }
106
107
    public function testGetClass()
108
    {
109
        $this->assertSame('Dummy\\Factory\\DummyRoutesDelegator',
110
            (new Routes($this->provider->reveal()))->getClassName());
111
    }
112
}
113