Пример #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;
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function getMatches(array $tokens, array $info = array())
 {
     $var = str_replace('$', '', $this->getInput($tokens));
     return array_filter(array_keys($this->getVariables()), function ($variable) use($var) {
         return AbstractMatcher::startsWith($var, $variable);
     });
 }
Пример #3
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;
 }
 /**
  * {@inheritdoc}
  */
 public function getMatches(array $tokens, array $info = array())
 {
     $input = $this->getInput($tokens);
     return array_filter($this->keywords, function ($keyword) use($input) {
         return AbstractMatcher::startsWith($input, $keyword);
     });
 }
Пример #5
0
 /**
  * {@inheritDoc}
  */
 public function getMatches(array $tokens, array $info = array())
 {
     $const = $this->getInput($tokens);
     return array_filter(array_keys(get_defined_constants()), function ($constant) use($const) {
         return AbstractMatcher::startsWith($const, $constant);
     });
 }
Пример #6
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;
 }
Пример #7
0
 /**
  * {@inheritDoc}
  */
 public function getMatches(array $tokens, array $info = array())
 {
     $func = $this->getInput($tokens);
     $functions = get_defined_functions();
     $allFunctions = array_merge($functions['user'], $functions['internal']);
     return array_filter($allFunctions, function ($function) use($func) {
         return AbstractMatcher::startsWith($func, $function);
     });
 }
Пример #8
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;
 }
Пример #9
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;
 }
Пример #10
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;
 }
Пример #11
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;
 }
Пример #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;
 }
Пример #13
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;
 }
Пример #14
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;
 }
Пример #15
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;
 }
Пример #16
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;
 }
Пример #17
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"];
     }
 }
 /**
  * {@inheritDoc}
  */
 public function getMatches(array $tokens, array $info = array())
 {
     $input = $this->getInput($tokens);
     $firstToken = array_pop($tokens);
     if (self::tokenIs($firstToken, self::T_STRING)) {
         // second token is the object operator
         array_pop($tokens);
     }
     $objectToken = array_pop($tokens);
     $objectName = str_replace('$', '', $objectToken[1]);
     $object = $this->getVariable($objectName);
     return array_filter(get_class_methods($object), function ($var) use($input) {
         return AbstractMatcher::startsWith($input, $var);
     });
 }
Пример #19
0
 /**
  * {@inheritDoc}
  */
 public function getMatches(array $tokens, array $info = array())
 {
     $class = $this->getNamespaceAndClass($tokens);
     if (strlen($class) > 0 && $class[0] === '\\') {
         $class = substr($class, 1, strlen($class));
     }
     $quotedClass = preg_quote($class);
     return array_map(function ($className) use($class) {
         // get the number of namespace separators
         $nsPos = substr_count($class, '\\');
         $pieces = explode('\\', $className);
         // $methods = Mirror::get($class);
         return implode('\\', array_slice($pieces, $nsPos, count($pieces)));
     }, array_filter(get_declared_classes(), function ($className) use($quotedClass) {
         return AbstractMatcher::startsWith($quotedClass, $className);
     }));
 }
Пример #20
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;
 }
Пример #21
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;
 }
Пример #22
0
 /**
  *
  * {@inheritdoc}
  *
  */
 public function getMatches(array $tokens, array $info = array())
 {
     $input = $this->getInput($tokens);
     $firstToken = array_pop($tokens);
     if (self::tokenIs($firstToken, self::T_STRING)) {
         // second token is the nekudotayim operator
         array_pop($tokens);
     }
     $class = $this->getNamespaceAndClass($tokens);
     $reflection = new \ReflectionClass($class);
     $vars = array_merge(array_map(function ($var) {
         return '$' . $var;
     }, array_keys($reflection->getStaticProperties())), array_keys($reflection->getConstants()));
     return array_map(function ($name) use($class) {
         return $class . '::' . $name;
     }, array_filter($vars, function ($var) use($input) {
         return AbstractMatcher::startsWith($input, $var);
     }));
 }
Пример #23
0
 /**
  *
  * {@inheritdoc}
  *
  */
 public function getMatches(array $tokens, array $info = array())
 {
     $input = $this->getInput($tokens);
     $firstToken = array_pop($tokens);
     if (self::tokenIs($firstToken, self::T_STRING)) {
         // second token is the nekudotayim operator
         array_pop($tokens);
     }
     $class = $this->getNamespaceAndClass($tokens);
     $reflection = new \ReflectionClass($class);
     $methods = $reflection->getMethods(\ReflectionMethod::IS_STATIC);
     $methods = array_map(function (\ReflectionMethod $method) {
         return $method->getName();
     }, $methods);
     return array_map(function ($name) use($class) {
         return $class . '::' . $name;
     }, array_filter($methods, function ($method) use($input) {
         return AbstractMatcher::startsWith($input, $method);
     }));
 }
Пример #24
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;
     }
 }
Пример #25
0
 /**
  * {@inheritdoc}
  */
 public function getMatches(array $tokens, array $info = array())
 {
     $input = $this->getInput($tokens);
     $firstToken = array_pop($tokens);
     if (self::tokenIs($firstToken, self::T_STRING)) {
         // second token is the object operator
         array_pop($tokens);
     }
     $objectToken = array_pop($tokens);
     $objectName = str_replace('$', '', $objectToken[1]);
     $object = $this->getVariable($objectName);
     if (!$object instanceof \MongoClient) {
         return array();
     }
     $list = $object->listDBs();
     return array_filter(array_map(function ($info) {
         return $info['name'];
     }, $list['databases']), function ($var) use($input) {
         return AbstractMatcher::startsWith($input, $var);
     });
 }
Пример #26
0
 /**
  * Get description for error output.
  *
  * @param mixed $actual
  *
  * @return string
  */
 protected function getFailDescription($actual)
 {
     return str_replace(['contains ', 'exists', 'has ', 'is ', 'are ', 'matches ', 'starts with ', 'ends with ', 'reference ', 'not not '], ['does not contain ', 'does not exist', 'does not have ', 'is not ', 'are not ', 'does not match ', 'starts not with ', 'ends not with ', 'don\'t reference ', 'not '], $this->expected->getFailDescription($actual));
 }
Пример #27
0
 /**
  * Get description for error output.
  *
  * @param array $actual
  *
  * @return string
  */
 protected function getFailDescription($actual)
 {
     return $this->expected->getFailDescription($actual[$this->number]) . sprintf(' on element #%d of %d', $this->number + 1, count($actual));
 }