public function testPrintReport()
 {
     if (file_exists($file = CodeCoverageSession::getCacheFile())) {
         unlink($file);
     }
     $options = array('enabled' => true, 'output.text' => true, 'output.html' => getcwd(), 'output.clover' => getcwd() . '/clover.xml');
     static::$container->setParameter('session.results', array('some'));
     $runner = new CodeCoverageSession();
     $runner->setOptions($options);
     $runner->setContainer(static::$container);
     $event = new ProcessEvent(static::$container->get('plugins.test'));
     $genericEvent = new GenericEvent(static::$container);
     $genericEvent->addProcessEvent($event);
     // not display coverage when results file not exists
     $runner->process($genericEvent);
     $this->assertNotDisplayContains('html output');
     $this->assertNotDisplayContains('text output');
     $this->assertNotDisplayContains('clover output');
     // display coverage when cache file exists
     Filesystem::create()->serialize($file, $runner);
     $runner->process($genericEvent);
     $this->assertDisplayContains('html output');
     $this->assertDisplayContains('text output');
     $this->assertDisplayContains('clover output');
 }
 public function testShouldRunAllAfterPass()
 {
     Filesystem::create()->cleanDir(static::$tmpDir);
     Filesystem::create()->mkdir(static::$tmpDir);
     static::buildFixtures();
     static::createApplication();
     $this->createTestClass('FooTest', 'tests/FooTest.php', '$this->assertTrue(true);');
     $this->createTestClass('SucceedTest', 'tests/SucceedTest.php', '$this->assertTrue(true);');
     $this->createTestClass('FailedTest', 'tests/FailedTest.php', '$this->assertTrue(false);');
     $this->getTester()->run('-vvv');
     $this->getTester()->run('all phpunit -vvv');
     $this->assertDisplayContains('Tests: 3');
     $this->assertDisplayContains('Assertions: 3');
     $this->assertDisplayContains('Failures: 1');
     $this->getTester()->run('all phpunit -vvv');
     $this->getTester()->run('all phpunit -vvv');
     $this->assertDisplayContains('Tests: 1');
     $this->assertDisplayContains('Assertions: 1');
     $this->assertDisplayContains('Failures: 1');
     $this->assertDisplayContains('Test\\FailedTest');
     $this->createTestClass('FailedTest', 'tests/FailedTest.php', '$this->assertTrue(true);');
     $this->evaluate();
     $this->assertDisplayContains('1 test, 1 assertion');
     $this->getTester()->run('all phpunit -vvv');
     $this->assertDisplayContains('3 tests');
 }
 public function afterSuite()
 {
     Filesystem::create()->serialize(Inspector::getCacheFileName(), $this->results);
     if ($this->coverage) {
         $this->coverage->saveState();
     }
 }
Exemple #4
0
 function let(ContainerInterface $container, PhpGuard $guard, PluginInterface $plugin)
 {
     Filesystem::create()->cleanDir(self::$tmpDir);
     Filesystem::create()->mkdir(self::$tmpDir);
     $container->get('phpguard')->willReturn($guard);
     $this->setContainer($container);
 }
Exemple #5
0
 public function testShouldMonitorBasedOnTags()
 {
     Filesystem::create()->cleanDir($dirTag1 = self::$tmpDir . '/tag1');
     Filesystem::create()->cleanDir($dirTag2 = self::$tmpDir . '/tag2');
     Filesystem::create()->mkdir($dirTag1);
     Filesystem::create()->mkdir($dirTag2);
     $ftag1 = $dirTag1 . '/test1.php';
     $ftag2 = $dirTag2 . '/test1.php';
     $this->getTester()->run('--tags=tag1');
     file_put_contents($ftag1, 'Hello World');
     file_put_contents($ftag2, 'Hello WOrld');
     $this->evaluate();
     $this->assertContains($ftag1, $this->getDisplay());
     $this->assertNotContains($ftag2, $this->getDisplay());
     $this->getTester()->run('--tags=tag2');
     touch($ftag1 = $dirTag1 . '/test2.php');
     touch($ftag2 = $dirTag2 . '/test2.php');
     $this->evaluate();
     $this->assertContains($ftag2, $this->getDisplay());
     $this->assertNotContains($ftag1, $this->getDisplay());
     $this->getTester()->run('--tags=tag1,tag2');
     touch($ftag1 = $dirTag1 . '/test3.php');
     touch($ftag2 = $dirTag2 . '/test3.php');
     $this->evaluate();
     $this->assertContains($ftag2, $this->getDisplay());
     $this->assertContains($ftag1, $this->getDisplay());
 }
Exemple #6
0
 public function testSerialization()
 {
     $file = getcwd() . '/test.dat';
     $data = array('foo' => 'bar', 'hello' => 'world');
     Filesystem::create()->serialize($file, $data);
     $unserialized = Filesystem::create()->unserialize($file);
     $this->assertEquals($data, $unserialized);
 }
 function its_runAll_create_success_event_if_results_only_contain_success_events(Runner $runner)
 {
     $results = array('succeed1' => ResultEvent::createSucceed('Succeed1'), 'succeed2' => ResultEvent::createSucceed('Succeed2'), 'succeed3' => ResultEvent::createSucceed('Succeed3'));
     Filesystem::create()->serialize($this->cacheFile, $results);
     $runner->run(Argument::any())->shouldBeCalled();
     $results = $this->runAll()->getResults();
     $results->shouldHaveCount(1);
     $result = $results[0];
     $result->shouldBeSucceed();
 }
Exemple #8
0
 public static function create()
 {
     $dir = PhpGuard::getPluginCache('behat');
     $file = $dir . DIRECTORY_SEPARATOR . static::FILENAME;
     clearstatcache($file);
     if (file_exists($file)) {
         return Filesystem::create()->unserialize($file);
     } else {
         return new self();
     }
 }
Exemple #9
0
 protected static function buildFixtures()
 {
     $finder = Finder::create();
     Filesystem::create()->copyDir(__DIR__ . '/fixtures', static::$tmpDir, $finder);
     chdir(static::$tmpDir);
     $exFinder = new ExecutableFinder();
     if (!is_executable($executable = $exFinder->find('composer.phar'))) {
         $executable = $exFinder->find('composer');
     }
     $process = new Process($executable . ' dumpautoload');
     $process->run();
     //static::$composerOutput = $process->getOutput();
 }
 public function __construct(CodeCoverageSession $coverageSession = null, Session $session = null, Filesystem $filesystem = null)
 {
     if (is_null($filesystem)) {
         $filesystem = Filesystem::create();
     }
     if (is_null($session)) {
         $session = new Session();
     }
     $this->coverageSession = $coverageSession;
     $this->filesystem = $filesystem;
     $this->session = $session;
     $this->cwd = getcwd();
 }
    /**
     * @BeforeScenario
     */
    public function createBootstrapFile()
    {
        $contextClass = uniqid("FeatureContext");
        $content = <<<EOC
<?php

use Behat\\Behat\\Context\\BehatContext;
use Behat\\Behat\\Exception\\PendingException;

class {$contextClass} extends BehatContext
{
    public function __construct(array \$parameters)
    {
    }

    /**
     * @Given /^I have passed step\$/
     */
    public function iHavePassStep()
    {
        return true;
    }

    /**
     * @Then /^I have failed step\$/
     */
    public function iHaveFailedStep()
    {
        throw new \\Exception("failed");
    }
}
EOC;
        $fs = Filesystem::create();
        $file = 'features/bootstrap/' . $contextClass . '.php';
        $dir = dirname($file);
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }
        $fs->putFileContents($file, $content);
        $behatContents = <<<EOC
default:
    context:
        class: {$contextClass}
EOC;
        $fs->putFileContents('behat.yml', $behatContents);
    }
 function let(ContainerInterface $container, PluginInterface $plugin, AdapterInterface $adapter, Listener $listener, ConsoleHandler $handler, Logger $logger, GenericEvent $event, InputInterface $input)
 {
     if (is_null(static::$cwd)) {
         static::$cwd = getcwd();
     }
     Filesystem::create()->mkdir(static::$tmpDir);
     $container->getByPrefix('plugins')->willReturn(array($plugin));
     $container->getParameter(Argument::any(), Argument::any())->willReturn(null);
     $container->setParameter(Argument::any(), Argument::any())->willReturn(null);
     $container->get('listen.listener')->willReturn($listener);
     $container->get('listen.adapter')->willReturn($adapter);
     $container->get('logger.handler')->willReturn($handler);
     $container->get('logger')->willReturn($logger);
     $event->getContainer()->willReturn($container);
     $container->get('ui.input')->willReturn($input);
     $this->setContainer($container);
 }
Exemple #13
0
 public static function buildFixtures($suffix = 'common')
 {
     $finder = Finder::create();
     Filesystem::create()->copyDir(static::$cwd . '/tests/fixtures/' . $suffix, static::$tmpDir, $finder);
 }
Exemple #14
0
 function letgo()
 {
     Filesystem::create()->cleanDir(self::$tmpDir);
 }
Exemple #15
0
 /**
  * @Given /^(?:|the )(?:spec |class |feature )file "(?P<file>[^"]+)" contains:$/
  */
 public function theFileContains($file, PyStringNode $string)
 {
     $fs = Filesystem::create();
     $dirname = dirname($file);
     if (!file_exists($dirname)) {
         $fs->mkdir($dirname);
     }
     $fs->putFileContents($file, $string->getRaw());
     clearstatcache($file);
 }
 /**
  * @codeCoverageIgnore
  */
 public function handleShutdown()
 {
     $fatalErrors = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR);
     $lastError = error_get_last();
     if ($lastError && in_array($lastError['type'], $fatalErrors)) {
         $message = 'Fatal Error ' . $lastError['message'];
         $error = $lastError;
         $trace = file($this->errorFile);
         $traces = array();
         for ($i = 0, $count = count($trace); $i < $count; $i++) {
             $text = trim($trace[$i]);
             if (false !== ($pos = strpos($text, 'PHP '))) {
                 $text = substr($text, $pos + 4);
             }
             $traces[] = $text;
         }
         $event = ResultEvent::createError($message, $error, null, $traces);
         Filesystem::create()->serialize(Inspector::getResultFileName(), array($event));
         if ($this->coverageRunner) {
             $this->coverageRunner->saveState();
         }
     }
 }
Exemple #17
0
 /**
  * @throws \RuntimeException
  *
  * @return array
  */
 private function renderResult()
 {
     /* @var ResultEvent $resultEvent */
     $results = array();
     $file = static::getCacheFileName();
     if (!file_exists($file)) {
         throw new \RuntimeException(sprintf('Unknown PhpSpec results'));
     }
     $data = Filesystem::create()->unserialize($file);
     // processing succeed first
     foreach ($data as $resultEvent) {
         $file = $resultEvent->getArgument('file');
         $failedKey = md5($file);
         if ($resultEvent->isSucceed()) {
             if (isset($this->failed[$failedKey])) {
                 unset($this->failed[$failedKey]);
             }
         }
     }
     foreach ($data as $resultEvent) {
         $file = $resultEvent->getArgument('file');
         $failedKey = md5($file);
         if (!$resultEvent->isSucceed()) {
             $this->failed[$failedKey] = $resultEvent;
             $results[] = $resultEvent;
         }
     }
     return $results;
 }
 public function saveState()
 {
     $target = $this->path . '/coverage_session.dat';
     if (is_writable($this->path)) {
         Filesystem::create()->serialize($target, $this);
     }
 }
Exemple #19
0
 function letgo()
 {
     chdir(self::$cwd);
     Filesystem::create()->cleanDir(self::$tmpDir . '/test-config');
 }
 function letgo()
 {
     Filesystem::create()->cleanDir(static::$tmpDir);
     chdir(static::$cwd);
 }
Exemple #21
0
 private function checkResult($showSuccess = true)
 {
     /* @var ResultEvent $event */
     $file = static::getResultFileName();
     if (!is_file($file)) {
         throw new \RuntimeException('Unknown phpunit results.');
     }
     $data = Filesystem::create()->unserialize($file);
     $results = array();
     foreach ($data as $event) {
         $file = $event->getArgument('file');
         $failedKey = md5(realpath($file));
         if ($event->isSucceed()) {
             unset($this->failed[$failedKey]);
             if ($showSuccess) {
                 $results[] = $event;
             }
         } else {
             $this->failed[$failedKey] = $file;
             $results[] = $event;
         }
     }
     return $results;
 }
 function its_runAll_should_set_application_exit_code_if_results_has_failed_or_broken(ContainerInterface $container)
 {
     $failed = ResultEvent::createFailed('Failed', array('file' => 'some_file'));
     $success = ResultEvent::createSucceed('Success');
     Filesystem::create()->serialize($this->cacheFile, array('failed' => $failed, 'success' => $success));
     $container->setParameter('application.exit_code', ResultEvent::FAILED)->shouldBeCalled();
     $this->runAll();
 }
Exemple #23
0
 protected function buildSpec($target, $class, $content)
 {
     $time = new \DateTime();
     $time = $time->format('H:i:s');
     $content = $this->getSpecContent($class, $content);
     $content = str_replace('%relative_path%', $target, $content);
     $target = static::$tmpDir . '/' . $target;
     $dir = dirname($target);
     Filesystem::create()->mkdir($dir);
     file_put_contents($target, $content);
     return $target;
 }