diff --git a/src/TestUtils/TestTrait.php b/src/TestUtils/TestTrait.php index 8f870c1..e9a7763 100644 --- a/src/TestUtils/TestTrait.php +++ b/src/TestUtils/TestTrait.php @@ -57,17 +57,21 @@ private static function runSnippet($sampleName, $params = []) } $testFunc = function () use ($sampleFile, $params) { - return shell_exec(sprintf( - 'php %s %s', - $sampleFile, - implode(' ', array_map('escapeshellarg', $params)) - )); + $argv = array_merge([$sampleFile], $params); + $argc = count($argv); + try { + ob_start(); + require $sampleFile; + return ob_get_clean(); + } catch (\Exception $e) { + ob_get_clean(); + throw $e; + } }; if (isset(self::$backoff)) { return self::$backoff->execute($testFunc); } - return $testFunc(); } } diff --git a/test/TestUtils/TestTraitTest.php b/test/TestUtils/TestTraitTest.php index 861be9d..4ff7a8a 100644 --- a/test/TestUtils/TestTraitTest.php +++ b/test/TestUtils/TestTraitTest.php @@ -18,6 +18,7 @@ namespace Google\Cloud\TestUtils\test; use Google\Cloud\TestUtils\TestTrait; +use Google\Cloud\TestUtils\ExponentialBackoffTrait; /** * Class TestTraitTest @@ -28,8 +29,7 @@ class TestTraitTest extends \PHPUnit_Framework_TestCase { use TestTrait; - - private static $backoff; + use ExponentialBackoffTrait; public static function checkProjectEnvVarBeforeClass() { @@ -108,18 +108,41 @@ public function testRunSnippet() $output1 = shell_exec($snippet2Cmd . ' foo bar baz'); $output2 = $this->runSnippet($snippet2File, ['foo', 'bar', 'baz']); $this->assertEquals($output1, $output2); + } + + public function testRunSnippetWithBackoff() + { + $this->useBackoff($retries = 5, function () use (&$timesCalled) { + $timesCalled++; + return true; + }); + + self::setDelayFunction(function ($delay) { + // do nothing! + }); + + try { + $output = $this->runSnippet('snippet3'); + $this->fail('Should have thrown exception'); + } catch (\Exception $e) { + $this->assertEquals('This is expected', $e->getMessage()); + } - self::$backoff = new FakeBackoff(); + $this->assertEquals($retries + 1, $timesCalled); + } - $output = $this->runSnippet('foo'); - $this->assertEquals('FakeBackoff', $output); + /** + * @expectedException Exception + * @expectedExceptionMessage This is expected + */ + public function testRunSnippetWithException() + { + $this->runSnippet('snippet3'); } -} -class FakeBackoff -{ - public function execute($fn) + public function setUp() { - return "FakeBackoff"; + // Clear backoffs before running each test + self::$backoff = null; } } diff --git a/test/src/snippet3.php b/test/src/snippet3.php new file mode 100644 index 0000000..b7010a5 --- /dev/null +++ b/test/src/snippet3.php @@ -0,0 +1,3 @@ +