Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     $actualLength = null;
     if (is_string($this->value)) {
         // @suggestion multibyte?
         $actualLength = strlen($this->value);
     }
     if (is_array($this->value)) {
         $actualLength = count($this->value);
     }
     if (is_object($this->value)) {
         $actualLength = count(get_object_vars($this->value));
     }
     // The configuration mode.
     if ($this->configurationOnly) {
         essence()->setMatcherConfiguration(__CLASS__, ["length" => $actualLength]);
         return true;
     }
     list($length) = $this->arguments;
     if ($length !== $actualLength) {
         $this->setMessage("%s (expected length) is not equal to %s (actual length)", [$length, $actualLength]);
         return false;
     }
     $this->setMessage("%s and %s have identical length", [$length, $actualLength]);
     return true;
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     $arguments = array_slice(array_merge($this->arguments, [null, null]), 0, 3);
     list($class, $message, $context) = $arguments;
     try {
         $callback = $this->value;
         if (!is_null($context)) {
             $callback = $callback->bindTo($context);
         }
         $callback();
     } catch (\Exception $exception) {
         if (get_class($exception) == $class) {
             if (!is_null($message) and $exception->getMessage() != $message) {
                 $this->setMessage("expected error message %s is not equal to %s", [$message, $exception->getMessage()]);
                 return false;
             }
             $this->setMessage("got %s, just as expected", [$class]);
             return true;
         } else {
             $this->setMessage("%s was expected, but got %s", [$class, get_class($exception)]);
             return false;
         }
     }
     $this->setMessage("nothing was thrown");
     return false;
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     if (is_array($this->value)) {
         $keys = array_keys($this->value);
     } else {
         $keys = array_keys(get_object_vars($this->value));
     }
     if ($this->configurationOnly) {
         essence()->setMatcherConfiguration("Essence\\Matchers\\ContainMatcher", $keys);
         return true;
     }
     list($elements) = $this->arguments;
     if (!is_array($elements)) {
         $elements = [$elements];
     }
     foreach ($elements as $key) {
         if (!in_array($key, $keys, true)) {
             $this->setMessage("key %s does not exist in %s", [$key, $this->value]);
             return false;
         }
     }
     $this->setMessage("key %s exists in %s", [$key, $this->value]);
     return true;
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     if (!empty($this->value)) {
         $this->setMessage("%s is not empty", [$this->value]);
         return false;
     }
     $this->setMessage("%s is empty", [$this->value]);
     return true;
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     if (!is_null($this->value)) {
         $this->setMessage("%s is not NULL", [$this->value]);
         return false;
     }
     $this->setMessage("the given value is NULL");
     return true;
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     if ($this->value !== false) {
         $this->setMessage("false (expected) is not equal to true (actual)");
         return false;
     }
     $this->setMessage("the given value is equal to false");
     return true;
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     if (!!$this->value) {
         $this->setMessage("%s is positive", [$this->value]);
         return true;
     }
     $this->setMessage("%s is not positive", [$this->value]);
     return false;
 }
Esempio n. 8
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     list($number, $delta) = $this->arguments;
     if (abs($this->value - $number) > $delta) {
         $this->setMessage("%s is not approximately equal to %s", [$this->value, $number]);
         return false;
     }
     $this->setMessage("%s is approximately equal to %s", [$this->value, $number]);
     return true;
 }
Esempio n. 9
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     list($pattern) = $this->arguments;
     if (!preg_match($pattern, $this->value)) {
         $this->setMessage("%s does not match %s", [$this->value, $pattern]);
         return false;
     }
     $this->setMessage("%s matches %s", [$this->value, $pattern]);
     return true;
 }
Esempio n. 10
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     list($method) = $this->arguments;
     if (!method_exists($this->value, $method)) {
         $this->setMessage("%s does not have a method called %s", [$this->value, $method]);
         return false;
     }
     $this->setMessage("%s has a method called %s", [$this->value, $method]);
     return true;
 }
Esempio n. 11
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     list($least, $most) = $this->arguments;
     if ($least <= $this->value and $this->value <= $most) {
         $this->setMessage("%s is within %s and %s", [$this->value, $least, $most]);
         return true;
     }
     $this->setMessage("%s is not within %s and %s", [$this->value, $least, $most]);
     return false;
 }
Esempio n. 12
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     list($anotherValue) = $this->arguments;
     if ($this->value === $anotherValue) {
         $this->setMessage("%s is equal to %s", [$this->value, $anotherValue]);
         return true;
     }
     $this->setMessage("%s is not equal to %s", [$this->value, $anotherValue]);
     return false;
 }
Esempio n. 13
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     if (count($this->arguments) > 0) {
         $this->number = (int) end($this->arguments);
     } else {
         $this->incorrectUsage("Desired condition was not specified.");
     }
     if ($config = essence()->getMatcherConfiguration("Essence\\Matchers\\LengthMatcher")) {
         $this->value = $config["length"];
     }
 }
Esempio n. 14
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     list($element) = $this->arguments;
     $result = true;
     if (is_string($this->value)) {
         $result = strpos($this->value, $element) !== false;
     }
     if (is_array($this->value)) {
         $result = in_array($element, $this->value, true);
     }
     if (!$result) {
         $this->setMessage("%s does not contain %s", [$this->value, $element]);
         return false;
     }
     $this->setMessage("%s contains %s", [$this->value, $element]);
     return true;
 }
Esempio n. 15
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     if ($this->configurationOnly) {
         return true;
     }
     list($elements) = $this->arguments;
     if (!is_array($elements)) {
         $elements = [$elements];
     }
     foreach ($elements as $element) {
         if (!in_array($element, $this->value, true)) {
             $this->setMessage("%s does not contain %s", [$this->value, $element]);
             return false;
         }
     }
     $this->setMessage("%s contains all %s elements", [$this->value, $elements]);
     return true;
 }
Esempio n. 16
0
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     parent::run();
     list($type) = $this->arguments;
     if (in_array($type, $this->valueType)) {
         if (gettype($this->value) == $type) {
             $this->setMessage("%s is of type %s", [$this->value, $type]);
             return true;
         }
         $this->setMessage("%s is not of type %s", [$this->value, $type]);
         return false;
     } else {
         if (is_object($this->value) and get_class($this->value) == $type) {
             $this->setMessage("%s is an instance of %s", [$this->value, $type]);
             return true;
         }
         $this->setMessage("%s is not an instance of %s", [$this->value, $type]);
         return false;
     }
 }