mageekguy\atoum\asserters\exception: lines coverage

95% of 455

OPs

93% of 75

Lines

75% of 79

Branches

35% of 49

Paths
Method OPs OPs % Lines Line % Branches Branches % Paths Path %
mageekguy\atoum\asserters\exception::__get() 50 64% 9 67% 12 50% 4 75%
mageekguy\atoum\asserters\exception::setWith() 72 100% 19 95% 14 71% 9 22%
mageekguy\atoum\asserters\exception::isInstanceOf() 69 99% 6 100% 13 77% 7 43%
mageekguy\atoum\asserters\exception::hasDefaultCode() 43 100% 7 100% 5 80% 2 50%
mageekguy\atoum\asserters\exception::hasCode() 45 100% 7 100% 5 80% 2 50%
mageekguy\atoum\asserters\exception::hasMessage() 46 100% 8 100% 5 80% 2 50%
mageekguy\atoum\asserters\exception::hasNestedException() 63 100% 9 100% 18 94% 18 11%
mageekguy\atoum\asserters\exception::getLastValue() 7 100% 1 100% 1 100% 1 100%
mageekguy\atoum\asserters\exception::valueIsSet() 12 100% 2 100% 1 100% 1 100%
mageekguy\atoum\asserters\exception::getMessageAsserter() 22 100% 1 100% 1 100% 1 100%
mageekguy\atoum\asserters\exception::check() 26 81% 6 83% 4 25% 2 50%
#
1
<?php
2

                    
3
namespace mageekguy\atoum\asserters;
4

                    
5
use
6
	mageekguy\atoum\asserters,
7
	mageekguy\atoum\exceptions
8
;
9

                    
10
class exception extends asserters\object
11
{
12
	protected static $lastValue = null;
13

                    
14
	public function __get($asserter)67%
15
	{
16
		switch (strtolower($asserter))
17
		{
18
			case 'hasdefaultcode':
19
			case 'hasnestedexception':
20
				return $this->{$asserter}();
21

                    
22
			case 'message':
23
				return $this->getMessageAsserter();
24

                    
25
			default:
26
				return $this->generator->__get($asserter);
27
		}
28
	}
29

                    
30
	public function setWith($value, $checkType = true)95%
31
	{
32
		$exception = $value;
33

                    
34
		if ($exception instanceof \closure)
35
		{
36
			$exception = null;
37

                    
38
			try
39
			{
40
				$value($this->getTest());
41
			}
42
			catch (\exception $exception) {}
43
		}
44

                    
45
		parent::setWith($exception, false);
46

                    
47
		if ($checkType === true)
48
		{
49
			if ($exception instanceof \exception === false)
50
			{
51
				$this->fail($this->_('%s is not an exception', $this));
52
			}
53
			else
54
			{
55
				$this->pass();
56

                    
57
				static::$lastValue = $exception;
58
			}
59
		}
60

                    
61
		return $this;
62
	}
63

                    
64
	public function isInstanceOf($value, $failMessage = null)100%
65
	{
66
		try
67
		{
68
			$this->check($value, __FUNCTION__);
69
		}
70
		catch (\logicException $exception)
71
		{
72
			if (self::classExists($value) === false || (strtolower(ltrim($value, '\\')) !== 'exception' && is_subclass_of($value, 'exception') === false))
73
			{
74
				throw new exceptions\logic\invalidArgument('Argument of ' . __METHOD__ . '() must be a \exception instance or an exception class name');
75
			}
76
		}
77

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

                    
81
	public function hasDefaultCode($failMessage = null)100%
82
	{
83
		if ($this->valueIsSet()->value->getCode() === 0)
84
		{
85
			$this->pass();
86
		}
87
		else
88
		{
89
			$this->fail($failMessage ?: $this->_('code is %s instead of 0', $this->value->getCode()));
90
		}
91

                    
92
		return $this;
93
	}
94

                    
95
	public function hasCode($code, $failMessage = null)100%
96
	{
97
		if ($this->valueIsSet()->value->getCode() === $code)
98
		{
99
			$this->pass();
100
		}
101
		else
102
		{
103
			$this->fail($failMessage ?: $this->_('code is %s instead of %s', $this->value->getCode(), $code));
104
		}
105

                    
106
		return $this;
107
	}
108

                    
109
	public function hasMessage($message, $failMessage = null)
110
	{
111
		if ($this->valueIsSet()->value->getMessage() == (string) $message)
112
		{
113
			$this->pass();
114
		}
115
		else
116
		{
117
			$this->fail($failMessage ?: $this->_('message \'%s\' is not identical to \'%s\'', $this->value->getMessage(), $message));
118
		}
119

                    
120
		return $this;
121
	}
122

                    
123
	public function hasNestedException(\exception $exception = null, $failMessage = null)100%
124
	{
125
		$nestedException = $this->valueIsSet()->value->getPrevious();
126

                    
127
		if (($exception === null && $nestedException !== null) || ($exception !== null && $nestedException == $exception))
128
		{
129
			$this->pass();
130
		}
131
		else
132
		{
133
			$this->fail($failMessage ?: ($exception === null ? $this->_('exception does not contain any nested exception') : $this->_('exception does not contain this nested exception')));
134
		}
135

                    
136
		return $this;
137
	}
138

                    
139
	public static function getLastValue()100%
140
	{
141
		return static::$lastValue;
142
	}
143

                    
144
	protected function valueIsSet($message = 'Exception is undefined')
145
	{
146
		return parent::valueIsSet($message);
147
	}
148

                    
149
	protected function getMessageAsserter()100%
150
	{
151
		return $this->generator->__call('phpString', array($this->valueIsSet()->value->getMessage()));
152
	}
153

                    
154
	protected function check($value, $method)83%
155
	{
156
		if ($value instanceof \exception === false)
157
		{
158
			throw new exceptions\logic\invalidArgument('Argument of ' . __CLASS__ . '::' . $method . '() must be an exception instance');
159
		}
160

                    
161
		return $this;
162
	}
163
}