Passed
Push — master ( 743cc2...6ffec3 )
by Oleg
02:50
created

GetTest::initDangerMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Generator\Controllers;
5
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use Doctrine\ORM\EntityManagerInterface;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\Argument;
10
use Prophecy\Prophecy\MethodProphecy;
11
use Prophecy\Prophecy\ObjectProphecy;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Log\LoggerInterface;
14
use SlayerBirden\DFCodeGeneration\CodeLoader;
15
use SlayerBirden\DFCodeGeneration\PrintFileTrait;
16
use Zend\Diactoros\ServerRequest;
17
use Zend\Expressive\Handler\NotFoundHandler;
18
use Zend\Hydrator\ExtractionInterface;
19
20
class GetTest extends TestCase
21
{
22
    use PrintFileTrait;
23
    /**
24
     * @var ObjectProphecy
25
     */
26
    private $provider;
27
28
    protected function setUp()
29
    {
30
        $this->provider = $this->prophesize(DataProviderInterface::class);
31
        $this->provider->provide()->willReturn([
32
            'ns' => 'Dummy\\Controller',
33
            'entityName' => 'User',
34
            'hasUnique' => false,
35
            'uniqueIdxMessage' => 'Test Constraint violation',
36
            'useStatement' => 'Dummy\\Entities\\User',
37
            'dataRelationship' => '// testing here',
38
        ]);
39
        $this->provider->getClassName(Argument::type('string'))->willReturn('Dummy\\Controller\\GetUserAction');
40
    }
41
42
    public function testGenerate()
43
    {
44
        $generator = new Get($this->provider->reveal());
45
46
        $body = $generator->generate();
47
48
        $em = $this->prophesize(EntityManagerInterface::class);
49
        $em->addMethodProphecy(
50
            (new MethodProphecy($em, 'getRepository', [Argument::any()]))
51
                ->willReturn($this->prophesize(ObjectRepository::class))
52
        );
53
54
        try {
55
            CodeLoader::loadCode($body, 'GetUserAction.php');
56
            $className = $generator->getClassName();
57
            $action = new $className(
58
                $em->reveal(),
59
                $this->prophesize(LoggerInterface::class)->reveal(),
60
                $this->prophesize(ExtractionInterface::class)->reveal()
61
            );
62
            $request = new ServerRequest();
63
            $delegator = $this->prophesize(NotFoundHandler::class);
64
            $this->initDangerMessage();
65
            $resp = $action->process($request, $delegator->reveal());
66
        } catch (\Throwable $exception) {
67
            echo 'File', PHP_EOL, $this->getPrintableFile($body), PHP_EOL;
68
            throw $exception;
69
        }
70
71
        $this->assertInstanceOf(ResponseInterface::class, $resp);
72
    }
73
74
    private function initDangerMessage()
75
    {
76
        $body = <<<'BODY'
77
<?php
78
79
namespace SlayerBirden\DataFlowServer\Notification;
80
class DangerMessage {}
81
BODY;
82
        CodeLoader::loadCode($body, 'danger.php');
83
    }
84
}
85