Skip to content

Commit

Permalink
Merge pull request #18 from kilip/wip
Browse files Browse the repository at this point in the history
implemented local session
  • Loading branch information
kilip committed Jun 17, 2019
2 parents 19fab19 + 7bd4f26 commit ec2cfc8
Show file tree
Hide file tree
Showing 42 changed files with 851 additions and 140 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Expand Up @@ -31,7 +31,7 @@ cache:
- &run-test-coverage
robo coverage
- &start-php-server
php -S localhost:8000 -t tests/Fixtures/public > /dev/null 2>&1 &
php -S localhost:8000 -t src/bridge/Resources/fixtures/public > /dev/null 2>&1 &

jobs:
include:
Expand All @@ -57,5 +57,8 @@ before_install:
install:
- *update-project-dependencies

before_script:
- *start-php-server

script:
- *run-test
4 changes: 4 additions & 0 deletions behat.yaml.dist
Expand Up @@ -3,12 +3,16 @@ default:
default:
paths:
- '%paths.base%/src/bridge/Resources/features'
- '%paths.base%/src/behat/Resources/features'
- '%paths.base%/src/phpspec/Resources/features'
contexts:
- Doyo\Bridge\CodeCoverage\Context\ContainerContext
- Doyo\Bridge\CodeCoverage\Context\CoverageContext
- Doyo\PhpSpec\CodeCoverage\Context\ApplicationContext
- Doyo\PhpSpec\CodeCoverage\Context\FilesystemContext
- Doyo\Bridge\CodeCoverage\Context\BehatContext:
cwd: '%paths.base%/src/bridge/Resources/fixtures'
- Doyo\Bridge\CodeCoverage\Context\ConsoleContext

extensions:
Doyo\Behat\CodeCoverage\Extension:
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Expand Up @@ -45,6 +45,8 @@
"symfony/event-dispatcher": "^3.4 | ^4.0"
},
"require-dev": {
"behat/mink-goutte-driver": "^1.2",
"behatch/contexts": "^3.2",
"guzzlehttp/guzzle": "^6.3",
"phpspec/phpspec": "^4.3 | ^5.0",
"phpunit/phpcov": "^4.0 | >=5.0",
Expand Down
3 changes: 2 additions & 1 deletion src/behat/Extension.php
Expand Up @@ -63,8 +63,9 @@ public function load(ContainerBuilder $container, array $config)
$container->set('doyo.coverage.container', $coverageContainer);
$container->set('doyo.coverage', $coverageContainer->get('coverage'));

/* @var \Symfony\Component\Console\Input\InputInterface $input */
$input = $container->get('cli.input');
$coverageEnabled = $input->hasParameterOption(['--coverage'], true);
$coverageEnabled = $input->hasParameterOption(['--coverage']);
$container->setParameter('doyo.coverage_enabled', $coverageEnabled);

$listener = new Definition(CoverageListener::class);
Expand Down
96 changes: 96 additions & 0 deletions src/bridge/.editorconfig
@@ -0,0 +1,96 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]
# Change these settings to your own preference
indent_style = space
indent_size = 4

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.feature]
indent_style = space
indent_size = 2

[*.json]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[*.neon]
indent_style = tab
indent_size = 4

[*.php]
indent_style = space
indent_size = 4

[*.xml]
indent_style = space
indent_size = 4

[*.yml]
indent_style = space
indent_size = 4
trim_trailing_whitespace = false

[*.yaml]
indent_style = space
indent_size = 4
trim_trailing_whitespace = false

[.circleci/config.yml]
indent_style = space
indent_size = 2

[.gitmodules]
indent_style = tab

[.php_cs]
indent_style = space
indent_size = 4

[.styleci.yml]
indent_style = space
indent_size = 2

[.travis.yml]
indent_style = space
indent_size = 2

[appveyor.yml]
indent_style = space
indent_size = 2

[behat.yml{.dist}]
indent_style = space
indent_size = 2

[coverage.yml{,.dist}]
indent_style = space
indent_size = 2

[coverage.yaml{,.dist}]
indent_style = space
indent_size = 2

[composer.json]
indent_style = space
indent_size = 4

[phpstan.neon{,.dist}]
indent_style = tab
indent_size = 4

[phpunit.xml{,.dist}]
indent_style = space
indent_size = 4
11 changes: 6 additions & 5 deletions src/bridge/Compiler/CoveragePass.php
Expand Up @@ -40,15 +40,14 @@ private function processDriver(ContainerBuilder $container)

private function processFilter(ContainerBuilder $container)
{
$config = $container->getParameter('config.filter');
$config = $container->getParameter('config');
$config = $config['filter'];
$definition = $container->getDefinition('coverage.filter');

foreach ($config as $options) {
$options['basePath'] = '';
$this->filterWhitelist($definition, $options, 'add');
$exclude = $options['exclude'];
foreach ($exclude as $item) {
$item['basePath'] = '';
$this->filterWhitelist($definition, $item, 'remove');
}
}
Expand Down Expand Up @@ -85,11 +84,13 @@ private function filterWhitelist(Definition $definition, $options, $method)
if (false !== ($pos=strpos($file, '*'))) {
$files = [];
foreach (glob($file) as $filename) {
$filename = realpath($filename);
$files[] = $filename;
}
}

$definition->addMethodCall($method.'File'.$methodSuffix, $files);
foreach($files as $file){
$definition->addMethodCall($method.'File'.$methodSuffix, [$file]);
}
}
}
}
48 changes: 48 additions & 0 deletions src/bridge/Compiler/SessionPass.php
@@ -0,0 +1,48 @@
<?php


namespace Doyo\Bridge\CodeCoverage\Compiler;


use Doyo\Bridge\CodeCoverage\Listener\LocalListener;
use Doyo\Bridge\CodeCoverage\Session\LocalSession;
use Doyo\Bridge\CodeCoverage\Session\RemoteSession;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class SessionPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$sessions = $container->getParameterBag()->get('sessions');

foreach($sessions as $name => $config){
$driver = $config['driver'];
if('local' === $driver){
$this->createLocalSession($container, $name, $config);
}
}
}

private function createLocalSession(ContainerBuilder $container, string $name, array $config)
{
$id = 'sessions.'.$name;
$sessionId = $id.'.session';
$listenerId = $id.'.listener';

$session = new Definition(LocalSession::class);
$session->addArgument($name);
$session->addTag('coverage.session');
$session->addMethodCall('init',[$container->getParameter('config')]);
$container->setDefinition($sessionId, $session);

$listener = new Definition(LocalListener::class);
$listener->addArgument(new Reference($sessionId));
$container->setDefinition($listenerId, $listener);

$dispatcher = $container->findDefinition('coverage');
$dispatcher->addMethodCall('addSubscriber',[new Reference($listenerId)]);
}
}
1 change: 1 addition & 0 deletions src/bridge/Configuration.php
Expand Up @@ -63,6 +63,7 @@ private function configureCoverageSection(ArrayNodeDefinition $node)
->end()
->booleanNode('xdebug_patch')->defaultTrue()->end()
->booleanNode('debug')->defaultFalse()->end()
->scalarNode('env')->defaultValue('prod')->end()
->arrayNode('coverage')
->addDefaultsIfNotSet()
->children()
Expand Down
13 changes: 8 additions & 5 deletions src/bridge/ContainerFactory.php
Expand Up @@ -15,6 +15,7 @@

use Doyo\Bridge\CodeCoverage\Compiler\CoveragePass;
use Doyo\Bridge\CodeCoverage\Compiler\ReportPass;
use Doyo\Bridge\CodeCoverage\Compiler\SessionPass;
use Doyo\Bridge\CodeCoverage\Console\Application;
use Doyo\Bridge\CodeCoverage\DependencyInjection\CodeCoverageExtension;
use Symfony\Component\Config\ConfigCache;
Expand All @@ -38,17 +39,14 @@ class ContainerFactory
public function __construct(array $config = [])
{
$this->config = $config;
$this->doCreateContainer();
}

/**
* @return ContainerInterface
*/
public function getContainer(): ContainerInterface
{
if (null === $this->container) {
$this->doCreateContainer();
}

return $this->container;
}

Expand Down Expand Up @@ -119,6 +117,7 @@ private function doCreateContainer()

$builder->addCompilerPass(new CoveragePass());
$builder->addCompilerPass(new ReportPass());
$builder->addCompilerPass(new SessionPass());
$builder->compile(true);

$dumper = new PhpDumper($builder);
Expand All @@ -143,7 +142,11 @@ private function normalizeConfig($configuration)
{
$configs = [];
foreach ($configuration['imports'] as $file) {
$configs[] = $this->importFile($file);
if(false !== ($file = realpath($file))){
$configs[] = $this->importFile($file);
}else{
throw new \RuntimeException('Import file: '.$file.' is not exists or readable.');
}
}

return $configs;
Expand Down
92 changes: 92 additions & 0 deletions src/bridge/Context/BehatContext.php
@@ -0,0 +1,92 @@
<?php


namespace Doyo\Bridge\CodeCoverage\Context;


use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\PyStringNode;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;

class BehatContext implements Context
{
private $configFile;

/**
* @var string
*/
private $cwd;

/**
* @var ConsoleContext
*/
private $consoleContext;

public function __construct(
$cwd = false
)
{
$cwd = $cwd ?:getcwd();
$this->cwd = $cwd;
}

/**
* @beforeScenario
*/
public function beforeScenario(BeforeScenarioScope $scope)
{
$this->consoleContext = $scope->getEnvironment()->getContext(ConsoleContext::class);

$coverageContext = $scope->getEnvironment()->getContext(CoverageContext::class);
$coverageContext->setWorkingDir($this->cwd);
}

/**
* @Given I configure behat with:
*/
public function iConfigureBehat(PyStringNode $node)
{
$contents = $node->getRaw();
$id = md5($contents);
$tmp = sys_get_temp_dir().'/doyo/tests/behat/behat-'.$id.'.yaml';
if(!is_dir($dir = dirname($tmp))){
mkdir($dir, 0775, true);
}
file_put_contents($tmp, $contents);

$this->configFile = $tmp;
}

/**
* @Given I run behat
*
* @param array $options
* @param string $cwd
*/
public function iRunBehat(array $options = [], $cwd = null)
{
$finder = new ExecutableFinder();
$phpdbg = $finder->find('phpdbg');
$cmd = realpath(__DIR__.'/../Resources/fixtures/bin/behat');
$configFile = $this->configFile;
$cwd = realpath($this->cwd);

$phpdbg = [$phpdbg, '-qrr'];
if(extension_loaded('xdebug')){
$phpdbg = [];
}


$commands = array_merge($phpdbg,[
$cmd,
'--config='.$configFile,
'--coverage'
]);

$commands = array_merge($commands, $options);
$this->consoleContext->run($commands, $cwd);
}
}

0 comments on commit ec2cfc8

Please sign in to comment.