Example #1
0
<?php

use Peridot\Core\Exception;
describe('Exception', function () {
    describe('->getTraceString()', function () {
        it('returns a manually set trace string', function () {
            $exception = new Exception('message');
            $exception->setTraceString('this is a trace');
            assert($exception->getTraceString() === 'this is a trace');
        });
        it('uses ->getTraceAsString if nothing manually set', function () {
            $e = null;
            try {
                throw new Exception('message');
            } catch (Exception $ex) {
                $e = $ex;
            }
            assert($e->getTraceString() === $e->getTraceAsString());
        });
        it('prefers the manually set trace string', function () {
            $e = null;
            try {
                throw new Exception('message');
            } catch (Exception $ex) {
                $e = $ex;
            }
            $e->setTraceString('trace');
            assert($e->getTraceString() === 'trace');
        });
    });
    describe('type accessors', function () {
        });
        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');
        });
    });
});