Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3d1d5ba1a4
Fetching contributors…

Cannot retrieve contributors at this time

file 141 lines (124 sloc) 3.288 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
<?php

namespace mageekguy\atoum\asserters;

use
mageekguy\atoum\asserters,
mageekguy\atoum\exceptions,
mageekguy\atoum\tools\diffs
;

class exception extends asserters\object
{
public function setWith($value, $label = null, $check = true)
{
$exception = $value;

if ($exception instanceof \closure)
{
$exception = null;

try
{
$value();
}
catch (\exception $exception) {}
}

parent::setWith($exception, $label, false);

if ($check === true)
{
if (self::isException($exception) === false)
{
$this->fail(sprintf($this->getLocale()->_('%s is not an exception'), $this));
}
else
{
$this->pass();
}
}

return $this;
}

public function isInstanceOf($value, $failMessage = null)
{
try
{
self::check($value, __FUNCTION__);
}
catch (\logicException $exception)
{
if (self::classExists($value) === false || (strtolower(ltrim($value, '\\')) !== 'exception' && is_subclass_of($value, 'exception') === false))
{
throw new exceptions\logic\invalidArgument('Argument of ' . __METHOD__ . '() must be a \exception instance or an exception class name');
}
}

return parent::isInstanceOf($value, $failMessage);
}

public function hasDefaultCode($failMessage = null)
{
if ($this->valueIsSet()->value->getCode() === 0)
{
return $this->pass();
}
else
{
$this->fail($failMessage !== null ? $failMessage : sprintf($this->getLocale()->_('code is %s instead of 0'), $this->value->getCode()));
}
}

public function hasCode($code, $failMessage = null)
{
if ($this->valueIsSet()->value->getCode() === $code)
{
return $this->pass();
}
else
{
$this->fail($failMessage !== null ? $failMessage : sprintf($this->getLocale()->_('code is %s instead of %s'), $this->value->getCode(), $code));
}
}

public function hasMessage($message, $failMessage = null)
{
if ($this->valueIsSet()->value->getMessage() == (string) $message)
{
return $this->pass();
}
else
{
$this->fail($failMessage !== null ? $failMessage : sprintf($this->getLocale()->_('message \'%s\' is not identical to \'%s\''), $this->value->getMessage(), $message));
}
}

public function hasNestedException(\exception $exception = null, $failMessage = null)
{
if ($exception === null)
{
if ($this->valueIsSet()->value->getPrevious() !== null)
{
return $this->pass();
}
else
{
$this->fail($failMessage !== null ? $failMessage : $this->getLocale()->_('exception does not contain any nested exception'));
}
}
else
{
if ($this->valueIsSet()->value->getPrevious() == $exception)
{
return $this->pass();
}
else
{
$this->fail($failMessage !== null ? $failMessage : $this->getLocale()->_('exception does not contain this nested exception'));
}
}
}

protected function valueIsSet($message = 'Exception is undefined')
{
return parent::valueIsSet($message);
}

protected static function check($value, $method)
{
if (self::isException($value) === false)
{
throw new exceptions\logic\invalidArgument('Argument of ' . __CLASS__ . '::' . $method . '() must be an exception instance');
}
}

protected static function isException($value)
{
return (parent::isObject($value) === true && $value instanceof \exception === true);
}
}
Something went wrong with that request. Please try again.