Grade
Issues
9
Complexity
30
Duplication
-
Coverage
-
1<?php 2namespace Tests\Functional\BehatContext; 3 4use Behat\Gherkin\Node\PyStringNode; 5use PHPUnit\Framework\Assert; 6use Symfony\Component\Validator\Constraint; 7use Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper\ConstraintPayloadDocHelper; 8use Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper\DocTypeHelper; 9use Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper\MinMaxHelper; 10use Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper\StringDocHelper; 11use Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper\TypeGuesser; 12use Yoanm\JsonRpcParamsSymfonyConstraintDoc\Infra\Transformer\ConstraintToParamsDocTransformer; 13use Yoanm\JsonRpcServerDoc\Domain\Model\Type\CollectionDoc; 14use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc; 15 16class ConstraintTransformerContext extends AbstractContext 1 issue17{ 18 /** @var null|TypeDoc */ 19 private $lastDocumenation = null; 20 /** @var Constraint|null */ 21 private $lastConstraint = null; 22 23 /** 24 * @Given I have the following Constraint: 25 */ 26 public function givenIHaveTheFollowingConstraint(PyStringNode $phpCode) 27 { 28 $this->lastConstraint = eval($phpCode->getRaw()); 1 issue29 } 30 31 /** 32 * @When I transform constraint 33 */ 34 public function whenINormalizeConstraint() 35 { 36 $constraintPayloadDocHelper = new ConstraintPayloadDocHelper(); 1 issue37 $transformer = new ConstraintToParamsDocTransformer( 38 new DocTypeHelper( 39 $constraintPayloadDocHelper, 40 new TypeGuesser() 41 ), 42 new StringDocHelper(), 43 new MinMaxHelper(), 44 $constraintPayloadDocHelper 45 ); 46 if (is_array($this->lastConstraint)) { 47 $this->lastDocumenation = $transformer->transformList($this->lastConstraint); 48 } else { 49 $this->lastDocumenation = $transformer->transform($this->lastConstraint); 50 } 51 } 52 53 /** 54 * @Then I should have a constraint doc of class :class 55 */ 56 public function thenIShouldHaveFollowingConstraintDoc($class) 57 { 58 // use ::class notation to avoid issue with inheritance 59 Assert::assertSame($class, get_class($this->lastDocumenation)); 1 issue60 } 61 62 /** 63 * @Then constraint doc item validation should be of type :class 64 */ 65 public function thenConstraintDocItemValidationShouldBeOfType($class) 66 { 67 $itemValidation = $this->lastDocumenation->getItemValidation(); 68 Assert::assertSame($class, get_class($itemValidation)); 1 issue69 } 70 71 /** 72 * @Then constraint doc should have :count sibling 73 * @Then constraint doc should have :count siblings 74 */ 75 public function thenConstraintDocShouldHaveXSiblings($count) 76 { 77 Assert::assertCount((int) $count, $this->lastDocumenation->getSiblingList()); 1 issue78 } 79 80 /** 81 * @Then constraint doc should have a sibling :siblingName of class :class 82 */ 83 public function thenConstraintDocShouldHaveASiblingName($siblingName, $class = null) 84 { 85 $sibling = $this->findSiblingNamed($siblingName); 86 if (null !== $class) { 87 Assert::assertInstanceOf($class, $sibling); 1 issue88 } 89 } 90 91 /** 92 * @Then constraint doc sibling :siblingName :methodName should return the value :result 93 */ 94 public function thenConstraintDocSiblingMethodShouldReturn($siblingName, $methodName, $result) 95 { 96 $sibling = $this->findSiblingNamed($siblingName); 97 $this->assertMethodCallResult($sibling, $methodName, $result); 98 } 99 100 /** 101 * @Then constraint doc sibling :siblingName :methodName should return the number :result 102 */ 103 public function thenConstraintDocSiblingMethodShouldReturnInteger($methodName, $result) 104 { 105 $this->assertMethodCallResult( 106 $this->lastDocumenation, 107 $methodName, 108 (int) $result == $result ? (int) $result : (float) $result 109 ); 110 } 111 112 /** 113 * @Then constraint doc sibling :siblingName :methodName should return null 114 */ 115 public function thenConstraintDocSiblingMethodShouldReturnNull($siblingName, $methodName) 116 { 117 $sibling = $this->findSiblingNamed($siblingName); 118 $this->assertMethodCallResult($sibling, $methodName, null); 119 } 120 121 /** 122 * @Then constraint doc sibling :siblingName :methodName should return true 123 */ 124 public function thenConstraintDocSiblingMethodShouldReturnTrue($siblingName, $methodName) 125 { 126 $sibling = $this->findSiblingNamed($siblingName); 127 $this->assertMethodCallResult($sibling, $methodName, true); 128 } 129 130 /** 131 * @Then constraint doc sibling :siblingName :methodName should return false 132 */ 133 public function thenConstraintDocSiblingMethodShouldReturnFalse($siblingName, $methodName) 134 { 135 $sibling = $this->findSiblingNamed($siblingName); 136 $this->assertMethodCallResult($sibling, $methodName, false); 137 } 138 139 /** 140 * @Then constraint doc sibling :siblingName :methodName should return an empty array 141 */ 142 public function thenConstraintDocSiblingMethodShouldReturnEmptyArray($siblingName, $methodName) 143 { 144 $sibling = $this->findSiblingNamed($siblingName); 145 $this->assertMethodCallResult($sibling, $methodName, []); 146 } 147 148 /** 149 * @Then constraint doc :methodName should return: 150 */ 151 public function thenIShouldHaveAConstraintDocWhichReturnsPyStringNode($methodName, PyStringNode $result) 152 { 153 $this->thenConstraintDocMethodShouldReturn( 154 $methodName, 155 $this->jsonDecode($result->getRaw()) 156 ); 157 } 158 159 /** 160 * @Then constraint doc :methodName should return the value :result 161 */ 162 public function thenConstraintDocMethodShouldReturn($methodName, $result) 163 { 164 $this->assertMethodCallResult($this->lastDocumenation, $methodName, $result); 165 } 166 167 /** 168 * @Then constraint doc :methodName should contain the value :result 169 */ 170 public function thenConstraintDocMethodShouldContain($methodName, $result) 171 { 172 $this->assertMethodCallContains($this->lastDocumenation, $methodName, $result); 173 } 174 175 /** 176 * @Then constraint doc :methodName should return the number :result 177 */ 178 public function thenConstraintDocMethodShouldReturnInteger($methodName, $result) 179 { 180 $this->assertMethodCallResult( 181 $this->lastDocumenation, 182 $methodName, 183 (int) $result == $result ? (int) $result : (float) $result 184 ); 185 } 186 187 /** 188 * @Then constraint doc :methodName should return null 189 */ 190 public function thenConstraintDocMethodShouldReturnNull($methodName) 191 { 192 $this->assertMethodCallResult($this->lastDocumenation, $methodName, null); 193 } 194 195 /** 196 * @Then constraint doc :methodName should return true 197 */ 198 public function thenConstraintDocMethodShouldReturnTrue($methodName) 199 { 200 $this->assertMethodCallResult($this->lastDocumenation, $methodName, true); 201 } 202 203 /** 204 * @Then constraint doc :methodName should return false 205 */ 206 public function thenConstraintDocMethodShouldReturnFalse($methodName) 207 { 208 $this->assertMethodCallResult($this->lastDocumenation, $methodName, false); 209 } 210 211 /** 212 * @Then constraint doc :methodName should return an empty array 213 */ 214 public function thenConstraintDocMethodShouldReturnEmptyArray($methodName) 215 { 216 $this->assertMethodCallResult($this->lastDocumenation, $methodName, []); 217 } 218 219 /** 220 * @param string $siblingName 221 * 222 * @return mixed 223 * @throws \Exception 224 */ 225 private function findSiblingNamed($siblingName) 226 { 227 Assert::assertInstanceOf(CollectionDoc::class, $this->lastDocumenation); 1 issue228 229 foreach ($this->lastDocumenation->getSiblingList() as $sibling) { 230 if ($siblingName === $sibling->getName()) { 231 return $sibling; 232 } 233 } 234 235 throw new \Exception(sprintf('Unable to find sibling named "%s"', $siblingName)); 236 } 237 238 /** 239 * @param $object 240 * @param $methodName 241 * @param $result 242 */ 243 private function assertMethodCallResult($object, $methodName, $result): void 244 { 245 Assert::assertSame($result, call_user_func_array([$object, $methodName], [])); 1 issue246 } 247 248 /** 249 * @param $object 250 * @param $methodName 251 * @param $result 252 */ 253 private function assertMethodCallContains($object, $methodName, $result): void 254 { 255 Assert::assertStringContainsString($result, call_user_func_array([$object, $methodName], [])); 256 } 257}
Issues severity distribution
0
CRITICAL
1
HIGH
7
MEDIUM
1
MINOR
MEDIUM
Code complexity
features/bootstrap/
ConstraintTransformerContext.php
16class ConstraintTransformerContext extends AbstractContext
HIGH
Security
Insecure Modules Libraries
features/bootstrap/
ConstraintTransformerContext.php
28 $this->lastConstraint = eval($phpCode->getRaw());
MINOR
Code style
features/bootstrap/
ConstraintTransformerContext.php
36 $constraintPayloadDocHelper = new ConstraintPayloadDocHelper();
MEDIUM
Best practice
features/bootstrap/
ConstraintTransformerContext.php
59 Assert::assertSame($class, get_class($this->lastDocumenation));
MEDIUM
Best practice
features/bootstrap/
ConstraintTransformerContext.php
68 Assert::assertSame($class, get_class($itemValidation));
MEDIUM
Best practice
features/bootstrap/
ConstraintTransformerContext.php
77 Assert::assertCount((int) $count, $this->lastDocumenation->getSiblingList());
MEDIUM
Best practice
features/bootstrap/
ConstraintTransformerContext.php
87 Assert::assertInstanceOf($class, $sibling);
MEDIUM
Best practice
features/bootstrap/
ConstraintTransformerContext.php
227 Assert::assertInstanceOf(CollectionDoc::class, $this->lastDocumenation);
MEDIUM
Best practice
features/bootstrap/
ConstraintTransformerContext.php
245 Assert::assertSame($result, call_user_func_array([$object, $methodName], []));