123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- <?php
- /*
- * This file is part of the Lime framework.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- /**
- * Unit test library.
- *
- * @package lime
- * @author Fabien Potencier <fabien.potencier@gmail.com>
- * @version SVN: $Id: LimeTest.php 23880 2009-11-14 10:14:34Z bschussek $
- */
- class LimeTest
- {
- protected
- $output = null,
- $options = array(),
- $errorReporting = true,
- $exception = null,
- $exceptionExpectation = null;
- public function __construct($plan = null, array $options = array())
- {
- $this->options = array(
- 'base_dir' => null,
- 'output' => 'tap',
- 'force_colors' => false,
- 'verbose' => false,
- 'serialize' => false,
- 'coverage' => false,
- );
- foreach (LimeShell::parseArguments($GLOBALS['argv']) as $argument => $value)
- {
- $this->options[str_replace('-', '_', $argument)] = $value;
- }
- $this->options = array_merge($this->options, $options);
- $this->options['base_dir'] = realpath($this->options['base_dir']);
- list ($file, $line) = LimeTrace::findCaller('LimeTest');
- if ($this->options['coverage'])
- {
- $this->output = new LimeOutputCoverage();
- }
- elseif (is_string($this->options['output']))
- {
- $factory = new LimeOutputFactory($this->options);
- $this->output = $factory->create($this->options['output']);
- }
- else
- {
- $this->output = $this->options['output'];
- }
- $this->output->focus($file);
- if (!is_null($plan))
- {
- $this->output->plan($plan);
- }
- set_error_handler(array($this, 'handleError'));
- // make sure that exceptions that are not caught by the test runner are
- // caught and formatted in an appropriate way
- set_exception_handler(array($this, 'handleException'));
- }
- public function setErrorReporting($enabled)
- {
- $this->errorReporting = $enabled;
- }
- public function __destruct()
- {
- $this->output->close();
- $this->output->flush();
- restore_error_handler();
- restore_exception_handler();
- }
- public function getOutput()
- {
- return $this->output;
- }
- private function test(LimeConstraintInterface $constraint, $value, $message)
- {
- try
- {
- $constraint->evaluate($value);
- return $this->pass($message);
- }
- catch (LimeConstraintException $e)
- {
- return $this->fail($message, $e->getMessage());
- }
- }
- /**
- * Tests a condition and passes if it is true
- *
- * @param mixed $exp condition to test
- * @param string $message display output message when the test passes
- *
- * @return boolean
- */
- public function ok($exp, $message = '')
- {
- if ((boolean)$exp)
- {
- return $this->pass($message);
- }
- else
- {
- return $this->fail($message);
- }
- }
- /**
- * Compares two values and passes if they are equal (==)
- *
- * @param mixed $exp1 left value
- * @param mixed $exp2 right value
- * @param string $message display output message when the test passes
- *
- * @return boolean
- */
- public function is($exp1, $exp2, $message = '')
- {
- return $this->test(new LimeConstraintIs($exp2), $exp1, $message);
- }
- /**
- * Compares two values and passes if they are identical (===)
- *
- * @param mixed $exp1 left value
- * @param mixed $exp2 right value
- * @param string $message display output message when the test passes
- *
- * @return boolean
- */
- public function same($exp1, $exp2, $message = '')
- {
- return $this->test(new LimeConstraintSame($exp2), $exp1, $message);
- }
- /**
- * Compares two values and passes if they are not equal
- *
- * @param mixed $exp1 left value
- * @param mixed $exp2 right value
- * @param string $message display output message when the test passes
- *
- * @return boolean
- */
- public function isnt($exp1, $exp2, $message = '')
- {
- return $this->test(new LimeConstraintIsNot($exp2), $exp1, $message);
- }
- /**
- * Compares two values and passes if they are not identical (!==)
- *
- * @param mixed $exp1 left value
- * @param mixed $exp2 right value
- * @param string $message display output message when the test passes
- *
- * @return boolean
- */
- public function isntSame($exp1, $exp2, $message = '')
- {
- return $this->test(new LimeConstraintNotSame($exp2), $exp1, $message);
- }
- /**
- * Tests a string against a regular expression
- *
- * @param string $exp value to test
- * @param string $regex the pattern to search for, as a string
- * @param string $message display output message when the test passes
- *
- * @return boolean
- */
- public function like($exp1, $exp2, $message = '')
- {
- return $this->test(new LimeConstraintLike($exp2), $exp1, $message);
- }
- /**
- * Checks that a string doesn't match a regular expression
- *
- * @param string $exp value to test
- * @param string $regex the pattern to search for, as a string
- * @param string $message display output message when the test passes
- *
- * @return boolean
- */
- public function unlike($exp1, $exp2, $message = '')
- {
- return $this->test(new LimeConstraintUnlike($exp2), $exp1, $message);
- }
- public function greaterThan($exp1, $exp2, $message = '')
- {
- return $this->test(new LimeConstraintGreaterThan($exp2), $exp1, $message);
- }
- public function greaterThanEqual($exp1, $exp2, $message = '')
- {
- return $this->test(new LimeConstraintGreaterThanEqual($exp2), $exp1, $message);
- }
- public function lessThan($exp1, $exp2, $message = '')
- {
- return $this->test(new LimeConstraintLessThan($exp2), $exp1, $message);
- }
- public function lessThanEqual($exp1, $exp2, $message = '')
- {
- return $this->test(new LimeConstraintLessThanEqual($exp2), $exp1, $message);
- }
- public function contains($exp1, $exp2, $message = '')
- {
- return $this->test(new LimeConstraintContains($exp2), $exp1, $message);
- }
- public function containsNot($exp1, $exp2, $message = '')
- {
- return $this->test(new LimeConstraintContainsNot($exp2), $exp1, $message);
- }
- /**
- * Always passes--useful for testing exceptions
- *
- * @param string $message display output message
- *
- * @return true
- */
- public function pass($message = '')
- {
- list ($file, $line) = LimeTrace::findCaller('LimeTest');
- $this->output->pass($message, $file, $line);
- return true;
- }
- /**
- * Always fails--useful for testing exceptions
- *
- * @param string $message display output message
- *
- * @return false
- */
- public function fail($message = '', $error = null)
- {
- list ($file, $line) = LimeTrace::findCaller('LimeTest');
- $this->output->fail($message, $file, $line, $error);
- return false;
- }
- /**
- * Outputs a diag message but runs no test
- *
- * @param string $message display output message
- *
- * @return void
- */
- public function diag($message)
- {
- $this->output->comment($message);
- }
- /**
- * Counts as $nbTests tests--useful for conditional tests
- *
- * @param string $message display output message
- * @param integer $nbTests number of tests to skip
- *
- * @return void
- */
- public function skip($message = '', $nbTests = 1)
- {
- list ($file, $line) = LimeTrace::findCaller('LimeTest');
- for ($i = 0; $i < $nbTests; $i++)
- {
- $this->output->skip($message, $file, $line);
- }
- }
- /**
- * Counts as a test--useful for tests yet to be written
- *
- * @param string $message display output message
- *
- * @return void
- */
- public function todo($message = '')
- {
- list ($file, $line) = LimeTrace::findCaller('LimeTest');
- $this->output->todo($message, $file, $line);
- }
- public function comment($message)
- {
- $this->output->comment($message);
- }
- public function mock($class, array $options = array())
- {
- return LimeMock::create($class, $this->output, $options);
- }
- public function stub($class, array $options = array())
- {
- $options = array_merge(array(
- 'nice' => true,
- 'no_exceptions' => true,
- ), $options);
- return LimeMock::create($class, new LimeOutputNone(), $options);
- }
- public function extendMock($class, array $options = array())
- {
- $options['stub_methods'] = false;
- return $this->mock($class, $options);
- }
- public function extendStub($class, array $options = array())
- {
- $options['stub_methods'] = false;
- return $this->stub($class, $options);
- }
- public function expect($exception, $code = null)
- {
- list ($file, $line) = LimeTrace::findCaller('LimeTest');
- $this->exceptionExpectation = new LimeExceptionExpectation($exception, $file, $line);
- $this->exception = null;
- }
- public function handleError($code, $message, $file, $line, $context)
- {
- if (!$this->errorReporting || ($code & error_reporting()) == 0)
- {
- return false;
- }
- switch ($code)
- {
- case E_WARNING:
- $message = 'Warning: '.$message;
- break;
- case E_NOTICE:
- $message = 'Notice: '.$message;
- break;
- }
- $this->output->warning($message, $file, $line);
- }
- public function handleException(Exception $exception)
- {
- if (!is_null($this->exceptionExpectation))
- {
- $this->exception = $exception;
- }
- else
- {
- $this->output->error(LimeError::fromException($exception));
- }
- return true;
- }
- public function verifyException()
- {
- if (!is_null($this->exceptionExpectation))
- {
- $expected = $this->exceptionExpectation->getException();
- $file = $this->exceptionExpectation->getFile();
- $line = $this->exceptionExpectation->getLine();
- if (is_string($expected))
- {
- $actual = is_object($this->exception) ? get_class($this->exception) : 'none';
- $message = sprintf('A "%s" was thrown', $expected);
- }
- else
- {
- $actual = $this->exception;
- $message = sprintf('A "%s" was thrown', get_class($expected));
- }
- // can't use ->is() here because the custom file and line need to be
- // passed to the output
- try
- {
- $constraint = new LimeConstraintIs($expected);
- $constraint->evaluate($actual);
- $this->output->pass($message, $file, $line);
- }
- catch (LimeConstraintException $e)
- {
- $this->output->fail($message, $file, $line, $e->getMessage());
- }
- }
- $this->exceptionExpectation = null;
- }
- }
|