Example #1
0
 function testCallback()
 {
     $callback = function (ExactArgument $argument) {
         return $argument->value() == 'foo';
     };
     $this->assert(Argument::callback($callback)->accepts(new ExactArgument('foo')));
     $this->assert->not(Argument::callback($callback)->accepts(new ExactArgument('bar')));
 }
Example #2
0
 public function printAll(Mockster $mockster)
 {
     $class = (new \ReflectionClass($mockster->mock()))->getParentClass();
     $all = [];
     foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
         $arguments = array_map(function () {
             return Argument::any();
         }, $method->getParameters());
         $calls = $mockster->__call($method->getName(), $arguments)->has()->calls();
         if ($calls) {
             $all[] = $this->printCalls($method->getName(), $calls);
         }
     }
     return "History of [{$class->getName()}]\n  " . implode("\n  ", $all);
 }
Example #3
0
 function testCanGetMoreGeneral()
 {
     Mockster::stub($this->foo->bar('one'))->will()->return_('bar');
     Mockster::stub($this->foo->bar(Argument::any()))->will()->return_('foo');
     $this->assert($this->mock->bar('one'), 'bar');
     $this->assert($this->mock->bar('two'), 'foo');
 }
Example #4
0
 function testPrintStubHistory()
 {
     Mockster::stub($this->foo->foo(Arg::any(), Arg::any()))->will()->return_('foo')->once()->then()->return_(['foo'])->once()->then()->return_(new \DateTime('2011-12-13 14:15:16 UTC'))->once()->then()->throw_(new \InvalidArgumentException("Oh no"));
     $this->mock->foo(4, 2);
     $this->mock->foo('One', 'Two');
     $this->mock->foo('Three');
     try {
         $this->mock->foo('Four', new RecordStubUsageTest_ToString());
     } catch (\InvalidArgumentException $ignored) {
     }
     $this->assert((new HistoryPrinter())->printStub($this->foo->foo()), "No calls recorded for [" . RecordStubUsageTest_FooClass::class . "::foo()]");
     $this->assert((new HistoryPrinter())->printStub($this->foo->foo(Arg::integer(), Arg::integer())), "History of [" . RecordStubUsageTest_FooClass::class . "::foo()]\n" . "  foo(4, 2) -> 'foo'");
     $this->assert((new HistoryPrinter())->printStub($this->foo->foo(Arg::string(), Arg::any())), "History of [" . RecordStubUsageTest_FooClass::class . "::foo()]\n" . "  foo('One', 'Two') -> ['foo']\n" . "  foo('Three', NULL) -> <DateTime>(2011-12-13T14:15:16+00:00)\n" . "  foo('Four', <" . RecordStubUsageTest_ToString::class . ">('foo')) !! <InvalidArgumentException>('Oh no')");
 }
Example #5
0
 /**
  * @param $method
  * @param $arguments
  * @return array|Argument[]
  */
 private function normalize($method, $arguments)
 {
     $normalized = [];
     $reflection = new \ReflectionMethod($this->class, $method);
     foreach ($reflection->getParameters() as $i => $parameter) {
         if (array_key_exists($i, $arguments)) {
             $argument = $arguments[$i];
             if ($argument instanceof Argument) {
                 $normalized[] = $argument;
             } else {
                 $normalized[] = Argument::exact($argument);
             }
         } else {
             if ($parameter->isDefaultValueAvailable()) {
                 $normalized[] = Argument::exact($parameter->getDefaultValue());
             }
         }
     }
     return $normalized;
 }