Skip to content

Commit f2acb43

Browse files
committed
Only show log when test fails
1 parent c914c1c commit f2acb43

File tree

1 file changed

+51
-59
lines changed

1 file changed

+51
-59
lines changed

tests/Elasticsearch/Tests/YamlRunnerTest.php

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class YamlRunnerTest extends \PHPUnit_Framework_TestCase
3737
/** @var Elasticsearch\client */
3838
private $client;
3939

40+
/** @var string */
41+
private $log = "";
42+
4043
/** @var string */
4144
public static $esVersion;
4245

@@ -55,9 +58,13 @@ public static function getHostEnvVar()
5558
}
5659
}
5760

61+
private function log($text)
62+
{
63+
$this->log .= $text;
64+
}
65+
5866
public static function setUpBeforeClass()
5967
{
60-
ob_implicit_flush();
6168
$host = YamlRunnerTest::getHostEnvVar();
6269
echo "Test Host: $host\n";
6370

@@ -85,11 +92,12 @@ public function setUp()
8592
//$params['logLevel'] = \Psr\Log\LogLevel::DEBUG;
8693

8794
$this->client = Elasticsearch\ClientBuilder::create()->setHosts($params['hosts'])->build();
95+
$this->log = "";
8896
}
8997

9098
private function clearCluster()
9199
{
92-
echo "\n>>>CLEARING<<<\n";
100+
$this->log("\n>>>CLEARING<<<\n");
93101
$host = YamlRunnerTest::getHostEnvVar();
94102
$ch = curl_init($host."/*");
95103
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@@ -119,19 +127,17 @@ private function clearCluster()
119127

120128
private function assertTruthy($value, $settings)
121129
{
122-
echo "\n |assertTruthy($settings): ".json_encode($value)."\n";
123-
ob_flush();
130+
$this->log("\n |assertTruthy($settings): ".json_encode($value)."\n");
124131
if (isset($value) === false || $value === 0 || $value === false || $value === null || $value === '') {
125-
$this->fail("Value is not truthy: ".print_r($value, true));
132+
$this->fail("Value is not truthy: ".print_r($value, true) . "\n" . $this->log);
126133
}
127134
}
128135

129136
private function assertFalsey($value, $settings)
130137
{
131-
echo "\n |assertFalsey($settings): ".json_encode($value)."\n";
132-
ob_flush();
138+
$this->log("\n |assertFalsey($settings): ".json_encode($value)."\n");
133139
if (!(isset($value) === false || $value === 0 || $value === false || $value === null || $value === '' || count($value) === 0)) {
134-
$this->fail("Value is not falsey: ".print_r($value, true));
140+
$this->fail("Value is not falsey: ".print_r($value, true) . "\n" . $this->log);
135141
}
136142
}
137143

@@ -143,11 +149,10 @@ private function assertRegex($pattern, $actual)
143149
$pattern = substr($pattern, 1, strlen($pattern)-2);
144150
$pattern = str_replace('/', '\/', $pattern);
145151
$pattern = "/$pattern/mx";
146-
echo "\n |> actual: $actual\n";
147-
echo "\n |> pattern: $pattern\n";
148-
ob_flush();
152+
$this->log("\n |> actual: $actual\n");
153+
$this->log("\n |> pattern: $pattern\n");
149154
$result = preg_match($pattern, $actual, $matches);
150-
$this->assertEquals(1, $result);
155+
$this->assertEquals(1, $result, $this->log);
151156
}
152157

153158
private function waitForYellow()
@@ -167,8 +172,8 @@ private function waitForYellow()
167172
++$counter;
168173

169174
if ($counter > 10) {
170-
echo "Aborting test due to failure in clearing cluster.\n";
171-
print_r($response);
175+
$this->log("Aborting test due to failure in clearing cluster.\n");
176+
$this->log(print_r($response, true));
172177
exit;
173178
}
174179
}
@@ -226,9 +231,8 @@ public function testYaml()
226231
foreach ($files as $testFile) {
227232
$counter = YamlRunnerTest::$testCounter;
228233

229-
echo "--------------------------------------------------------------------------\n";
230-
echo "#$counter : $testFile\n";
231-
ob_flush();
234+
$this->log("--------------------------------------------------------------------------\n");
235+
$this->log("#$counter : $testFile\n");
232236
YamlRunnerTest::$testCounter += 1;
233237

234238
if ($this->skipTest($testFile) === true) {
@@ -267,8 +271,7 @@ public function testYaml()
267271

268272
foreach ($yamlDocs as $doc) {
269273
$ts = date('c');
270-
echo " ".key($doc['values'])." [$ts] - Future: false\n";
271-
ob_flush();
274+
$this->log(" ".key($doc['values'])." [$ts] - Future: false\n");
272275

273276
$this->clearCluster();
274277

@@ -281,8 +284,7 @@ public function testYaml()
281284
}
282285
$this->executeTestCase($doc['values'], $testFile, false);
283286

284-
echo "Success\n\n";
285-
ob_flush();
287+
$this->log("Success\n\n");
286288
}
287289
}
288290
}
@@ -298,8 +300,7 @@ public function testFutureModeYaml()
298300
$files = func_get_args();
299301

300302
foreach ($files as $testFile) {
301-
echo "$testFile\n";
302-
ob_flush();
303+
$this->log("$testFile\n");
303304

304305
if ($this->skipTest($testFile) === true) {
305306
$this->markTestSkipped('Skipped due to skip-list');
@@ -337,14 +338,12 @@ public function testFutureModeYaml()
337338

338339
foreach ($yamlDocs as $doc) {
339340
$ts = date('c');
340-
echo " ".key($doc['values'])." [$ts] - Future: true\n";
341+
$this->log(" ".key($doc['values'])." [$ts] - Future: true\n");
341342

342343
if ($containsExist !== false) {
343344
$this->markTestSkipped('Test contains `exist`, not easily tested in async. Skipping.');
344345
}
345346

346-
ob_flush();
347-
348347
$this->clearCluster();
349348

350349
if ($setup !== null) {
@@ -407,36 +406,32 @@ private function executeTestCase($test, $testFile, $future)
407406

408407
foreach ($test[$key] as $operators) {
409408
foreach ($operators as $operator => $settings) {
410-
echo " > $operator: ";
411-
ob_flush();
409+
$this->log(" > $operator: ");
412410
if ($operator === 'do') {
413411
if (key($settings) === 'catch') {
414412
$catch = $this->getValue($settings, 'catch');
415413
$expectedError = str_replace("/", "", $catch);
416414
next($settings);
417415

418-
echo "(catch: $expectedError) ";
419-
ob_flush();
416+
$this->log("(catch: $expectedError) ");
420417
} else {
421418
$expectedError = null;
422419
}
423420

424421
$method = key($settings);
425422
$hash = $this->getValue($settings, $method);
426423

427-
echo "\n |$method\n";
428-
ob_flush();
424+
$this->log("\n |$method\n");
429425

430426

431427
$hash = YamlRunnerTest::replaceWithStash($hash, $stash);
432428

433429

434430
try {
435-
echo " |".json_encode($hash)."\n";
431+
$this->log(" |".json_encode($hash)."\n");
436432
ob_flush();
437433
$response = $this->callMethod($method, $hash, $future);
438-
echo " |".json_encode($response)."\n";
439-
ob_flush();
434+
$this->log(" |".json_encode($response)."\n");
440435

441436
//$this->waitForYellow();
442437

@@ -477,27 +472,27 @@ private function executeTestCase($test, $testFile, $future)
477472
if ($this->checkForRegex($expected) === true) {
478473
$this->assertRegex($expected, $actual);
479474
} else {
480-
$this->assertEquals($expected, $actual);
475+
$this->assertEquals($expected, $actual, $this->log);
481476
}
482477

483478
//$this->assertSame()
484479

485-
echo "\n";
480+
$this->log("\n");
486481
} elseif ($operator === "is_true") {
487482
if (empty($settings) === true) {
488483
$response = YamlRunnerTest::replaceWithStash($response, $stash);
489484
$this->assertTruthy($response, $settings);
490485
} else {
491486
$settings = YamlRunnerTest::replaceWithStash($settings, $stash);
492-
echo "settings after replace: ";
487+
$this->log("settings after replace: ");
493488
//print_r($settings);
494-
echo "\n";
489+
$this->log("\n");
495490
$actual = $this->getNestedVar($response, $settings);
496491
$actual = YamlRunnerTest::replaceWithStash($actual, $stash);
497492
$this->assertTruthy($actual, $settings);
498493
}
499494

500-
echo "\n";
495+
$this->log("\n");
501496
} elseif ($operator === "is_false") {
502497
if (empty($settings) === true) {
503498
$response = YamlRunnerTest::replaceWithStash($response, $stash);
@@ -508,36 +503,34 @@ private function executeTestCase($test, $testFile, $future)
508503
$this->assertFalsey($actual, $settings);
509504
}
510505

511-
echo "\n";
506+
$this->log("\n");
512507
} elseif ($operator === 'set') {
513508
$stashKey = $this->getValue($settings, key($settings));
514-
echo " $stashKey\n";
509+
$this->log(" $stashKey\n");
515510
$stash["$$stashKey"] = $this->getNestedVar($response, key($settings));
516-
echo "Stash updated. Total stash now: \n";
511+
$this->log("Stash updated. Total stash now: \n");
517512
//print_r($stash);
518-
echo "\n";
519-
ob_flush();
513+
$this->log("\n");
520514
} elseif ($operator === "length") {
521515
$expectedCount = $this->getValue($settings, key($settings));
522-
$this->assertCount($expectedCount, $this->getNestedVar($response, key($settings)));
523-
echo "\n";
516+
$this->assertCount($expectedCount, $this->getNestedVar($response, key($settings)), $this->log);
517+
$this->log("\n");
524518
} elseif ($operator === "lt") {
525519
$expectedCount = $this->getValue($settings, key($settings));
526-
$this->assertLessThan($expectedCount, $this->getNestedVar($response, key($settings)));
527-
echo "\n";
520+
$this->assertLessThan($expectedCount, $this->getNestedVar($response, key($settings)), $this->log);
521+
$this->log("\n");
528522
} elseif ($operator === "gt") {
529523
$expectedCount = $this->getValue($settings, key($settings));
530-
$this->assertGreaterThan($expectedCount, $this->getNestedVar($response, key($settings)));
531-
echo "\n";
524+
$this->assertGreaterThan($expectedCount, $this->getNestedVar($response, key($settings)), $this->log);
525+
$this->log("\n");
532526
} elseif ($operator === "skip") {
533527
if (isset($settings['version']) === true) {
534528
$version = $settings['version'];
535529
$version = str_replace(" ", "", $version);
536530
$version = explode("-", $version);
537531

538532
if (isset($version[0]) && $version[0] == 'all') {
539-
echo "Skipping: all\n";
540-
ob_flush();
533+
$this->log("Skipping: all\n");
541534
if ($key == 'setup') {
542535
throw new SetupSkipException();
543536
}
@@ -551,7 +544,7 @@ private function executeTestCase($test, $testFile, $future)
551544
}
552545
if (version_compare(YamlRunnerTest::$esVersion, $version[0]) >= 0
553546
&& version_compare($version[1], YamlRunnerTest::$esVersion) >= 0) {
554-
echo "Skipping: ".$settings['reason']."\n";
547+
$this->log("Skipping: ".$settings['reason']."\n");
555548

556549
if ($key == 'setup') {
557550
throw new SetupSkipException();
@@ -564,13 +557,12 @@ private function executeTestCase($test, $testFile, $future)
564557
$whitelist = array();
565558

566559
if (array_search($feature, $whitelist) === false) {
567-
echo "Unsupported optional feature: $feature\n";
560+
$this->log("Unsupported optional feature: " . print_r($feature, true) . "\n");
568561

569562
return;
570563
}
571564
}
572565
}
573-
ob_flush();
574566
}
575567
}
576568
}
@@ -613,7 +605,7 @@ private function handleCaughtException(\Exception $exception, $expectedError)
613605
}
614606

615607
if ($passed === true) {
616-
$this->assertTrue(true);
608+
$this->assertTrue(true, $this->log);
617609
if ($exception->getPrevious() !== null) {
618610
return json_decode($exception->getPrevious()->getMessage(), true);
619611
}
@@ -689,8 +681,8 @@ private function getNestedVar(&$context, $name)
689681
$piece = str_replace('\.', '.', $piece);
690682
if (!is_array($context) || !array_key_exists($piece, $context)) {
691683
// error occurred
692-
echo "Could not find nested property [$piece] in context";//.print_r($context, true);
693-
echo "\nReturning null...";
684+
$this->log("Could not find nested property [$piece] in context");//.print_r($context, true);
685+
$this->log("\nReturning null...");
694686
return null;
695687
}
696688
$context = &$context[$piece];

0 commit comments

Comments
 (0)