/**
  * @param string $syntax
  * @param array  $data
  * @return string
  */
 public function render($syntax, array $data = array())
 {
     ArgumentChecker::check($syntax, 'string');
     $renderer = new ValueRenderer();
     if (self::$color) {
         $renderer->setTheme(new DefaultTheme());
     }
     return preg_replace_callback('/\\?/', function () use(&$data, $renderer) {
         $r = $renderer->render(array_shift($data));
         return $r;
     }, $syntax);
 }
 public function __call($words, $args)
 {
     if (null !== $words) {
         $this->syntax .= strtolower(preg_replace('/([A-Z])/', ' $1', $words)) . ' ';
     }
     if (count($args) > 0) {
         $this->data[] = $args[0];
         $this->syntax .= '? ';
     }
     try {
         $syntax = ModuleManager::getInstance()->getSyntaxCache()->getSyntax($this->getSyntax());
     } catch (Exception $e) {
         $syntax = null;
     }
     if ($syntax) {
         self::$lastBuilder = null;
         $class = $syntax->getClass();
         /** @var AbstractModule $instance */
         $instance = new $class();
         $data = $this->getData();
         $types = $syntax->getArgumentTypes();
         $checker = new DataTypeChecker();
         for ($i = 0; $i < count($types); ++$i) {
             try {
                 $data[$i] = $checker->check($types[$i], $data[$i]);
             } catch (DataTypeMismatchException $e) {
                 $renderer = new ValueRenderer();
                 throw new Exception("Argument " . ($i + 1) . ' (' . $renderer->render($data[$i]) . ') must be ' . implode(' or ', $types[$i]) . '.');
             }
         }
         $instance->setData($data);
         $instance->syntax = $syntax;
         try {
             $instance->{$syntax->getMethod()}();
             $this->testCase->assertTrue(true);
         } catch (DidNotMatchException $e) {
             $this->handleFailure($e, $instance);
         }
     }
     return $this;
 }
 public function testRenderAnythingConstant()
 {
     $this->assert($this->renderer->render(self::ANYTHING))->equals('<ANYTHING>');
 }
Exemple #4
0
 protected function renderArguments(array $args = null)
 {
     if (null === $args) {
         return '';
     }
     $valueRenderer = new ValueRenderer();
     return $valueRenderer->renderAll($args);
 }
Exemple #5
0
 /**
  * Expected arguments when invoking the mock.
  *
  * @throws \Exception
  * @return MockBuilder
  */
 public function with()
 {
     $methodArguments = new MethodArguments();
     $this->currentWith = $methodArguments->getMethodArgumentValues(func_get_args(), $this->getClassName() . "::" . $this->currentRules[0]);
     foreach ($this->currentRules as $rule) {
         if ($this->rules[$rule][md5('null')]['hasSetTimes']) {
             $renderer = new ValueRenderer();
             $converter = new NumberToTimesConverter();
             $args = $renderer->renderAll($this->currentWith);
             $times = $this->rules[$rule][md5('null')]['times'];
             $convertToMethod = $converter->convertToMethod($times);
             throw new Exception(sprintf("%s:\n  ->expects('%s')->with(%s)->%s", "When using with you must specify expectations for each with()", $rule, $args, $convertToMethod));
         }
         $this->rules[$rule][md5('null')]['times'] = -1;
     }
     $this->setupWith(new Action\ReturnValueAction(array(null)), $this->isExpecting ? 1 : -1);
     return $this;
 }