private function buildPolicy($method, callable $fn) { if (!method_exists('\\Shmock\\Policy', $method)) { throw new \InvalidArgumentException("{$method} does not exist on policies"); } $builder = new ClassBuilder(); $builder->setExtends("\\Shmock\\Policy"); $builder->addMethod($method, function (Invocation $i) use($fn) { return $i->callWith($fn); }, ['$className', '$methodName', '$value', '$static']); $clazz = $builder->create(); return new $clazz(); }
/** * Helper method to properly initialize a class builder with everything * ready for $builder->create() to be invoked. Has no side effects * * @return \Shmock\ClassBuilder\ClassBuilder */ protected function initializeClassBuilder() { // build the mock class $builder = new ClassBuilder(); if (class_exists($this->className)) { $builder->setExtends($this->className); } elseif (interface_exists($this->className)) { $builder->addInterface($this->className); } else { throw new \InvalidArgumentException("Class or interface " . $this->className . " does not exist"); } // every mocked method goes through this invocation, which delegates // the retrieval of the correct Spec to this Instance's Ordering constraint. $resolveCall = function (Invocation $inv) { $spec = $this->ordering->nextSpec($inv->getMethodName()); return $spec->doInvocation($inv); }; return $this->addMethodsToBuilder($builder, $resolveCall); }