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

Get::generate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
ccs 0
cts 19
cp 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Generator\Tests;
5
6
use Zend\Code\Generator\ClassGenerator;
7
use Zend\Code\Generator\FileGenerator;
8
use Zend\Code\Generator\MethodGenerator;
9
use Zend\Code\Generator\ParameterGenerator;
10
11
class Get extends AbstractTest
12
{
13
    /**
14
     * @return string
15
     * @throws \Doctrine\Common\Annotations\AnnotationException
16
     * @throws \ReflectionException
17
     */
18
    public function generate(): string
19
    {
20
        $className = 'Get' . $this->getBaseName($this->entityClassName) . 'Cest';
21
        $baseName = $this->getBaseName($this->entityClassName);
22
        $class = new ClassGenerator($className);
23
24
        $class->addMethodFromGenerator(
25
            (new MethodGenerator('_before'))
26
                ->setParameter((new ParameterGenerator('I'))->setType('\ApiTester'))
27
                ->setBody($this->getBefore())
28
        );
29
30
        $class->addMethodFromGenerator(
31
            (new MethodGenerator('get' . $baseName))
32
                ->setParameter((new ParameterGenerator('I'))->setType('\ApiTester'))
33
                ->setBody($this->getSuccessCase())
34
        );
35
36
        $class->addMethodFromGenerator(
37
            (new MethodGenerator('getNonExisting' . $baseName))
38
                ->setParameter((new ParameterGenerator('I'))->setType('\ApiTester'))
39
                ->setBody($this->getNonExistingCase())
40
        );
41
42
        return (new FileGenerator())
43
            ->setClass($class)
44
            ->generate();
45
    }
46
47
    private function getSuccessCase(): string
48
    {
49
        $body = <<<'BODY'
50
$I->wantTo('get %1$s');
51
$I->haveHttpHeader('Content-Type', 'application/json');
52
$I->sendGET('/%1$s/%2$d');
53
$I->seeResponseCodeIs(HttpCode::OK);
54
$I->seeResponseContainsJson([
55
    'success' => true,
56
    'data' => [
57
        '%1$s' => %3$s
58
    ]
59
]);
60
BODY;
61
62
        $id = $this->getId($this->entityClassName);
63
        $params = $this->getHaveInRepoParams($this->entityClassName);
64
        if (isset($params['id'])) {
65
            unset($params['id']);
66
        }
67
68
        return sprintf($body, $this->shortName, $id, var_export($params, true));
69
    }
70
71
    private function getNonExistingCase(): string
72
    {
73
        $body = <<<'BODY'
74
$I->wantTo('get non-existing %1$s');
75
$I->haveHttpHeader('Content-Type', 'application/json');
76
$I->sendGET('/%1$s/0');
77
$I->seeResponseCodeIs(HttpCode::NOT_FOUND);
78
$I->seeResponseContainsJson([
79
    'success' => false,
80
    'data' => [
81
        '%1$s' => null
82
    ]
83
]);
84
BODY;
85
86
        return sprintf($body, $this->shortName);
87
    }
88
}
89