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 Add 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 = 'Add' . $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('add' . $baseName)) |
32
|
|
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
33
|
|
|
->setBody($this->getSuccessCase()) |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
$class->addMethodFromGenerator( |
37
|
|
|
(new MethodGenerator('addInvalid' . $baseName)) |
38
|
|
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
39
|
|
|
->setBody($this->getValidationCase()) |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$class->addMethodFromGenerator( |
43
|
|
|
(new MethodGenerator('addFailedConstraint' . $baseName)) |
44
|
|
|
->setParameter((new ParameterGenerator('I'))->setType('\ApiTester')) |
45
|
|
|
->setBody($this->getUniqueConstraintCase()) |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
return (new FileGenerator()) |
49
|
|
|
->setClass($class) |
50
|
|
|
->generate(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
56
|
|
|
* @throws \ReflectionException |
57
|
|
|
*/ |
58
|
|
|
private function getSuccessCase(): string |
59
|
|
|
{ |
60
|
|
|
$body = <<<'BODY' |
61
|
|
|
$I->wantTo('create %1$s'); |
62
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
63
|
|
|
$I->sendPOST('/%1$s', %2$s); |
64
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
65
|
|
|
$I->seeResponseContainsJson([ |
66
|
|
|
'success' => true, |
67
|
|
|
'data' => [ |
68
|
|
|
'%1$s' => %2$s |
69
|
|
|
] |
70
|
|
|
]); |
71
|
|
|
BODY; |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
return sprintf($body, $this->shortName, var_export($this->getPostParams(), true)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
80
|
|
|
* @throws \ReflectionException |
81
|
|
|
*/ |
82
|
|
|
private function getValidationCase(): string |
83
|
|
|
{ |
84
|
|
|
$params = $this->getPostParams(); |
85
|
|
|
if (count($params) > 0) { |
86
|
|
|
$body = <<<'BODY' |
87
|
|
|
$I->wantTo('create incomplete %1$s'); |
88
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
89
|
|
|
$I->sendPOST('/%1$s', %2$s); |
90
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
91
|
|
|
$I->seeResponseContainsJson([ |
92
|
|
|
'success' => false, |
93
|
|
|
'data' => [ |
94
|
|
|
'validation' => %3$s |
95
|
|
|
] |
96
|
|
|
]); |
97
|
|
|
BODY; |
98
|
|
|
|
99
|
|
|
$validation = []; |
100
|
|
|
foreach ($params as $key => $val) { |
101
|
|
|
$validation[] = [ |
102
|
|
|
'field' => $key |
103
|
|
|
]; |
104
|
|
|
unset($params[$key]); |
105
|
|
|
break; |
106
|
|
|
} |
107
|
|
|
return sprintf($body, $this->shortName, var_export($params, true), var_export($validation, true)); |
108
|
|
|
} else { |
109
|
|
|
return '//TODO add validation case'; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function getUniqueConstraintCase(): string |
114
|
|
|
{ |
115
|
|
|
if (empty($this->unique)) { |
116
|
|
|
return '//TODO add unique case'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$params = $this->getHaveInRepoParams($this->entityClassName); |
120
|
|
|
foreach ($params as $key => $param) { |
121
|
|
|
if (is_object($param) && method_exists($param, 'getId')) { |
122
|
|
|
$params[$key] = $param->getId(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$body = <<<'BODY' |
127
|
|
|
$I->wantTo('create %1$s with failed constraint'); |
128
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
129
|
|
|
$I->sendPOST('/%1$s', %2$s); |
130
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
131
|
|
|
$I->seeResponseContainsJson([ |
132
|
|
|
'success' => false, |
133
|
|
|
]); |
134
|
|
|
BODY; |
135
|
|
|
|
136
|
|
|
return sprintf($body, $this->shortName, var_export($params, true)); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|