|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +use Behat\Behat\Context\Context; |
| 13 | +use Behat\Behat\Context\Environment\InitializedContextEnvironment; |
| 14 | +use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
| 15 | +use Sanpi\Behatch\Context\RestContext; |
| 16 | +use Symfony\Component\PropertyAccess\PropertyAccess; |
| 17 | + |
| 18 | +final class SwaggerContext implements Context |
| 19 | +{ |
| 20 | + private $propertyAccessor; |
| 21 | + |
| 22 | + public function __construct() |
| 23 | + { |
| 24 | + $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Gives access to the Behatch context. |
| 29 | + * |
| 30 | + * @param BeforeScenarioScope $scope |
| 31 | + * |
| 32 | + * @BeforeScenario |
| 33 | + */ |
| 34 | + public function gatherContexts(BeforeScenarioScope $scope) |
| 35 | + { |
| 36 | + /** @var InitializedContextEnvironment $environment */ |
| 37 | + $environment = $scope->getEnvironment(); |
| 38 | + $this->restContext = $environment->getContext(RestContext::class); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @Then the Swagger class ":class" exist |
| 43 | + */ |
| 44 | + public function assertTheSwaggerClassExist($className) |
| 45 | + { |
| 46 | + $this->getClassInfos($className); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @Then the Swagger class ":class" not exist |
| 51 | + */ |
| 52 | + public function assertTheSwaggerClassNotExist($className) |
| 53 | + { |
| 54 | + try { |
| 55 | + $this->getClassInfos($className); |
| 56 | + |
| 57 | + throw new \PHPUnit_Framework_ExpectationFailedException(sprintf('The class "%s" exist.', $className)); |
| 58 | + } catch (\Exception $exception) { |
| 59 | + // an exception must be catched |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @Then the value of the node ":node" of the Swagger class ":class" is ":value" |
| 65 | + */ |
| 66 | + public function assertNodeValueIs($nodeName, $className, $value) |
| 67 | + { |
| 68 | + $classInfos = $this->getClassInfos($className); |
| 69 | + \PHPUnit_Framework_Assert::assertEquals($this->propertyAccessor->getValue($classInfos, $nodeName), $value); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @Then the value of the node ":node" of the property ":prop" of the Swagger class ":class" is ":value" |
| 74 | + */ |
| 75 | + public function assertPropertyNodeValueIs($nodeName, $propertyName, $className, $value) |
| 76 | + { |
| 77 | + $property = $this->getProperty($propertyName, $className); |
| 78 | + if (empty($property)) { |
| 79 | + throw new \PHPUnit_Framework_ExpectationFailedException( |
| 80 | + sprintf('The property "%s" for the class "%s" exist.', $propertyName, $className) |
| 81 | + ); |
| 82 | + } |
| 83 | + \PHPUnit_Framework_Assert::assertEquals($this->propertyAccessor->getValue($property, $nodeName), $value); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @Then the value of the node ":node" of the operation ":operation" of the Swagger class ":class" is ":value" |
| 88 | + */ |
| 89 | + public function assertOperationNodeValueIs($nodeName, $operationMethod, $className, $value) |
| 90 | + { |
| 91 | + $property = $this->getOperation($operationMethod, $className); |
| 92 | + |
| 93 | + \PHPUnit_Framework_Assert::assertEquals($this->propertyAccessor->getValue($property, $nodeName), $value); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @Then :nb operations are available for Swagger class ":class" |
| 98 | + */ |
| 99 | + public function assertNbOperationsExist($nb, $className) |
| 100 | + { |
| 101 | + $operations = $this->getOperations($className); |
| 102 | + |
| 103 | + \PHPUnit_Framework_Assert::assertEquals($nb, count($operations)); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @Then :nb properties are available for Swagger class ":class" |
| 108 | + */ |
| 109 | + public function assertNbPropertiesExist($nb, $className) |
| 110 | + { |
| 111 | + $properties = $this->getProperties($className); |
| 112 | + |
| 113 | + \PHPUnit_Framework_Assert::assertEquals($nb, count($properties)); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @Then ":prop" property doesn't exist for the Swagger class ":class" |
| 118 | + */ |
| 119 | + public function assertPropertyNotExist($propertyName, $className) |
| 120 | + { |
| 121 | + $property = $this->getProperty($propertyName, $className); |
| 122 | + if (empty($property)) { |
| 123 | + throw new \PHPUnit_Framework_ExpectationFailedException( |
| 124 | + sprintf('The property "%s" for the class "%s" exist.', $propertyName, $className) |
| 125 | + ); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @Then ":prop" property is required for Swagger class ":class" |
| 131 | + */ |
| 132 | + public function assertPropertyIsRequired(string $propertyName, string $className) |
| 133 | + { |
| 134 | + $classInfo = $this->getClassInfos($className); |
| 135 | + if (!in_array($propertyName, $classInfo->{'required'})) { |
| 136 | + throw new \Exception(sprintf('Property "%s" of class "%s" is not required', $propertyName, $className)); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private function getProperty(string $propertyName, string $className) : stdClass |
| 141 | + { |
| 142 | + $properties = $this->getProperties($className); |
| 143 | + $propertyInfos = null; |
| 144 | + foreach ($properties as $classPropertyName => $property) { |
| 145 | + if ($classPropertyName === $propertyName) { |
| 146 | + return $property; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return new stdClass(); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Gets an operation by its method name. |
| 155 | + * |
| 156 | + * @param string $className |
| 157 | + * @param string $method |
| 158 | + * |
| 159 | + * @throws Exception |
| 160 | + * |
| 161 | + * @return array | stdClass |
| 162 | + */ |
| 163 | + private function getOperation(string $method, string $className) : stdClass |
| 164 | + { |
| 165 | + foreach ($this->getOperations($className) as $classMethod => $operation) { |
| 166 | + if ($classMethod === $method) { |
| 167 | + return $operation; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + throw new \Exception(sprintf('Operation "%s" of class "%s" does not exist.', $method, $className)); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Gets all operations of a given class. |
| 176 | + */ |
| 177 | + private function getOperations(string $className) : stdClass |
| 178 | + { |
| 179 | + $classInfos = $this->getClassInfos($className); |
| 180 | + |
| 181 | + return empty($classInfos) ? $classInfos : new stdClass(); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Gets all properties of a given class. |
| 186 | + */ |
| 187 | + private function getProperties(string $className) : stdClass |
| 188 | + { |
| 189 | + $classInfos = $this->getClassInfos($className); |
| 190 | + |
| 191 | + return empty($classInfos->{'properties'}) ? $classInfos->{'properties'} : new stdClass(); |
| 192 | + } |
| 193 | + |
| 194 | + private function getClassInfos(string $className, bool $getOperation = false) : stdClass |
| 195 | + { |
| 196 | + $json = $this->getLastJsonResponse(); |
| 197 | + $classInfos = null; |
| 198 | + |
| 199 | + if (isset($json->{'definitions'}) && !$getOperation) { |
| 200 | + foreach ($json->{'definitions'} as $classTitle => $classData) { |
| 201 | + if ($classTitle === $className) { |
| 202 | + $classInfos = $classData; |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + if (isset($json->{'paths'}) && $getOperation) { |
| 208 | + foreach ($json->{'paths'} as $classTitle => $classPath) { |
| 209 | + foreach ($classPath as $classOperations) { |
| 210 | + foreach ($classOperations as $classOperation) { |
| 211 | + if (in_array($className, $classOperation['tags'])) { |
| 212 | + $classInfos = $classOperations; |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + if (empty($classInfos)) { |
| 220 | + throw new \Exception(sprintf('Class %s cannot be found in the vocabulary', $className)); |
| 221 | + } |
| 222 | + |
| 223 | + return $classInfos; |
| 224 | + } |
| 225 | + |
| 226 | + private function getLastJsonResponse() : stdClass |
| 227 | + { |
| 228 | + $content = $this->restContext->getMink()->getSession()->getDriver()->getContent(); |
| 229 | + if (null === ($decoded = json_decode($content))) { |
| 230 | + throw new \RuntimeException('JSON response seems to be invalid'); |
| 231 | + } |
| 232 | + |
| 233 | + return $decoded; |
| 234 | + } |
| 235 | +} |
0 commit comments