1: <?php
2: namespace Peridot\Leo\Interfaces;
3:
4: use Peridot\Leo\Interfaces\Assert\CollectionAssertTrait;
5: use Peridot\Leo\Interfaces\Assert\ObjectAssertTrait;
6: use Peridot\Leo\Interfaces\Assert\TypeAssertTrait;
7: use Peridot\Leo\Assertion;
8: use Peridot\Leo\Leo;
9: use Peridot\Leo\Responder\ResponderInterface;
10:
11: 12: 13: 14: 15: 16: 17: 18: 19:
20: class Assert
21: {
22: use TypeAssertTrait;
23: use ObjectAssertTrait;
24: use CollectionAssertTrait;
25:
26: 27: 28: 29: 30:
31: public static $operators = [
32: '==' => 'loosely->equal',
33: '===' => 'equal',
34: '>' => 'above',
35: '>=' => 'least',
36: '<' => 'below',
37: '<=' => 'most',
38: '!=' => 'not->loosely->equal',
39: '!==' => 'not->equal'
40: ];
41:
42: 43: 44:
45: protected $assertion;
46:
47: 48: 49:
50: public function __construct(Assertion $assertion = null)
51: {
52: if (is_null($assertion)) {
53: $assertion = Leo::assertion();
54: }
55: $this->assertion = $assertion;
56: }
57:
58: 59: 60: 61: 62: 63: 64:
65: public function equal($actual, $expected, $message = "")
66: {
67: $this->assertion->setActual($actual);
68: return $this->assertion->to->loosely->equal($expected, $message);
69: }
70:
71: 72: 73: 74: 75: 76: 77:
78: public function notEqual($actual, $expected, $message = "")
79: {
80: $this->assertion->setActual($actual);
81: return $this->assertion->to->not->equal($expected, $message);
82: }
83:
84: 85: 86: 87: 88: 89: 90: 91:
92: public function throws(callable $fn, $exceptionType, $exceptionMessage = "", $message = "")
93: {
94: $this->assertion->setActual($fn);
95: return $this->assertion->to->throw($exceptionType, $exceptionMessage, $message);
96: }
97:
98: 99: 100: 101: 102: 103: 104: 105:
106: public function doesNotThrow(callable $fn, $exceptionType, $exceptionMessage = "", $message = "")
107: {
108: $this->assertion->setActual($fn);
109: return $this->assertion->not->to->throw($exceptionType, $exceptionMessage, $message);
110: }
111:
112: 113: 114: 115: 116: 117:
118: public function ok($object, $message = "")
119: {
120: $this->assertion->setActual($object);
121: return $this->assertion->to->be->ok($message);
122: }
123:
124: 125: 126: 127: 128: 129:
130: public function notOk($object, $message = "")
131: {
132: $this->assertion->setActual($object);
133: return $this->assertion->to->not->be->ok($message);
134: }
135:
136: 137: 138: 139: 140: 141: 142:
143: public function strictEqual($actual, $expected, $message = "")
144: {
145: $this->assertion->setActual($actual);
146: return $this->assertion->to->equal($expected, $message);
147: }
148:
149: 150: 151: 152: 153: 154: 155:
156: public function notStrictEqual($actual, $expected, $message = "")
157: {
158: $this->assertion->setActual($actual);
159: return $this->assertion->to->not->equal($expected, $message);
160: }
161:
162: 163: 164: 165: 166: 167: 168:
169: public function match($value, $pattern, $message = "")
170: {
171: $this->assertion->setActual($value);
172: return $this->assertion->to->match($pattern, $message);
173: }
174:
175: 176: 177: 178: 179: 180: 181:
182: public function notMatch($value, $pattern, $message = "")
183: {
184: $this->assertion->setActual($value);
185: return $this->assertion->to->not->match($pattern, $message);
186: }
187:
188: 189: 190: 191: 192: 193: 194: 195:
196: public function operator($left, $operator, $right, $message = "")
197: {
198: if (!isset(static::$operators[$operator])) {
199: throw new \InvalidArgumentException("Invalid operator $operator");
200: }
201: $this->assertion->setActual($left);
202: return $this->assertion->{static::$operators[$operator]}($right, $message);
203: }
204:
205: 206: 207: 208: 209: 210:
211: public function __call($method, $args)
212: {
213: switch ($method) {
214: case 'instanceOf':
215: return call_user_func_array([$this, 'isInstanceOf'], $args);
216: case 'include':
217: return call_user_func_array([$this, 'isIncluded'], $args);
218: default:
219: throw new \BadMethodCallException("Call to undefined method $method");
220: }
221: }
222: }
223: