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

GetsTest::testGenerate()   B

Complexity

Conditions 2
Paths 8

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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