/** * Creates a proxy for the give class using the MockOptions. This will call to create a mock, then create * a method proxy and assign the proxy to the given mock. * @param string $toMock class to mock * @param \Kodova\Poser\MockOptions $options * @throws \Kodova\Poser\Exception\PoserException * @return mixed the mocked object */ public function createProxy($toMock, MockOptions $options) { //set the name in the options if needed $name = $options->getName(); if ($name == null) { $name = $toMock; $options->setName($name); } //are they requesting a mock that uses static that has already been defined if ($options->canMockStatic()) { if (class_exists($toMock, false)) { $class = new ReflectionClass($toMock); if ($class->implementsInterface('Kodova\\Poser\\Proxy\\SubstituteProxy')) { $mock = new $toMock(); } else { throw new PoserException("Unable to create a mock that can stub static calls for {$toMock} as it has already been loaded someplace else"); } } else { $mock = $this->createMock($toMock, $options); } } else { $mock = $this->createMock($toMock, $options); } $invocationContainer = new InvocationContainer($this->mockingMonitor, $options); $mock->setProxy(new MethodProxy($mock, $this->mockingMonitor, $invocationContainer, $options)); return $mock; }
/** * Gets a generator that can build the created mock. * @param string $toMock * @param MockOptions $options * @return Generator */ public function getGenerator($toMock, MockOptions $options) { if ($options->canMockStatic()) { return new SubstituteGenerator($toMock, $options); } $class = new \ReflectionClass($toMock); if ($class->isInterface()) { return new InterfaceGenerator($toMock); } //default return new ExtendedGenerator($toMock); }
/** * Creates and returns the mock built by the builder * * @return mixed The mocked object */ public function mock() { $options = new MockOptions(); $options->setDefaultAnswer($this->defaultAnswer); $options->setName($this->name); $options->setMockStatic($this->mockStatic); $options->setConstants($this->constants); return $this->poserCore->mock($this->class, $options); }
/** * (non-PHPdoc) * @see Poser\Proxy\Generator.AbstractGenerator::getConstants() * @return array * @return array */ public function getConstants() { return $this->mockOptions->getConstants(); }