Skip to content

Commit

Permalink
Merge a82d5ea into 19fab19
Browse files Browse the repository at this point in the history
  • Loading branch information
kilip committed Jun 15, 2019
2 parents 19fab19 + a82d5ea commit 48f0424
Show file tree
Hide file tree
Showing 22 changed files with 418 additions and 114 deletions.
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
8 changes: 4 additions & 4 deletions src/bridge/Compiler/CoveragePass.php
Expand Up @@ -44,11 +44,9 @@ private function processFilter(ContainerBuilder $container)
$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 +83,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]);
}
}
}
}
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
5 changes: 1 addition & 4 deletions src/bridge/ContainerFactory.php
Expand Up @@ -38,17 +38,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
15 changes: 10 additions & 5 deletions src/bridge/Context/ContainerContext.php
Expand Up @@ -40,16 +40,21 @@ public function setContainer($container)
*/
public function iConfigureCodeCoverage(PyStringNode $node = null)
{
$config = [];
$config = [
'env' => 'behat',
'debug' => true,
];
if (null !== $node) {
$config = Yaml::parse($node->getRaw());
}

$container = (new ContainerFactory($config, true))->getContainer();
$container->set('console.input', new StringInput(''));
$container->set('console.output', new StreamOutput(fopen('php://memory', '+w')));
if(is_null($this->container)){
$container = (new ContainerFactory($config))->getContainer();
$container->set('console.input', new StringInput(''));
$container->set('console.output', new StreamOutput(fopen('php://memory', '+w')));

$this->container = $container;
$this->container = $container;
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/bridge/Resources/coverage.yaml
@@ -0,0 +1,10 @@
filter:
- directory: ""
exclude:
- Spec
- Resources
- Context
- Driver
- vendor
- build
- file: RoboFile.php
10 changes: 5 additions & 5 deletions src/bridge/Resources/features/config.feature
Expand Up @@ -19,12 +19,12 @@ Feature: Configuration
Given I have load container with:
"""
reports:
clover: build/clover.xml
crap4j: build/logs/crap4j.xml
html: build/html
php: build/cov/php.cov
clover: build/behat-test/clover.xml
crap4j: build/behat-test/logs/crap4j.xml
html: build/behat-test/html
php: build/behat-test/cov/php.cov
text: console
xml: build/xml
xml: build/behat-test/xml
"""
Then service "<processor>" should loaded

Expand Down
44 changes: 34 additions & 10 deletions src/bridge/Session/AbstractSession.php
Expand Up @@ -72,6 +72,8 @@ abstract class AbstractSession implements SessionInterface, \Serializable
'testCase',
];

private $started = false;

/**
* AbstractSession constructor.
*
Expand All @@ -94,6 +96,7 @@ public function init(array $config)
$this->config = $config;
$this->createContainer($config);
$this->processor = $this->container->get('factory')->createProcessor(true);
$this->reset();
$this->save();
}

Expand Down Expand Up @@ -131,7 +134,10 @@ public function refresh()
$adapter = $this->adapter;

$cached = $adapter->getItem(static::CACHE_KEY)->get();
$this->fromCache($cached);

if(!is_null($cached)){
$this->fromCache($cached);
}

$this->createContainer($this->config);
}
Expand All @@ -153,11 +159,8 @@ private function toCache()
return $data;
}

private function fromCache($cache)
private function fromCache(array $cache)
{
if (null === $cache) {
return;
}
foreach ($cache as $name => $value) {
$this->{$name} = $value;
}
Expand Down Expand Up @@ -212,29 +215,50 @@ public function setTestCase(TestCase $testCase)
public function start()
{
if (null === $this->testCase) {
throw new SessionException('Can not start coverage without null TestCase');
throw new SessionException('Can not start coverage with null TestCase');
}

try {
$container = $this->container;
$testCase = $this->testCase;
$processor = $container->get('factory')->createProcessor();
$processor->setCurrentTestCase($testCase);

$processor->start($testCase);

$this->currentProcessor = $processor;
$this->started = true;
} catch (\Exception $exception) {
$this->started = false;
$message = sprintf(
"Can not start coverage on session %s. Error message:\n%s",
$this->getName(),
$exception->getMessage()
);
$exception = new SessionException($message);
$this->addException($exception);
}
}

public function stop()
{
$this->currentProcessor->stop();
$this->processor->merge($this->currentProcessor);
try{
$this->currentProcessor->stop();
$this->processor->merge($this->currentProcessor);
$this->started = false;
}catch (\Exception $exception){
$message = sprintf(
"Can not stop coverage on session <comment>%s</comment>. Error message:\n<error>%s</error>",
$this->name,
$exception->getMessage()
);
$e = new SessionException($message);
$this->addException($e);
}
}

public function shutdown()
{
if (null !== $this->currentProcessor) {
if ($this->started) {
$this->stop();
}
$this->save();
Expand Down
3 changes: 0 additions & 3 deletions src/bridge/Session/LocalSession.php
Expand Up @@ -20,13 +20,10 @@ public static function startSession($name): bool
$self = new static($name);
try {
$self->start();
register_shutdown_function([$self, 'shutdown']);

return true;
} catch (\Exception $exception) {
$self->addException($exception);
$self->save();

return false;
}
}
Expand Down
37 changes: 15 additions & 22 deletions src/bridge/Session/RemoteSession.php
Expand Up @@ -26,31 +26,24 @@ public static function startSession()
return false;
}

$name = $_SERVER[static::HEADER_SESSION_KEY];
$session = new static($name);

if (isset($_SERVER[static::HEADER_TEST_CASE_KEY])) {
$session->doStartSession();
} else {
if(!isset($_SERVER[static::HEADER_TEST_CASE_KEY])){
return false;
}
$session->save();

return true;
}

public function doStartSession()
{
$name = $_SERVER[static::HEADER_TEST_CASE_KEY];
$testCase = new TestCase($name);
$this->setTestCase($testCase);

try {
$this->start();
register_shutdown_function([$this, 'shutdown']);
} catch (\Exception $e) {
$this->reset();
$this->exceptions[] = $e;
$sessionName = $_SERVER[static::HEADER_SESSION_KEY];
$session = new static($sessionName);
$testCaseName = $_SERVER[static::HEADER_TEST_CASE_KEY];
$testCase = new TestCase($testCaseName);

try{
$session->setTestCase($testCase);
$session->start();
$session->save();
return true;
}catch (\Exception $e){
$session->addException($e);
$session->save();
return false;
}
}
}

0 comments on commit 48f0424

Please sign in to comment.