示例#1
0
 public function testDummyReporterStopped()
 {
     $reporter = new DummyReporter();
     $runner = new Runner();
     $runner->addReporter($reporter);
     $test = new AlwaysSuccessTest();
     $runner->addTest($test);
     $runner->getEventManager()->attach(RunEvent::EVENT_AFTER_RUN, function () {
         return false;
     });
     $runner->run();
 }
示例#2
0
 public function testDummyReporterStopped()
 {
     $reporter = new DummyReporter(true);
     $runner = new Runner();
     $runner->addReporter($reporter);
     $check1 = new AlwaysSuccessCheck();
     $check2 = new AlwaysSuccessCheck();
     $runner->addCheck($check1);
     $runner->addCheck($check2);
     $result = $runner->run();
     $this->assertFalse($result->offsetExists($check2));
 }
示例#3
0
 public function testAfterRunStopTesting()
 {
     $eventFired = false;
     $eventFired2 = false;
     $self = $this;
     $test1 = new AlwaysSuccessTest();
     $test2 = new ReturnThisTest(new Failure());
     $this->runner->addTest($test1);
     $this->runner->addTest($test2);
     $this->runner->getEventManager()->attach(RunEvent::EVENT_AFTER_RUN, function (RunEvent $e) use(&$self, &$test1, &$eventFired) {
         $eventFired = true;
         $self->assertInstanceOf('ZFTool\\Diagnostics\\Test\\TestInterface', $e->getTarget());
         // stop testing after first test
         if ($e->getTarget() === $test1) {
             return false;
         }
     });
     $this->runner->getEventManager()->attach(RunEvent::EVENT_STOP, function (RunEvent $e) use(&$self, &$test1, &$eventFired2) {
         $eventFired2 = true;
         $self->assertInstanceOf('ZFTool\\Diagnostics\\Result\\Collection', $e->getResults());
         $self->assertInstanceOf('ZFTool\\Diagnostics\\Result\\Success', $e->getLastResult());
         $self->assertSame($test1, $e->getTarget());
     });
     $results = $this->runner->run();
     $this->assertTrue($eventFired);
     $this->assertTrue($eventFired2);
     $this->assertNotNull($results[$test1]);
     $this->assertNotContains($test2, $results);
 }
 public function runAction()
 {
     $sm = $this->getServiceLocator();
     /* @var $console AdapterInterface */
     /* @var $config array */
     /* @var $mm ModuleManager */
     $console = $sm->get('console');
     $config = $sm->get('Configuration');
     $mm = $sm->get('ModuleManager');
     $verbose = $this->params()->fromRoute('verbose', false);
     $debug = $this->params()->fromRoute('debug', false);
     $quiet = !$verbose && !$debug && $this->params()->fromRoute('quiet', false);
     $breakOnFailure = $this->params()->fromRoute('break', false);
     $checkGroupName = $this->params()->fromRoute('filter', false);
     // Get basic diag configuration
     $config = isset($config['diagnostics']) ? $config['diagnostics'] : array();
     // Collect diag tests from modules
     $modules = $mm->getLoadedModules(false);
     foreach ($modules as $moduleName => $module) {
         if (is_callable(array($module, 'getDiagnostics'))) {
             $checks = $module->getDiagnostics();
             if (is_array($checks)) {
                 $config[$moduleName] = $checks;
             }
             // Exit the loop early if we found check definitions for
             // the only check group that we want to run.
             if ($checkGroupName && $moduleName == $checkGroupName) {
                 break;
             }
         }
     }
     // Filter array if a check group name has been provided
     if ($checkGroupName) {
         $config = array_intersect_ukey($config, array($checkGroupName => 1), 'strcasecmp');
         if (empty($config)) {
             $m = new ConsoleModel();
             $m->setResult($console->colorize(sprintf("Unable to find a group of diagnostic checks called \"%s\". Try to use module name (i.e. \"%s\").\n", $checkGroupName, 'Application'), ColorInterface::YELLOW));
             $m->setErrorLevel(1);
             return $m;
         }
     }
     // Check if there are any diagnostic checks defined
     if (empty($config)) {
         $m = new ConsoleModel();
         $m->setResult($console->colorize("There are no diagnostic checks currently enabled for this application - please add one or more " . "entries into config \"diagnostics\" array or add getDiagnostics() method to your Module class. " . "\n\nMore info: https://github.com/zendframework/ZFTool/blob/master/docs/" . "DIAGNOSTICS.md#adding-checks-to-your-module\n", ColorInterface::YELLOW));
         $m->setErrorLevel(1);
         return $m;
     }
     // Analyze check definitions and construct check instances
     $checkCollection = array();
     foreach ($config as $checkGroupName => $checks) {
         foreach ($checks as $checkLabel => $check) {
             // Do not use numeric labels.
             if (!$checkLabel || is_numeric($checkLabel)) {
                 $checkLabel = false;
             }
             // Handle a callable.
             if (is_callable($check)) {
                 $check = new Callback($check);
                 if ($checkLabel) {
                     $check->setLabel($checkGroupName . ': ' . $checkLabel);
                 }
                 $checkCollection[] = $check;
                 continue;
             }
             // Handle check object instance.
             if (is_object($check)) {
                 if (!$check instanceof CheckInterface) {
                     throw new RuntimeException('Cannot use object of class "' . get_class($check) . '" as check. ' . 'Expected instance of ZendDiagnostics\\Check\\CheckInterface');
                 }
                 // Use duck-typing for determining if the check allows for setting custom label
                 if ($checkLabel && is_callable(array($check, 'setLabel'))) {
                     $check->setLabel($checkGroupName . ': ' . $checkLabel);
                 }
                 $checkCollection[] = $check;
                 continue;
             }
             // Handle an array containing callback or identifier with optional parameters.
             if (is_array($check)) {
                 if (!count($check)) {
                     throw new RuntimeException('Cannot use an empty array() as check definition in "' . $checkGroupName . '"');
                 }
                 // extract check identifier and store the remainder of array as parameters
                 $testName = array_shift($check);
                 $params = $check;
             } elseif (is_scalar($check)) {
                 $testName = $check;
                 $params = array();
             } else {
                 throw new RuntimeException('Cannot understand diagnostic check definition "' . gettype($check) . '" in "' . $checkGroupName . '"');
             }
             // Try to expand check identifier using Service Locator
             if (is_string($testName) && $sm->has($testName)) {
                 $check = $sm->get($testName);
                 // Try to use the ZendDiagnostics namespace
             } elseif (is_string($testName) && class_exists('ZendDiagnostics\\Check\\' . $testName)) {
                 $class = new \ReflectionClass('ZendDiagnostics\\Check\\' . $testName);
                 $check = $class->newInstanceArgs($params);
                 // Try to use the ZFTool namespace
             } elseif (is_string($testName) && class_exists('ZFTool\\Diagnostics\\Check\\' . $testName)) {
                 $class = new \ReflectionClass('ZFTool\\Diagnostics\\Check\\' . $testName);
                 $check = $class->newInstanceArgs($params);
                 // Check if provided with a callable inside an array
             } elseif (is_callable($testName)) {
                 $check = new Callback($testName, $params);
                 if ($checkLabel) {
                     $check->setLabel($checkGroupName . ': ' . $checkLabel);
                 }
                 $checkCollection[] = $check;
                 continue;
                 // Try to expand check using class name
             } elseif (is_string($testName) && class_exists($testName)) {
                 $class = new \ReflectionClass($testName);
                 $check = $class->newInstanceArgs($params);
             } else {
                 throw new RuntimeException('Cannot find check class or service with the name of "' . $testName . '" (' . $checkGroupName . ')');
             }
             if (!$check instanceof CheckInterface) {
                 // not a real check
                 throw new RuntimeException('The check object of class ' . get_class($check) . ' does not implement ' . 'ZendDiagnostics\\Check\\CheckInterface');
             }
             // Use duck-typing for determining if the check allows for setting custom label
             if ($checkLabel && is_callable(array($check, 'setLabel'))) {
                 $check->setLabel($checkGroupName . ': ' . $checkLabel);
             }
             $checkCollection[] = $check;
         }
     }
     // Configure check runner
     $runner = new Runner();
     $runner->addChecks($checkCollection);
     $runner->getConfig()->setBreakOnFailure($breakOnFailure);
     if (!$quiet && $this->getRequest() instanceof ConsoleRequest) {
         if ($verbose || $debug) {
             $runner->addReporter(new VerboseConsole($console, $debug));
         } else {
             $runner->addReporter(new BasicConsole($console));
         }
     }
     // Run tests
     $results = $runner->run();
     $request = $this->getRequest();
     // Return result
     if ($request instanceof ConsoleRequest) {
         return $this->processConsoleRequest($results);
     }
     if ($request instanceof Request) {
         return $this->processHttpRequest($request, $results);
     }
 }
示例#5
0
 public function testConfig()
 {
     $this->assertInstanceOf('ZFTool\\Diagnostics\\Config', $this->runner->getConfig());
     $this->assertInstanceOf('ZFTool\\Diagnostics\\ConfigInterface', $this->runner->getConfig());
     $newConfig = new Config();
     $this->runner->setConfig($newConfig);
     $this->assertInstanceOf('ZFTool\\Diagnostics\\Config', $this->runner->getConfig());
     $this->assertSame($newConfig, $this->runner->getConfig());
     $newConfig->setBreakOnFailure(true);
     $this->assertTrue($newConfig->getBreakOnFailure());
     $this->assertTrue($this->runner->getBreakOnFailure());
     $this->runner->setBreakOnFailure(false);
     $this->assertFalse($this->runner->getBreakOnFailure());
     $this->assertFalse($newConfig->getBreakOnFailure());
     $newConfig->setCatchErrorSeverity(100);
     $this->assertEquals(100, $newConfig->getCatchErrorSeverity());
     $this->assertEquals(100, $this->runner->getCatchErrorSeverity());
     $this->runner->setCatchErrorSeverity(200);
     $this->assertEquals(200, $newConfig->getCatchErrorSeverity());
     $this->assertEquals(200, $this->runner->getCatchErrorSeverity());
     $this->setExpectedException('ZFTool\\Diagnostics\\Exception\\InvalidArgumentException');
     $this->runner->setConfig('foo');
 }
 /**
  * Run diagnostics
  *
  * @return ConsoleModel|ViewModel
  * @throws \ZFTool\Diagnostics\Exception\RuntimeException
  */
 public function runAction()
 {
     // check for help mode
     if ($this->requestOptions->getFlagHelp()) {
         return $this->runHelp();
     }
     // get needed options to shorten code
     $flagVerbose = $this->requestOptions->getFlagVerbose();
     $flagDebug = $this->requestOptions->getFlagDebug();
     $flagQuiet = $this->requestOptions->getFlagQuiet();
     $flagBreak = $this->requestOptions->getFlagBreak();
     $testGroupName = $this->requestOptions->getTestGroupName();
     // output header
     if (!$flagQuiet) {
         $this->consoleHeader('Starting diagnostics for Zend Framework 2 project');
     }
     // start output
     if (!$flagQuiet) {
         $this->console->writeLine('       => Get basic diag configuration');
     }
     // Get basic diag configuration
     $config = isset($this->configuration['diagnostics']) ? $this->configuration['diagnostics'] : array();
     // start output
     if (!$flagQuiet) {
         $this->console->writeLine('       => Collect diag tests from modules ');
     }
     // Collect diag tests from modules
     $modules = $this->moduleManager->getLoadedModules(false);
     foreach ($modules as $moduleName => $module) {
         if (is_callable(array($module, 'getDiagnostics'))) {
             $tests = $module->getDiagnostics();
             if (is_array($tests)) {
                 $config[$moduleName] = $tests;
             }
             // Exit the loop early if we found test definitions for
             // the only test group that we want to run.
             if ($testGroupName && $moduleName == $testGroupName) {
                 break;
             }
         }
     }
     // Filter array if a test group name has been provided
     if ($testGroupName) {
         $config = array_intersect_key($config, array($testGroupName => 1));
     }
     // start output
     if (!$flagQuiet) {
         $this->console->writeLine('       => Analyze test definitions and construct test instances');
     }
     // Analyze test definitions and construct test instances
     $testCollection = array();
     foreach ($config as $testGroupName => $tests) {
         foreach ($tests as $testLabel => $test) {
             // Do not use numeric labels.
             if (!$testLabel || is_numeric($testLabel)) {
                 $testLabel = false;
             }
             // Handle a callable.
             if (is_callable($test)) {
                 $test = new Callback($test);
                 if ($testLabel) {
                     $test->setLabel($testGroupName . ': ' . $testLabel);
                 }
                 $testCollection[] = $test;
                 continue;
             }
             // Handle test object instance.
             if (is_object($test)) {
                 if (!$test instanceof TestInterface) {
                     throw new RuntimeException('Cannot use object of class "' . get_class($test) . '" as test. ' . 'Expected instance of ZFTool\\Diagnostics\\Test\\TestInterface');
                 }
                 if ($testLabel) {
                     $test->setLabel($testGroupName . ': ' . $testLabel);
                 }
                 $testCollection[] = $test;
                 continue;
             }
             // Handle an array containing callback or identifier with optional parameters.
             if (is_array($test)) {
                 if (!count($test)) {
                     throw new RuntimeException('Cannot use an empty array() as test definition in "' . $testGroupName . '"');
                 }
                 // extract test identifier and store the remainder of array as parameters
                 $testName = array_shift($test);
                 $params = $test;
             } elseif (is_scalar($test)) {
                 $testName = $test;
                 $params = array();
             } else {
                 throw new RuntimeException('Cannot understand diagnostic test definition "' . gettype($test) . '" in "' . $testGroupName . '"');
             }
             // Try to expand test identifier using Service Locator
             if (is_string($testName) && $this->getServiceLocator()->has($testName)) {
                 $test = $this->getServiceLocator()->get($testName);
                 // Try to use the built-in test class
             } elseif (is_string($testName) && class_exists('ZFTool\\Diagnostics\\Test\\' . $testName)) {
                 $class = new \ReflectionClass('ZFTool\\Diagnostics\\Test\\' . $testName);
                 $test = $class->newInstanceArgs($params);
                 // Check if provided with a callable inside the array
             } elseif (is_callable($testName)) {
                 $test = new Callback($testName, $params);
                 if ($testLabel) {
                     $test->setLabel($testGroupName . ': ' . $testLabel);
                 }
                 $testCollection[] = $test;
                 continue;
                 // Try to expand test using class name
             } elseif (is_string($testName) && class_exists($testName)) {
                 $class = new \ReflectionClass($testName);
                 $test = $class->newInstanceArgs($params);
             } else {
                 throw new RuntimeException('Cannot find test class or service with the name of "' . $testName . '" (' . $testGroupName . ')');
             }
             if (!$test instanceof TestInterface) {
                 // not a real test
                 throw new RuntimeException('The test object of class ' . get_class($test) . ' does not implement ' . 'ZFTool\\Diagnostics\\Test\\TestInterface');
             }
             // Apply label
             if ($testLabel) {
                 $test->setLabel($testGroupName . ': ' . $testLabel);
             }
             $testCollection[] = $test;
         }
     }
     if (!$flagQuiet) {
         $this->console->writeLine();
         $this->console->write(' Diag ', Color::NORMAL, Color::CYAN);
         $this->console->write(' ');
     }
     // Configure test runner
     $runner = new Runner();
     $runner->addTests($testCollection);
     $runner->getConfig()->setBreakOnFailure($flagBreak);
     if (!$flagQuiet && $this->getRequest() instanceof ConsoleRequest) {
         if ($flagVerbose || $flagDebug) {
             $runner->addReporter(new VerboseConsole($this->console, $flagDebug));
         } else {
             $runner->addReporter(new BasicConsole($this->console));
         }
     }
     // Run tests
     $results = $runner->run();
     // Return result
     if ($this->getRequest() instanceof ConsoleRequest) {
         // Return appropriate error code in console
         $model = new ConsoleModel();
         $model->setVariable('results', $results);
         if ($results->getFailureCount() > 0) {
             $model->setErrorLevel(1);
         } else {
             $model->setErrorLevel(0);
         }
     } else {
         // Display results as a web page
         $model = new ViewModel();
         $model->setVariable('results', $results);
     }
     return $model;
 }