<?php use Peridot\EventEmitter; use Peridot\Core\Context; use Peridot\Core\Suite; use Peridot\Reporter\AnonymousReporter; use Peridot\Reporter\ReporterInterface; use Peridot\Runner\Runner; describe('AnonymousReporter', function () { beforeEach(function () { $this->eventEmitter = new EventEmitter(); $this->output = new Symfony\Component\Console\Output\NullOutput(); $this->context = Context::getInstance(); }); it('should call the init function passed in', function () { $output = null; $emitter = null; new AnonymousReporter(function (ReporterInterface $reporter) use(&$output, &$emitter) { $output = $reporter->getOutput(); $emitter = $reporter->getEventEmitter(); }, $this->output, $this->eventEmitter, $this->context); assert(!is_null($output) && !is_null($emitter), 'output, and emitter should not be null'); }); });
/** * Initialize the Context with the same event emitter as the Environment. * * @param EventEmitterInterface $emitter */ protected function initializeContext(EventEmitterInterface $emitter) { $this->context = Context::getInstance(); $this->context->setEventEmitter($emitter); }
<?php use Peridot\EventEmitter; use Peridot\Core\Suite; use Peridot\Core\Context; use Peridot\Reporter\AnonymousReporter; use Peridot\Reporter\ReporterFactory; use Peridot\Reporter\SpecReporter; use Peridot\Runner\Runner; describe('ReporterFactory', function () { beforeEach(function () { $output = new Symfony\Component\Console\Output\NullOutput(); $this->factory = new ReporterFactory($output, new EventEmitter(), Context::getInstance()); }); describe('->create()', function () { context("using a valid reporter name", function () { it("should return an instance of the named reporter", function () { $reporter = $this->factory->create('spec'); assert($reporter instanceof SpecReporter, "should create SpecReporter"); }); it("should return an anonymous reporter if callable used", function () { $this->factory->register('spec2', 'desc', function ($reporter) { }); $reporter = $this->factory->create('spec2'); assert($reporter instanceof AnonymousReporter, "should create AnonymousReporter"); }); }); context("using a valid name with an invalid factory", function () { it("should throw an exception", function () { $this->factory->register('nope', 'doesnt work', 'Not\\A\\Class'); $exception = null;
}); it('should output time', function () { $time = PHP_Timer::secondsToTimeString($this->reporter->getTime()); assert(strstr($this->contents, $time) !== false, 'should contain time text'); }); it('should output failure text', function () { assert(strstr($this->contents, '1 failing') !== false, 'should contain failure text'); }); it('should output pending count', function () { assert(strstr($this->contents, '1 pending') !== false, 'should contain pending text'); }); it('should display exception stacks and messages', function () { $expectedExceptionMessage = " ooops" . PHP_EOL . " nextline"; assert(strstr($this->contents, $expectedExceptionMessage) !== false, "should include exception message"); $trace = preg_replace('/^#/m', " #", $this->exception->getTraceAsString()); assert(strstr($this->contents, $trace) !== false, "should include exception stack"); }); it('should honor peridot exception traces', function () { $output = new BufferedOutput(); $emitter = new EventEmitter(); $reporter = new SpecReporter($output, $emitter, Context::getInstance()); $exception = new PeridotException('message'); $exception->setTraceString('trace!!'); $emitter->emit('test.failed', new Test('failing test', function () { }), $exception); $reporter->footer(); $contents = $output->fetch(); assert(strstr($contents, 'trace!!') !== false, 'should contain manually set trace'); }); }); });
$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); }