From 1f6822b27f044c696bca4e4c8c1f3818a07242a6 Mon Sep 17 00:00:00 2001 From: Zachary Tong Date: Fri, 7 Mar 2014 12:35:01 -0500 Subject: [PATCH] [TEST] Use forked version of Symfony/Yaml The forked Yaml parser supports map via the stdClass() object, which is a required feature for a number of yaml tests. This commit switches over to the forked parser until the point in time which Symfony devs decide to merge the PR. Commit also includes various changes to the YamlTestRunner to make it compatible with objects. --- composer.json | 10 +++- tests/Elasticsearch/Tests/YamlRunnerTest.php | 51 ++++++++++++++++---- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index 66362f702..7d58fad77 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "phpunit/phpunit": "3.7.*", "mockery/mockery": "dev-master@dev", "athletic/athletic": "~0.1", - "symfony/yaml": "2.4.*@dev", + "symfony/yaml": "2.4.3 as 2.4.2", "satooshi/php-coveralls": "dev-master", "mikey179/vfsStream": "~1.2", "twig/twig": "1.*", @@ -33,5 +33,11 @@ "Elasticsearch\\Tests" : "tests/", "Elasticsearch\\Benchmarks" : "benchmarks/" } - } + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/polyfractal/Yaml" + } + ] } diff --git a/tests/Elasticsearch/Tests/YamlRunnerTest.php b/tests/Elasticsearch/Tests/YamlRunnerTest.php index 3e2a6effc..9a744b48f 100644 --- a/tests/Elasticsearch/Tests/YamlRunnerTest.php +++ b/tests/Elasticsearch/Tests/YamlRunnerTest.php @@ -222,7 +222,8 @@ public function testYaml() try { $tDoc = array(); $tDoc['document'] = $this->checkForTimestamp($testFile, $document); - $tDoc['values'] = $this->yaml->parse($tDoc['document'], false, true, false); + $tDoc['document'] = $this->checkForEmptyProperty($testFile, $tDoc['document']); + $tDoc['values'] = $this->yaml->parse($tDoc['document'], false, false, true); if (key($tDoc['values']) === 'setup') { $setup = $tDoc['values']; @@ -295,7 +296,8 @@ private function executeTestCase($test, $testFile) if ($operator === 'do') { if (key($settings) === 'catch') { - $expectedError = str_replace("/", "", $settings['catch']); + $catch = $this->getValue($settings, 'catch'); + $expectedError = str_replace("/", "", $catch); next($settings); echo "(catch: $expectedError) "; @@ -305,7 +307,7 @@ private function executeTestCase($test, $testFile) } $method = key($settings); - $hash = $settings[$method]; + $hash = $this->getValue($settings, $method); echo "\n |$method\n"; ob_flush(); @@ -384,7 +386,7 @@ private function executeTestCase($test, $testFile) } elseif($operator === 'match') { - $expected = $settings[key($settings)]; + $expected = $this->getValue($settings, key($settings)); if (key($settings) === '') { $actual = $response; } else if (key($settings) === '$body') { @@ -396,8 +398,15 @@ private function executeTestCase($test, $testFile) $expected = $this->replaceWithStash($expected, $stash); $actual = $this->replaceWithStash($actual, $stash); - if (is_object($expected) === true) { - $expected = (array)$expected; + if ($actual != $expected) { + //Holy janky batman + if (is_array($actual) && count($actual) == 0) { + $actual = (object) $actual; + } else { + $actual = json_decode(json_encode($actual)); + } + + $expected = json_decode(json_encode($expected)); } if ($this->checkForRegex($expected) === true) { @@ -436,20 +445,24 @@ private function executeTestCase($test, $testFile) echo "\n"; } elseif ($operator === 'set') { - $stash['$'.$settings[key($settings)]] = $this->getNestedVar($response, key($settings)); + $stashKey = $this->getValue($settings, key($settings)); + $stash["$$stashKey"] = $this->getNestedVar($response, key($settings)); echo "\n"; } elseif ($operator === "length") { - $this->assertCount($settings[key($settings)], $this->getNestedVar($response, key($settings))); + $expectedCount = $this->getValue($settings, key($settings)); + $this->assertCount($expectedCount, $this->getNestedVar($response, key($settings))); echo "\n"; } elseif ($operator === "lt") { - $this->assertLessThan($settings[key($settings)], $this->getNestedVar($response, key($settings))); + $expectedCount = $this->getValue($settings, key($settings)); + $this->assertLessThan($expectedCount, $this->getNestedVar($response, key($settings))); echo "\n"; } elseif ($operator === "gt") { - $this->assertGreaterThan($settings[key($settings)], $this->getNestedVar($response, key($settings))); + $expectedCount = $this->getValue($settings, key($settings)); + $this->assertGreaterThan($expectedCount, $this->getNestedVar($response, key($settings))); echo "\n"; } elseif ($operator === "skip") { if (isset($settings['version']) === true) { @@ -498,6 +511,16 @@ private function callMethod($method, $hash) return $ret; } + private function getValue($a, $key) { + if (is_array($a)) { + return $a[$key]; + } elseif(is_object($a)) { + return $a->$key; + } else { + die('non-array, non-object in getValue()'); + } + } + private function snakeToCamel($val) { return str_replace(' ', '', lcfirst(ucwords(str_replace('_', ' ', $val)))); } @@ -538,6 +561,14 @@ private function checkForTimestamp($file, $document) } + private function checkForEmptyProperty($file, $document) { + $pattern = "/{.*?('').*?:.*?{/"; + + $document = preg_replace($pattern, '{ $body: {', $document); + + return $document; + } + private function checkForRegex($value) { if (is_string($value) !== true) { return false;