Skip to content

Commit

Permalink
[TEST] Use forked version of Symfony/Yaml
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
polyfractal committed Mar 7, 2014
1 parent 374bcfc commit 1f6822b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand All @@ -33,5 +33,11 @@
"Elasticsearch\\Tests" : "tests/",
"Elasticsearch\\Benchmarks" : "benchmarks/"
}
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/polyfractal/Yaml"
}
]
}
51 changes: 41 additions & 10 deletions tests/Elasticsearch/Tests/YamlRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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) ";
Expand All @@ -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();
Expand Down Expand Up @@ -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') {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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))));
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1f6822b

Please sign in to comment.