private static function recursiveSort(&$array) { foreach ($array as &$value) { if (is_array($value)) { YamlRunnerTest::recursiveSort($value); } } return sort($array); }
private function executeTestCase($test, $testFile) { $stash = array(); $response = array(); reset($test); $key = key($test); foreach ($test[$key] as $operators) { foreach ($operators as $operator => $settings) { echo " > {$operator}: "; ob_flush(); if ($operator === 'do') { if (key($settings) === 'catch') { $catch = $this->getValue($settings, 'catch'); $expectedError = str_replace("/", "", $catch); next($settings); echo "(catch: {$expectedError}) "; ob_flush(); } else { $expectedError = null; } $method = key($settings); $hash = $this->getValue($settings, $method); echo "\n |{$method}\n"; ob_flush(); $hash = YamlRunnerTest::replaceWithStash($hash, $stash); try { echo " |" . json_encode($hash) . "\n"; ob_flush(); $response = $this->callMethod($method, $hash); echo " |" . json_encode($response) . "\n"; ob_flush(); //$this->waitForYellow(); if (isset($expectedError) === true) { $this->fail("Expected Exception not thrown: {$expectedError}"); } } catch (Missing404Exception $exception) { if ($expectedError === 'missing') { $this->assertTrue(true); } else { $this->fail($exception->getMessage()); } $response = json_decode($exception->getMessage(), true); } catch (Conflict409Exception $exception) { if ($expectedError === 'conflict') { $this->assertTrue(true); } else { $this->fail($exception->getMessage()); } $response = json_decode($exception->getMessage(), true); } catch (Forbidden403Exception $exception) { if ($expectedError === 'forbidden') { $this->assertTrue(true); } else { $this->fail($exception->getMessage()); } $response = json_decode($exception->getMessage(), true); } catch (BadRequest400Exception $exception) { if ($expectedError === 'request') { $this->assertTrue(true); } elseif (isset($expectedError) === true && preg_match("/{$expectedError}/", $exception->getMessage()) === 1) { $this->assertTrue(true); } else { $this->fail($exception->getMessage()); } $response = json_decode($exception->getMessage(), true); } catch (ServerErrorResponseException $exception) { if ($expectedError === 'request') { $this->assertTrue(true); } elseif (isset($expectedError) === true && preg_match("/{$expectedError}/", $exception->getMessage()) === 1) { $this->assertTrue(true); } else { $this->fail($exception->getMessage()); } $response = json_decode($exception->getMessage(), true); } catch (Elasticsearch\Common\Exceptions\RuntimeException $exception) { if ($expectedError === 'param') { $this->assertTrue(true); } elseif (isset($expectedError) === true && preg_match("/{$expectedError}/", $exception->getMessage()) === 1) { $this->assertTrue(true); } else { $this->fail($exception->getMessage()); } $response = json_decode($exception->getMessage(), true); } catch (\Exception $exception) { if ($expectedError === null) { $this->fail($exception->getMessage()); } elseif (preg_match("/{$expectedError}/", $exception->getMessage()) === 1) { $this->assertTrue(true); } else { $this->fail($exception->getMessage()); } $response = json_decode($exception->getMessage(), true); } } elseif ($operator === 'match') { $expected = $this->getValue($settings, key($settings)); if (key($settings) === '') { $actual = $response; } else { if (key($settings) === '$body') { $actual = $response; } else { $actual = $this->getNestedVar($response, key($settings)); } } $expected = YamlRunnerTest::replaceWithStash($expected, $stash); $actual = YamlRunnerTest::replaceWithStash($actual, $stash); 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) { $this->assertRegex($expected, $actual); } else { $this->assertEquals($expected, $actual); } //$this->assertSame() echo "\n"; } elseif ($operator === "is_true") { if (empty($settings) === true) { $response = YamlRunnerTest::replaceWithStash($response, $stash); $this->assertTruthy($response, $settings); } else { $settings = YamlRunnerTest::replaceWithStash($settings, $stash); echo "settings after replace: "; print_r($settings); echo "\n"; $actual = $this->getNestedVar($response, $settings); $actual = YamlRunnerTest::replaceWithStash($actual, $stash); $this->assertTruthy($actual, $settings); } echo "\n"; } elseif ($operator === "is_false") { if (empty($settings) === true) { $response = YamlRunnerTest::replaceWithStash($response, $stash); $this->assertFalsey($response, $settings); } else { $actual = $this->getNestedVar($response, $settings); $actual = YamlRunnerTest::replaceWithStash($actual, $stash); $this->assertFalsey($actual, $settings); } echo "\n"; } elseif ($operator === 'set') { $stashKey = $this->getValue($settings, key($settings)); echo " {$stashKey}\n"; $stash["\${$stashKey}"] = $this->getNestedVar($response, key($settings)); echo "Stash updated. Total stash now: \n"; print_r($stash); echo "\n"; ob_flush(); } elseif ($operator === "length") { $expectedCount = $this->getValue($settings, key($settings)); $this->assertCount($expectedCount, $this->getNestedVar($response, key($settings))); echo "\n"; } elseif ($operator === "lt") { $expectedCount = $this->getValue($settings, key($settings)); $this->assertLessThan($expectedCount, $this->getNestedVar($response, key($settings))); echo "\n"; } elseif ($operator === "gt") { $expectedCount = $this->getValue($settings, key($settings)); $this->assertGreaterThan($expectedCount, $this->getNestedVar($response, key($settings))); echo "\n"; } elseif ($operator === "skip") { if (isset($settings['version']) === true) { $version = $settings['version']; $version = str_replace(" ", "", $version); $version = explode("-", $version); if (isset($version[0]) && $version[0] == 'all') { echo "Skipping: all\n"; ob_flush(); if ($key == 'setup') { throw new SetupSkipException(); } return; } if (!isset($version[0])) { $version[0] = ~PHP_INT_MAX; } if (!isset($version[1])) { $version[1] = PHP_INT_MAX; } if (version_compare(YamlRunnerTest::$esVersion, $version[0]) >= 0 && version_compare($version[1], YamlRunnerTest::$esVersion) >= 0) { echo "Skipping: " . $settings['reason'] . "\n"; if ($key == 'setup') { throw new SetupSkipException(); } return; } } else { if (isset($settings['features']) === true) { $feature = $settings['features']; $whitelist = array('gtelte'); if (array_search($feature, $whitelist) === false) { echo "Unsupported optional feature: {$feature}\n"; return; } } } } ob_flush(); } } }
private function handleCaughtException(\Exception $exception, $expectedError) { $reflect = new ReflectionClass($exception); $caught = $reflect->getShortName(); $passed = false; if ($caught === 'Missing404Exception' && $expectedError === 'missing') { $passed = true; } elseif ($caught === 'Conflict409Exception' && $expectedError === 'conflict') { $passed = true; } elseif ($caught === 'Missing404Exception' && $expectedError === 'missing') { $passed = true; } elseif ($caught === 'Forbidden403Exception' && $expectedError === 'forbidden') { $passed = true; } elseif ($caught === 'RequestTimeout408Exception' && $expectedError === 'request_timeout') { $passed = true; } elseif ($caught === 'BadRequest400Exception' && $expectedError === 'request') { $passed = true; } elseif ($caught === 'ServerErrorResponseException' && $expectedError === 'request') { $passed = true; } elseif ($caught === 'RuntimeException' && $expectedError === 'param') { $passed = true; } elseif ($caught === 'Missing404Exception' && $expectedError === 'missing') { $passed = true; } if ($passed === false) { if (YamlRunnerTest::checkExceptionRegex($expectedError, $exception)) { $passed = true; } elseif ($exception->getPrevious() !== null) { // try second level if (YamlRunnerTest::checkExceptionRegex($expectedError, $exception->getPrevious())) { $passed = true; } } } if ($passed === true) { $this->assertTrue(true); return json_decode($exception->getMessage(), true); } //$this->fail("Tried to match exception, failed. Exception: ".$exception->getMessage()); throw $exception; }