protected static function mockImpl($type_, array $args_ = []) { $type = new \ReflectionClass($type_); if ($type->isFinal()) { throw new Exception_IllegalArgument('mock/factory', 'Can not mock final class.'); } $mtime = @filemtime($type->getFileName()); $classMock = 'Mock_' . str_replace('\\', '_', $type->getNamespaceName()) . '_' . $type->getShortName() . "_{$mtime}"; if (false === @class_exists($classMock)) { if (null === self::$m_tmpPath) { self::$m_tmpPath = (string) Test_Runner::get()->getTempPath(); } $fileName = self::$m_tmpPath . "/{$classMock}.php"; if (false === @file_exists($fileName)) { $source = self::weaveMock($classMock, new \ReflectionClass('Components\\Mock'), $type); if (false === @file_put_contents($fileName, $source, 0644)) { throw new Exception_IllegalState('mock/factory', sprintf('Unable to create mock [type: %1$s, path: %2$s].', $type_, $fileName)); } } require_once $fileName; } $classMock = "Components\\{$classMock}"; if (0 < count($args_)) { $refl = new \ReflectionClass($classMock); $mock = $refl->newInstanceArgs($args_); } else { $mock = new $classMock(); } $mock->mockType = $type; return $mock; }
/** * @test * @profile * @ignore(ignoreUntilFixed) */ public function mockFactory() { $mockException = Mock_Factory::mock('Components\\Test_Exception', array('test/unit/case/mock', 'Mocked Exception.')); $mockExceptionDefault = Mock_Factory::mock('Components\\Test_Exception'); $mockRunner = Mock_Factory::mock('Components\\Test_Runner'); $mockListener = Mock_Factory::mock('Components\\Test_Listener'); $mockListener->when('onInitialize')->doReturn(true); $mockListener->when('onExecute')->doReturn(true); $mockListener->when('onTerminate')->doNothing(); assertTrue($mockListener->onExecute($mockRunner)); assertTrue($mockListener->onInitialize($mockRunner)); $mockLL->onTerminate($mockRunner); assertEquals('test/unit/case/mock', $mockException->getNamespace()); assertEquals('Mocked Exception.', $mockException->getMessage()); assertEquals('test/exception', $mockExceptionDefault->getNamespace()); assertEquals('Test exception.', $mockExceptionDefault->getMessage()); $mockBindingModule = Mock_Factory::mock('Components\\Binding_Module'); $mockBindingModule->when('bind')->doAnswer(function (Binding_Module $self_, $type_) { echo "Bound {$type_}\r\n"; return $self_->bind($type_); }); $mockBindingModule->when('configure')->doAnswer(function (Binding_Module $self_) { $self_->bind('Components\\Test_Runner')->toInstance(Test_Runner::get()); $self_->bind(Integer::TYPE)->toInstance(22)->named('boundInteger'); }); $injector = Injector::create($mockBindingModule); assertSame(Test_Runner::get(), $injector->resolveInstance('Components\\Test_Runner')); assertEquals(22, $injector->resolveInstance(Integer::TYPE, 'boundInteger')); }
/** * @param string $name_ * @param boolean $successful_ * @param string $message_ */ public function add($name_, $successful_ = false, $message_ = null) { array_push($this->m_assertions, array('name' => $name_, 'result' => $successful_, 'message' => $message_)); Test_Runner::get()->output->appendAssertion($name_, $successful_, $message_); }
/** * @see \Components\Binding_Module::configure() */ protected function configure() { $this->bind('Test_Runner')->toInstance(Test_Runner::get()); }