Overview

Namespaces

  • None
  • Peridot
    • Leo
      • Formatter
      • Interfaces
        • Assert
      • Matcher
        • Template
      • ObjectPath
      • Responder
  • PHP

Classes

  • Assert
  • Overview
  • Namespace
  • Class
  • Tree
  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:  * Assert is a non-chainable, object oriented interface
 13:  * on top of a Leo Assertion.
 14:  *
 15:  * @method instanceOf() instanceOf(object $actual, string $expected, string $message = "") Perform an instanceof assertion.
 16:  * @method include() include(array $haystack, string $expected, string $message = "") Perform an inclusion assertion.
 17:  *
 18:  * @package Peridot\Leo\Interfaces
 19:  */
 20: class Assert
 21: {
 22:     use TypeAssertTrait;
 23:     use ObjectAssertTrait;
 24:     use CollectionAssertTrait;
 25: 
 26:     /**
 27:      * An array of operators mapping to assertions.
 28:      *
 29:      * @var array
 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:      * @var Assertion
 44:      */
 45:     protected $assertion;
 46: 
 47:     /**
 48:      * @param ResponderInterface $responder
 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:      * Perform an a loose equality assertion.
 60:      *
 61:      * @param mixed $actual
 62:      * @param mixed $expected
 63:      * @param string $message
 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:      * Perform a negated loose equality assertion.
 73:      *
 74:      * @param mixed $actual
 75:      * @param mixed $expected
 76:      * @param string $message
 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:      * Performs a throw assertion.
 86:      *
 87:      * @param callable $fn
 88:      * @param $exceptionType
 89:      * @param string $exceptionMessage
 90:      * @param string $message
 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:      * Performs a negated throw assertion.
100:      *
101:      * @param callable $fn
102:      * @param $exceptionType
103:      * @param string $exceptionMessage
104:      * @param string $message
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:      * Perform an ok assertion.
114:      *
115:      * @param mixed $object
116:      * @param string $message
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:      * Perform a negated assertion.
126:      *
127:      * @param mixed $object
128:      * @param string $message
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:      * Perform a strict equality assertion.
138:      *
139:      * @param mixed $actual
140:      * @param mixed $expected
141:      * @param string $message
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:      * Perform a negated strict equality assertion.
151:      *
152:      * @param mixed $actual
153:      * @param mixed $expected
154:      * @param string $message
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:      * Perform a pattern assertion.
164:      *
165:      * @param string $value
166:      * @param string $pattern
167:      * @param string $message
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:      * Perform a negated pattern assertion.
177:      *
178:      * @param string $value
179:      * @param string $pattern
180:      * @param string $message
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:      * Compare two values using the given operator.
190:      *
191:      * @param mixed $left
192:      * @param string $operator
193:      * @param mixed $right
194:      * @param string $message
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:      * Defined to allow use of reserved words for methods.
207:      *
208:      * @param $method
209:      * @param $args
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: 
Leo API documentation generated by ApiGen