szurubooru/src/Assert.php

96 lines
2.2 KiB
PHP
Raw Normal View History

2014-05-04 21:23:12 +02:00
<?php
class Assert
{
public function throws($callback, $expectedMessage)
{
$success = false;
try
{
$callback();
$success = true;
}
catch (Exception $e)
{
if (stripos($e->getMessage(), $expectedMessage) === false)
2014-05-07 00:34:02 +02:00
{
$this->fail('Assertion failed. Expected: "' . $expectedMessage . '", got: "' . $e->getMessage() . '"'
. PHP_EOL . $e->getTraceAsString() . PHP_EOL . '---' . PHP_EOL);
}
2014-05-04 21:23:12 +02:00
}
if ($success)
$this->fail('Assertion failed. Expected exception, got nothing');
}
public function doesNotThrow($callback)
{
try
{
$ret = $callback();
}
catch (Exception $e)
{
2014-05-07 00:34:02 +02:00
$this->fail('Assertion failed. Expected nothing, got exception: "' . $e->getMessage() . '"'
. PHP_EOL . $e->getTraceAsString() . PHP_EOL . '---' . PHP_EOL);
2014-05-04 21:23:12 +02:00
}
return $ret;
}
public function isNull($actual)
{
if ($actual !== null and $actual !== false)
$this->fail('Assertion failed. Expected: NULL, got: "' . $actual . '"');
}
2014-05-04 21:23:12 +02:00
public function isNotNull($actual)
{
if ($actual === null or $actual === false)
$this->fail('Assertion failed. Expected: not NULL, got: "' . $actual . '"');
2014-05-04 21:23:12 +02:00
}
public function isTrue($actual)
{
return $this->areEqual(1, intval(boolval($actual)));
}
public function isFalse($actual)
{
return $this->areEqual(0, intval(boolval($actual)));
}
public function areEqual($expected, $actual)
{
if ($expected !== $actual)
$this->fail('Assertion failed. Expected: "' . $this->dumpVar($expected) . '", got: "' . $this->dumpVar($actual) . '"');
}
public function areEquivalent($expected, $actual)
2014-05-04 21:23:12 +02:00
{
if ($expected != $actual)
$this->fail('Assertion failed. Expected: "' . $this->dumpVar($expected) . '", got: "' . $this->dumpVar($actual) . '"');
}
public function areNotEqual($expected, $actual)
{
if ($expected === $actual)
$this->fail('Assertion failed. Specified objects are equal');
}
public function areNotEquivalent($expected, $actual)
{
if ($expected == $actual)
$this->fail('Assertion failed. Specified objects are equivalent');
}
2014-05-04 21:23:12 +02:00
public function dumpVar($var)
{
ob_start();
var_dump($var);
return trim(ob_get_clean());
}
public function fail($message)
{
throw new SimpleException($message);
}
}