Beispiel #1
0
 /**
  * Matches if value is not null.
  *
  * @factory
  */
 public static function notNullValue()
 {
     if (!self::$_NOT_INSTANCE) {
         self::$_NOT_INSTANCE = Hamcrest_Core_IsNot::not(self::nullValue());
     }
     return self::$_NOT_INSTANCE;
 }
Beispiel #2
0
 public function doAssert(\Hamcrest_Matcher $matcher, $message = null)
 {
     $matcher = \Hamcrest_Core_IsCollectionContaining::hasItem($matcher);
     $matcher = \Hamcrest_Core_IsNot::not($matcher);
     if (!empty($message)) {
         \Hamcrest_MatcherAssert::assertThat($message, $this->value, $matcher);
     } else {
         \Hamcrest_MatcherAssert::assertThat($this->value, $matcher);
     }
 }
Beispiel #3
0
 /**
  * Evaluates to false if ANY of the passed in matchers evaluate to true.
  *
  * @factory ...
  */
 public static function noneOf()
 {
     $args = func_get_args();
     return Hamcrest_Core_IsNot::not(new self(Hamcrest_Util::createMatcherArray($args)));
 }
Beispiel #4
0
/**
 * Matches if value does not match $value.
 */
function not($value)
{
    require_once 'Hamcrest/Core/IsNot.php';
    return Hamcrest_Core_IsNot::not($value);
}
Beispiel #5
0
 public function assert($name, $args)
 {
     $name = trim($name, '_');
     // Convert camelCase to underscores
     $name = preg_replace_callback('/([a-z])([A-Z])/', function ($m) {
         return $m[1] . '_' . $m[2];
     }, $name);
     // Make it all lowercase
     $name = strtolower($name);
     // Remove 'to' if it's at the beginning since it might be used
     // when manually calling expect()
     $name = preg_replace('/^to_/', '', $name);
     // Extract ORs/ANDs/BUTs/AS from name
     if (preg_match('/^[a-z]+_(or|and|but|as)_?$/i', $name)) {
         // We need to disable implicit assertion if set
         $origImplicit = $this->implicitAssert;
         $this->implicitAssert = false;
         $parts = preg_split('/\\b(or|and|but|as)\\b/i', $name, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
         $prefix = '';
         do {
             $part = array_shift($parts);
             if (empty($part)) {
                 break;
             }
             // Should be an "as"
             $this->assert($prefix . $part, count($parts) ? array() : $args);
             $prefix = array_shift($parts);
         } while (count($parts));
         if (strtolower($prefix) === 'as') {
             $this->describedAs($args[0]);
         }
         if ($origImplicit) {
             $this->implicitAssert = true;
             $this->doAssert();
         }
         return $this;
     }
     // Explode by the underscore
     $parts = explode('_', $name);
     // Calculate if it's a negation
     $isNegation = false;
     foreach ($parts as $part) {
         if ($part === 'not' || $part == 'no') {
             $isNegation = !$isNegation;
         }
     }
     // Manage coordination operators
     switch ($parts[0]) {
         case 'described':
             if (empty($parts[1]) || $parts[1] !== 'as') {
                 break;
             }
         case 'as':
             $this->describedAs($args[0]);
             return $this;
         case 'and':
             $this->expression->addOperator('AND', 10);
             array_shift($parts);
             break;
         case 'or':
             $this->expression->addOperator('OR', 5);
             array_shift($parts);
             break;
         case 'but':
             $this->expression->addOperator('BUT', 1);
             array_shift($parts);
             break;
         default:
             // If no operator was given assume OR
             if (NULL !== $this->prevMatcher) {
                 $this->expression->addOperator('OR', 5);
             }
     }
     // If nothing left reuse previous matcher
     if (empty($parts)) {
         if (NULL === $this->prevMatcher) {
             throw new \RuntimeException("Unable to re-use previous matcher since it's empty");
         } else {
             if (empty($args)) {
                 throw new \RuntimeException("Unable to re-use previous matcher without arguments being given");
             }
         }
         $matcher = $this->prevMatcher;
     } else {
         $matcher = implode(' ', $parts);
         $this->prevMatcher = $matcher;
     }
     // Find the matcher for the given name
     $callback = Spec::matchers()->find($matcher);
     if (FALSE === $callback) {
         // Remove special words from the original matcher name
         $parts = explode('_', $name);
         $parts = array_diff($parts, $this->specialWords);
         $name = implode('_', $parts);
         $msg = "Matcher '" . str_replace('_', ' ', $name) . "' not found.";
         $suggestions = Spec::matchers()->suggest($name, 0.5);
         if (count($suggestions)) {
             $msg .= " Perhaps you meant to use '" . array_shift($suggestions) . "' ?";
         }
         throw new \Exception($msg);
     }
     // Instantiate the matcher
     $matcher = call_user_func_array($callback, $args);
     if ($isNegation) {
         $matcher = \Hamcrest_Core_IsNot::not($matcher);
     }
     $this->expression->addOperand($matcher);
     // Run the assertion now if the implicit flag is set
     if ($this->implicitAssert) {
         $this->doAssert();
     }
     return $this;
 }
Beispiel #6
0
 protected function createMatcher()
 {
     return Hamcrest_Core_IsNot::not('something');
 }
 /**
  * Matches an empty array.
  *
  * @factory
  */
 public static function nonEmptyArray()
 {
     return Hamcrest_Core_DescribedAs::describedAs('a non-empty array', self::arrayWithSize(Hamcrest_Core_IsNot::not(0)));
 }