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')); }
<?php require '../boot.php'; require 'PHPUnit/Framework.php'; require 'PHPUnit/Framework/Test.php'; require 'PHPUnit/TextUI/TestRunner.php'; // we need a lot time and memory here! set_time_limit(120); ini_set('memory_limit', '100M'); // code covarage starts now (if we need it, and xdebug is available) if (isset($_GET['report'])) { if (function_exists('xdebug_start_code_coverage')) { xdebug_start_code_coverage(); } else { unset($_GET['report']); } } // define constas needed for test suite define('DS', DIRECTORY_SEPARATOR); define('TESTS_DIR', TESTS_ROOT . DS . 'unit'); define('TESTS_REPORTS', TESTS_ROOT . DS . 'reports'); define('TESTS_ASSETS', TESTS_ROOT . DS . 'assets'); $options = array(); if (isset($_GET['report'])) { $options['reportDirectory'] = TESTS_REPORTS; } // run the test $r = new Test_Runner(array_cut($_GET, 'for', ''), isset($_GET['skip']) ? explode(',', $_GET['skip']) : array()); $r->runHtml($options);
/** * @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()); }
/** * Generates EMMA code coverage report. * * @param array $xDebugReport_ * * @todo Implement */ protected function report(Test_Runner $runner_, array $xDebugReport_) { $testPaths = $runner_->getTestPaths(); $classpaths = []; foreach (Runtime_Classloader::getClassloaders() as $classloader) { $classpaths = array_merge($classpaths, $classloader->getClasspaths()); } $emmaReport = []; foreach ($classpaths as $clazz => $path) { if (isset($testPaths[$path])) { continue; } $this->reportForClass($clazz, $path, $xDebugReport_, $emmaReport); } $document = new \DOMDocument('1.0', 'utf-8'); $document->formatOutput = true; $report = $document->createElement('report'); $document->appendChild($report); $stats = $document->createElement('stats'); $report->appendChild($stats); $statsPackages = $document->createElement('packages'); $statsPackages->setAttribute('value', count($this->m_packages)); $stats->appendChild($statsPackages); $statsClasses = $document->createElement('classes'); $statsClasses->setAttribute('value', count($this->m_classes)); $stats->appendChild($statsClasses); $statsMethods = $document->createElement('methods'); $statsMethods->setAttribute('value', count($this->m_methods)); $stats->appendChild($statsMethods); $statsFiles = $document->createElement('srcfiles'); $statsFiles->setAttribute('value', count($this->m_files)); $stats->appendChild($statsFiles); $statsLines = $document->createElement('srclines'); $statsLines->setAttribute('value', $this->m_lines); $stats->appendChild($statsLines); $data = $document->createElement('data'); $report->appendChild($data); $all = $document->createElement('all'); $all->setAttribute('name', 'all classes'); $data->appendChild($all); $countLines = 0; $countLinesCovered = 0; $countClasses = 0; $countClassesCovered = 0; $countMethods = 0; $countMethodsCovered = 0; foreach ($emmaReport as $package) { foreach ($package as $files) { foreach ($files as $clazz) { $countClasses++; $countLines += $clazz['lines']; $countLinesCovered += $clazz['covered']; if (0 < $clazz['covered']) { $countClassesCovered++; } foreach ($clazz['methods'] as $method) { $countMethods++; if (0 < $method['covered']) { $countMethodsCovered++; } } } } } $this->appendCoverage($document, $all, 'class, %', $countClasses, $countClassesCovered); $this->appendCoverage($document, $all, 'method, %', $countMethods, $countMethodsCovered); $this->appendCoverage($document, $all, 'block, %', $countMethods, $countMethodsCovered); $this->appendCoverage($document, $all, 'line, %', $countLines, $countLinesCovered); foreach (array_keys($emmaReport) as $package) { $this->appendPackage($document, $all, $package, $emmaReport[$package]); } $document->save($this->m_buildPath); }