/src/Skajdo/TestSuite/Assert.php
PHP | 567 lines | 365 code | 51 blank | 151 comment | 46 complexity | 297b8eb74d0ec8a0dcd2f80d6a5750cf MD5 | raw file
- <?php
- namespace Skajdo\TestSuite;
- use \Skajdo\TestSuite\Exception\TestFailed;
- use \Skajdo\TestSuite\Exception\TestSkipped;
- use \Enhance\TextFactory;
- /**
- * Assertion object
- * Perform checks for given objects.
- */
- class Assert
- {
- /**
- * @var Assert
- */
- protected static $instance;
- /**
- * @var \Enhance\TextFactory
- */
- protected $text;
- /**
- * @return Assert
- */
- public static function getInstance()
- {
- if (!self::$instance) {
- self::$instance = new self();
- }
- return self::$instance;
- }
- /**
- * @return \Skajdo\TestSuite\Assert
- */
- protected function __construct()
- {
- $this->text = TextFactory::getLanguageText('En');
- }
- /**
- * Tell if given entities are identical.
- *
- * Objects must reference the same class instance to be identical.
- * Multidimensional arrays are handled properly.
- *
- * @param $expected
- * @param $actual
- * @throws TestFailed
- */
- public function isIdentical($expected, $actual)
- {
- if (is_float($expected)) {
- $expected = (string) $expected;
- $actual = (string) $actual;
- }
- if ($expected !== $actual) {
- self::testFailed(array(Message::IS_NOT_IDENTICAL, $actual, $expected));
- }
- }
- /**
- * Tell if current entities are NOT identical.
- *
- * Objects are considered not identical event if they are different instanced of the same class.
- * Multidimensional arrays are handled properly.
- *
- * @param $expected
- * @param $actual
- * @throws TestFailed
- */
- public function isNotIdentical($expected, $actual)
- {
- if (is_float($expected)) {
- $expected = (string) $expected;
- $actual = (string) $actual;
- }
- if ($expected === $actual) {
- self::testFailed(Message::IS_IDENTICAL, array($actual, $expected));
- }
- }
- /**
- * @deprecated use Assert::isIdentical()
- * @param $expected
- * @param $actual
- * @throws TestFailed
- */
- public function areIdentical($expected, $actual)
- {
- $this->isIdentical($expected, $actual);
- }
- /**
- * @deprecated use Assert::isNotIdentical()
- * @param $expected
- * @param $actual
- * @throws TestFailed
- */
- public function areNotIdentical($expected, $actual)
- {
- $this->isNotIdentical($expected, $actual);
- }
- /**
- * Tell if current entitiy equals boolean TRUE
- *
- * @param $actual
- * @throws TestFailed
- */
- public function isTrue($actual)
- {
- if ($actual !== true) {
- $this->testFailed(Message::EXPECTED_VALUE, array(true, $actual));
- }
- }
- /**
- * Tell if current entity is not equal to boolean TRUE
- *
- * @param $actual
- */
- public function isNotTrue($actual)
- {
- if ($actual === true) {
- $this->testFailed(Message::NOT_EXPECTED_VALUE, array(true));
- }
- }
- /**
- * Tell if current entitiy equals boolean FALSE
- *
- * @param $actual
- * @throws TestFailed
- */
- public function isFalse($actual)
- {
- if ($actual !== false) {
- $this->testFailed(Message::EXPECTED_VALUE, array(false, $actual));
- }
- }
- /**
- * Tell if given string ($actual) contains another string ($expected).
- *
- * @param $neddle
- * @param $haystack
- * @param bool $caseSensitive
- * @param string $encoding
- * @throws TestFailed
- */
- public function contains($neddle, $haystack, $caseSensitive = true, $encoding = 'utf8')
- {
- if ($caseSensitive !== true) {
- $neddle = mb_strtolower($neddle, $encoding);
- $haystack = mb_strtolower($haystack, $encoding);
- }
- $result = mb_strpos($haystack, $neddle);
- if ($result === false) {
- $this->testFailed(array(Message::SHOULD_CONTAIN_VALUE, $neddle, $haystack));
- }
- }
- /**
- * Make sure that $haystack does not contain $neddle
- *
- * @param $neddle
- * @param $haystack
- * @param bool $caseSensitive
- * @param string $encoding
- * @throws TestFailed
- */
- public function notContains($neddle, $haystack, $caseSensitive = true, $encoding = 'utf8')
- {
- if ($caseSensitive !== true) {
- $neddle = mb_strtolower($neddle, $encoding);
- $haystack = mb_strtolower($haystack, $encoding);
- }
- $result = mb_strpos($haystack, $neddle);
- if ($result !== false) {
- $this->testFailed(array(Message::SHOULD_NOT_CONTAIN_VALUE, $neddle, $haystack));
- }
- }
- /**
- * Tell if given value is NULL
- *
- * @param $actual
- * @throws TestFailed
- */
- public function isNull($actual)
- {
- if ($actual !== null) {
- $this->testFailed(Message::EXPECTED_VALUE, array('NULL', $actual));
- }
- }
- /**
- * Tell if given value is NOT NULL
- *
- * @param $actual
- * @throws TestFailed
- */
- public function isNotNull($actual)
- {
- if ($actual === null) {
- $this->testFailed(Message::NOT_EXPECTED_VALUE, array('NULL'));
- }
- }
- /**
- * @param $actual
- */
- public function isArray($actual)
- {
- if (!is_array($actual)) {
- $this->testFailed(Message::EXPECTED_VALUE, array('array', $actual));
- }
- }
- /**
- * @param $actual
- */
- public function isNotArray($actual)
- {
- if (is_array($actual)) {
- if ($actual === null) {
- $this->testFailed(Message::NOT_EXPECTED_VALUE, array('array'));
- }
- }
- }
- public function isBool($actual)
- {
- if (!is_bool($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
- ), 0);
- }
- }
- public function isNotBool($actual)
- {
- if (is_bool($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
- ), 0);
- }
- }
- public function isFloat($actual)
- {
- if (!is_float($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
- ), 0);
- }
- }
- public function isNotFloat($actual)
- {
- if (is_float($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
- ), 0);
- }
- }
- public function isInt($actual)
- {
- if (!is_int($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
- ), 0);
- }
- }
- public function isNotInt($actual)
- {
- if (is_int($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
- ), 0);
- }
- }
- public function isNumeric($actual)
- {
- if (!is_numeric($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
- ), 0);
- }
- }
- public function isNotNumeric($actual)
- {
- if (is_numeric($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
- ), 0);
- }
- }
- public function isObject($actual)
- {
- if (!is_object($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
- ), 0);
- }
- }
- public function isNotObject($actual)
- {
- if (is_object($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
- ), 0);
- }
- }
- public function isResource($actual)
- {
- if (!is_resource($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
- ), 0);
- }
- }
- public function isNotResource($actual)
- {
- if (is_resource($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
- ), 0);
- }
- }
- public function isScalar($actual)
- {
- if (!is_scalar($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
- ), 0);
- }
- }
- public function isNotScalar($actual)
- {
- if (is_scalar($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
- ), 0);
- }
- }
- public function isString($actual)
- {
- if (!is_string($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
- ), 0);
- }
- }
- public function isNotString($actual)
- {
- if (is_string($actual)) {
- throw new TestFailed(
- str_replace(
- '{0}',
- 'null',
- str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
- ), 0);
- }
- }
- /**
- * @param string $message
- * @throws TestFailed
- */
- public function fail($message = null)
- {
- throw new TestFailed(Message::format(Message::TestFailed, array($message)));
- }
- /**
- * @deprecated use skip() instead
- * @throws TestSkipped
- */
- public function inconclusive()
- {
- $this->skip();
- }
- /**
- * Skip given test
- *
- * @throws TestSkipped
- */
- public function skip($message = null)
- {
- if($message === null){
- $message = Message::TEST_SKIPPED;
- }
- throw new TestSkipped($message);
- }
- /**
- * Object must be an instance of expected class
- */
- public function isInstanceOfClass($expected, $actual)
- {
- $actualType = get_class($actual);
- if ($expected !== $actualType) {
- throw new TestFailed($actualType .' is not an instance of class ' . $expected);
- }
- }
- /**
- * Tell if current object is an instance of given type
- *
- * @deprecated
- * @param string $expected
- * @param mixed $actual
- * @throws Exception\TestFailed
- */
- public function isInstanceOfType($expected, $actual)
- {
- $this->isInstanceOf($expected, $actual);
- }
- /**
- * @depreacated
- * @param $expected
- * @param $actual
- * @throws Exception\TestFailed
- */
- public function isNotInstanceOfType($expected, $actual)
- {
- $this->isNotInstanceOf($expected, $actual);
- }
- /**
- * Tell if current object is an instance of given type
- *
- * @param string $expected
- * @param mixed $actual
- * @throws Exception\TestFailed
- */
- public function isInstanceOf($expected, $actual)
- {
- if(!is_object($actual) || !is_a($actual, $expected)){
- $this->testFailed(Message::IS_NOT_INSTANCEOF, array($actual, $expected));
- }
- }
- /**
- * Tell if current object is NOT an instance of given type
- *
- * @param string $expected
- * @param mixed $actual
- * @throws Exception\TestFailed
- */
- public function isNotInstanceOf($expected, $actual)
- {
- if(!is_object($actual) || !is_a($actual, $expected)){
- $this->testFailed(Message::IS_INSTANCEOF, array($actual, $expected));
- }
- }
- public function throws($class, $methodName, $args = null)
- {
- $exception = false;
- try {
- if ($args !== null) {
- /** @noinspection PhpParamsInspection */
- call_user_func_array(array($class, $methodName), $args);
- } else {
- $class->{$methodName}();
- }
- } catch (\Exception $e) {
- $exception = true;
- }
- if (!$exception) {
- throw new TestFailed($this->text->ExpectedExceptionNotThrown, 0);
- }
- }
- private static function getDescription($mixed)
- {
- if (is_object($mixed)) {
- return get_class($mixed);
- } else {
- if (is_bool($mixed)) {
- return $mixed ? 'true' : 'false';
- } else {
- return (string)$mixed;
- }
- }
- }
- /**
- * @param $message
- * @param array $arguments
- * @throws Exception\TestFailed
- */
- protected static function testFailed($message, array $arguments = array())
- {
- throw new TestFailed(Message::format($message, $arguments), 0);
- }
- }