Inheritance: implements Coduo\PHPMatcher\Factory
Beispiel #1
0
 public function setUp()
 {
     parent::setUp();
     $this->client = new Client(['base_uri' => self::BASEURL]);
     $matcherFactory = new SimpleFactory();
     $this->matcher = $matcherFactory->createMatcher();
 }
Beispiel #2
0
 /**
  * @param $value
  * @param $pattern
  * @param null $error
  * @return bool
  */
 public static function match($value, $pattern, &$error = null)
 {
     $factory = new SimpleFactory();
     $matcher = $factory->createMatcher();
     if (!$matcher->match($value, $pattern)) {
         $error = $matcher->getError();
         return false;
     }
     return true;
 }
Beispiel #3
0
 protected function assertJsonResponseContent($response, $filename)
 {
     $expectedResponse = file_get_contents(__DIR__ . sprintf('/../Tests/Responses/%s.json', $filename));
     $actualResponse = $response->getContent();
     $actualResponse = json_encode(json_decode($actualResponse), JSON_PRETTY_PRINT);
     $factory = new SimpleFactory();
     $matcher = $factory->createMatcher();
     $result = $matcher->match($actualResponse, $expectedResponse);
     if (!$result) {
         echo $matcher->getError();
         $expectedResponse = explode(PHP_EOL, (string) $expectedResponse);
         $actualResponse = explode(PHP_EOL, (string) $actualResponse);
         $diff = new \Diff($expectedResponse, $actualResponse, array());
         $renderer = new \Diff_Renderer_Text_Unified();
         echo $diff->render($renderer);
     }
     $this->assertTrue($result);
 }
Beispiel #4
0
 /**
  * @dataProvider nullExamples
  */
 public function test_null_value_in_the_json($value, $pattern)
 {
     $factory = new SimpleFactory();
     $matcher = $factory->createMatcher();
     $match = $matcher->match($value, $pattern);
     $this->assertTrue($match, $matcher->getError());
 }
 /**
  * @param string $actualResponse
  * @param string $filename
  * @param string $mimeType
  */
 protected function assertResponseContent($actualResponse, $filename, $mimeType)
 {
     $responseSource = $this->getExpectedResponsesFolder();
     $expectedResponse = file_get_contents(sprintf('%s/%s.%s', $responseSource, $filename, $mimeType));
     $factory = new SimpleFactory();
     $matcher = $factory->createMatcher();
     $result = $matcher->match($actualResponse, $expectedResponse);
     if (!$result) {
         $difference = $matcher->getError();
         $difference = $difference . PHP_EOL;
         $expectedResponse = explode(PHP_EOL, (string) $expectedResponse);
         $actualResponse = explode(PHP_EOL, (string) $actualResponse);
         $diff = new \Diff($expectedResponse, $actualResponse, array());
         $renderer = new \Diff_Renderer_Text_Unified();
         $difference = $difference . $diff->render($renderer);
         $this->fail($difference);
     }
 }
 /**
  * @Then /^the response JSON matches "([^"]*)"$/
  *
  * @param string $expectedJsonFilePath
  *
  * @throws \Exception
  */
 public function theResponseJSONMatches($expectedJsonFilePath)
 {
     $factory = new PHPMatcherFactory();
     $matcher = $factory->createMatcher();
     $responseJson = json_encode(json_decode($this->getResponseWithAssertion()->getContent()), JSON_PRETTY_PRINT);
     $expectedJson = file_get_contents($this->getFilePath($expectedJsonFilePath));
     if ($matcher->match($responseJson, $expectedJson) === false) {
         echo $matcher->getError();
         $difference = new \Diff(explode(PHP_EOL, $expectedJson), explode(PHP_EOL, $responseJson));
         echo $difference->render(new \Diff_Renderer_Text_Unified());
         throw new \Exception('Response JSON did not match.');
     }
 }
Beispiel #7
0
 /**
  * Assert that a response matches fixture data (with wildcard patterns)
  *
  * This uses the matcher format for json files. See the project at
  * https://github.com/coduo/php-matcher for more information.
  *
  * Construct a JSON fixture file with the php-matcher patterns and you can
  * assert that a server response matches the spec, even if the data is
  * changing.
  *
  * @param ResponseInterface $response psr7 response object
  * @param string|resource   $filename path within `$fixtureFolder`
  * @param string            $mimeType file extension and mime type
  *
  * @throws LogicException
  * @throws RuntimeException
  * @return void
  */
 public static function assertResponseContent(ResponseInterface $response, $filename, $mimeType = 'json')
 {
     self::$faker = Faker\Factory::create();
     $spec = self::getFixtureText($filename, $mimeType);
     $actual = $response->getBody()->getContents();
     // Check that the spec and actual are both valid json
     $test = json_decode($spec);
     if ($test === null && json_last_error() !== JSON_ERROR_NONE) {
         self::fail('Invalid Specter File, unable to decode ' . $filename . '.');
     }
     $test = json_decode($actual);
     if ($test === null && json_last_error() !== JSON_ERROR_NONE) {
         self::fail('Invalid Specter File, unable to decode response.');
     }
     unset($test);
     // Build these both into arrays for processing
     $actual = json_decode($actual, true);
     $spec = json_decode($spec, true);
     // Convert the spec to matcher format
     $matcherSpec = self::getMatcherFormat($spec);
     $factory = new SimpleFactory();
     $matcher = $factory->createMatcher();
     // Pass this test because the matcher matched
     if ($matcher->match($actual, $matcherSpec)) {
         self::assertTrue(true);
         return;
     }
     // Display the output in a better format by using a diffing tool
     // We convert to strings to try to make the output more accurate
     $difference = $matcher->getError() . PHP_EOL;
     $specString = json_encode($spec, JSON_PRETTY_PRINT);
     $specString = explode(PHP_EOL, $specString);
     $actualString = json_encode($actual, JSON_PRETTY_PRINT);
     $actualString = explode(PHP_EOL, $actualString);
     $diffOptions = [];
     $diff = new Diff($specString, $actualString, $diffOptions);
     $renderer = new Diff_Renderer_Text_Unified();
     self::fail($difference . $diff->render($renderer), 'Incorrect api response');
 }
 /**
  * @Then the JSON response should match:
  */
 public function theJsonResponseShouldMatch(PyStringNode $string)
 {
     $factory = new SimpleFactory();
     $matcher = $factory->createMatcher();
     $match = $matcher->match($this->response->getBody()->getContents(), $string->getRaw());
     \PHPUnit_Framework_Assert::assertTrue($match, $matcher->getError());
 }
Beispiel #9
0
 public function setUp()
 {
     $factory = new SimpleFactory();
     $this->matcher = $factory->createMatcher();
 }
 public function test_creating_matcher()
 {
     $factory = new SimpleFactory();
     $this->assertInstanceOf('Coduo\\PHPMatcher\\Matcher', $factory->createMatcher());
 }
Beispiel #11
0
 /**
  * @param  mixed   $value
  * @param  mixed   $pattern
  * @return boolean
  */
 function match($value, $pattern)
 {
     $factory = new SimpleFactory();
     $matcher = $factory->createMatcher();
     return $matcher->match($value, $pattern);
 }
Beispiel #12
0
 /**
  * Checks that response body contains JSON from PyString.
  *
  * Do not check that the response body /only/ contains the JSON from PyString,
  *
  * @param PyStringNode $jsonString
  *
  * @throws \RuntimeException
  *
  * @Then /^(?:the )?response should contain json:$/
  */
 public function theResponseShouldContainJson(PyStringNode $jsonString)
 {
     $etalon = json_decode($this->replacePlaceHolder($jsonString->getRaw()), true);
     $actual = $this->response->json();
     if (null === $etalon) {
         throw new \RuntimeException("Can not convert etalon to json:\n" . $this->replacePlaceHolder($jsonString->getRaw()));
     }
     $factory = new SimpleFactory();
     $matcher = $factory->createMatcher();
     Assertions::assertTrue($matcher->match($actual, $etalon));
 }
 /**
  * @return Matcher
  */
 private function createMatcher()
 {
     $factory = new SimpleFactory();
     return $factory->createMatcher();
 }