/** * Read a suite path on the environment's read stream and execute * them against a standard Peridot runner. * * @param Context $context * @param Environment $environment * @param Message $message * @return void */ public function loop(Context $context, Environment $environment, Message $message) { while (true) { $input = fgets($environment->getReadStream()); list($token, $path) = $this->getTestInfo($input); $context->setFile($path); putenv("PERIDOT_TEST_TOKEN={$token}"); require $path; $runner = new Runner($context->getCurrentSuite(), $environment->getConfiguration(), $environment->getEventEmitter()); $runner->run(new TestResult($environment->getEventEmitter())); $message->end(); $context->clear(); } }
/** * Run the application. * * @param TestMessage $message * @return void */ public function run(TestMessage $message) { $this->environment->load($this->reader); $this->message = $message; $this->listen($this->environment->getEventEmitter()); $context = Context::getInstance(); $this->looper->loop($context, $this->environment, $message); }
/** * Initialize reporter. Setup and listen for runner events * * @return void */ public function init() { $this->root = Context::getInstance()->getCurrentSuite(); $this->eventEmitter->on('runner.start', [$this, 'onRunnerStart']); $this->eventEmitter->on('suite.start', [$this, 'onSuiteStart']); $this->eventEmitter->on('suite.end', [$this, 'onSuiteEnd']); $this->eventEmitter->on('test.passed', [$this, 'onTestPassed']); $this->eventEmitter->on('test.failed', [$this, 'onTestFailed']); $this->eventEmitter->on('test.pending', [$this, 'onTestPending']); $this->eventEmitter->on('runner.end', [$this, 'onRunnerEnd']); }
/** * Get the RunnerInterface being used by the Peridot application. * If one is not set, a default Runner will be used. * * @return RunnerInterface */ public function getRunner() { if ($this->runner === null) { $this->runner = new Runner(Context::getInstance()->getCurrentSuite(), $this->getConfiguration(), $this->environment->getEventEmitter()); } return $this->runner; }
/** * @return LumenAppScope */ function lumen() { return Context::getInstance()->getCurrentSuite()->getScope()->lumen; }
/** * When the runner starts we will mix in the http kernel scope into the root suite, * thereby making it available EVERYWHERE. */ public function onRunnerStart() { $rootSuite = Context::getInstance()->getCurrentSuite(); $rootSuite->getScope()->peridotAddChildScope($this->scope); }
/** * Initialize the Context with the same event emitter as the Environment. * * @param EventEmitterInterface $emitter */ protected function initializeContext(EventEmitterInterface $emitter) { Context::getInstance()->setEventEmitter($emitter); }
<?php use Evenement\EventEmitter; use Peridot\Plugin\HttpKernel\HttpKernelPlugin; use Peridot\Plugin\HttpKernel\HttpKernelScope; use Peridot\Runner\Context; use Silex\Application; use Symfony\Component\HttpKernel\Client; describe('HttpKernelPlugin', function () { beforeEach(function () { $this->application = (include __DIR__ . '/../app/app.php'); $this->emitter = new EventEmitter(); $this->plugin = new HttpKernelPlugin($this->emitter, $this->application); }); describe('->onRunnerStart()', function () { it('should mix the scope in to the root suite', function () { $this->plugin->onRunnerStart(); $root = Context::getInstance()->getCurrentSuite(); $scope = $root->getScope(); assert($scope->client instanceof Client, 'root suite $scope->client should be Client'); }); }); describe('->getScope()', function () { it('should return the plugin scope', function () { $scope = $this->plugin->getScope(); assert($scope instanceof HttpKernelScope, "scope should be instance of HttpKernelScope"); }); }); });
$this->context->addSetupFunction($before); $result = call_user_func($this->context->getCurrentSuite()->getSetupFunctions()[0]); assert("result" === $result, "expected addSetupFunction to register setup function"); }); }); describe('->addTearDownFunction()', function () { it('should register an afterEach callback on the current suite', function () { $after = function () { return "result"; }; $this->context->addTearDownFunction($after); $result = call_user_func($this->context->getCurrentSuite()->getTearDownFunctions()[0]); assert("result" === $result, "expected addSetupFunction to register tear down function"); }); }); describe('::getInstance()', function () { beforeEach(function () { $this->property = $this->reflection->getProperty('instance'); $this->property->setAccessible(true); $this->previous = $this->property->getValue(); $this->property->setValue(null); }); afterEach(function () { $this->property->setValue($this->previous); }); it("should return a singleton instance of Context", function () { $context = Context::getInstance(); assert($context instanceof Context, "getInstance should return a Context"); }); }); });
/** * Add a tear down function for all specs in the * current suite * * @param callable $fn */ function afterEach(callable $fn) { Context::getInstance()->addTearDownFunction($fn); }
<?php use Peridot\Runner\Context; use Peridot\Runner\SuiteLoader; describe("SuiteLoader", function () { beforeEach(function () { $this->loader = new SuiteLoader('*.spec.php'); $this->fixtures = __DIR__ . '/../fixtures'; $this->context = Context::getInstance(); $this->file = $this->context->getFile(); }); afterEach(function () { $this->context->setFile($this->file); }); describe('->getTests()', function () { it("should return file paths matching *.spec.php recursively", function () { $tests = $this->loader->getTests($this->fixtures); assert(count($tests) == 4, "suite loader should have loaded 4 specs"); }); it("should return single file if it exists", function () { $test = $this->loader->getTests($this->fixtures . '/test.spec.php'); assert(count($test) == 1, "suite loader should load 1 spec"); }); it("should throw exception if path not found", function () { $exception = null; try { $this->loader->getTests('nope'); } catch (Exception $e) { $exception = $e; } assert(!is_null($exception), "loader should have thrown exception");