/** * @Given /^the JSON node "([^"]*)" of the objects of the JSON node "([^"]*)" should contains "([^"]*)"$/ * * @param string $node * @param string $collection * @param string $arrayValue */ public function theJSONNodeOfTheObjectsOfTheJSONNodeShouldContains($node, $collection, $arrayValue) { $accessor = PropertyAccess::createPropertyAccessor(); $json = $this->getSession()->getPage()->getContent(); $collection = $this->inspector->evaluate(new Json($json), $collection); foreach ($collection as $element) { in_array($arrayValue, $accessor->getValue($element, $node)); } }
/** * @Then the JSON response should have the following nodes: * * @param TableNode $table */ public function theJSONResponseIsComposedOf(TableNode $table) { $count = 0; foreach ($table->getColumnsHash() as $row) { ++$count; $expectedValue = $row['value']; // Check for null value // The `~` is used to specify null value (like in YAML) unless the type is explicitly set to string // in which case will be processed as the string '~'. if ('~' === $expectedValue && (false === array_key_exists('type', $row) || 'string' !== $row['type'])) { $this->theJsonNodeShouldBeNull($row['node']); continue; } // Default type is set to string $expectedValueType = 'string'; if (array_key_exists('type', $row)) { // Trim the expected value type of all spaces before using its value $_expectedValueType = str_replace(' ', '', $row['type']); if (false === empty($_expectedValueType)) { $expectedValueType = $_expectedValueType; } unset($_expectedValueType); } $expectedValue = $this->normalizeValue($row['value'], $expectedValueType); if (true === is_bool($expectedValue) || true === is_int($expectedValue)) { PHPUnit::assertEquals($expectedValue, $this->inspector->evaluate($this->getJson(), $row['node'])); continue; } if ('array' === $expectedValueType) { $this->theJsonNodeShouldBeAnArray($row['node']); continue; } if ('object' === $expectedValueType) { $this->theJsonNodeShouldBeAnObject($row['node']); continue; } // If want to compare to an empty string, the value must be `""` in the table // Otherwise an empty string means no check on the value if ('""' === $expectedValue) { $this->theJsonNodeShouldBeEqualTo($row['node'], ''); continue; } if ('' === $expectedValue) { $this->theJsonNodeShouldExist($row['node']); continue; } $this->theJsonNodeShouldBeEqualTo($row['node'], $expectedValue); } $nbrOfNodes = $this->getNumberOfNodes($this->getJson()->getContent()); PHPUnit::assertEquals($nbrOfNodes, $count, sprintf('Expected to find %d nodes. Found %d instead', $nbrOfNodes, $count)); }
public function test_should_valid_json_through_its_schema() { $this->given($json = new \mock\Sanpi\Behatch\Json\Json('{}'), $schema = new \mock\Sanpi\Behatch\Json\JsonSchema('{}'), $schema->getMockController()->resolve = $schema, $schema->getMockController()->validate = 'foobar', $inspector = new TestedClass('foo'))->when($result = $inspector->validate($json, $schema))->variable($result)->isEqualTo('foobar')->mock($schema)->call('resolve')->withArguments(new RefResolver(new UriRetriever()))->once()->call('validate')->withArguments($json, new Validator())->once(); }