Skip to content

Commit

Permalink
test: Improve mutation coverage
Browse files Browse the repository at this point in the history
- Add more test cases to improve mutation coverage
  • Loading branch information
MontealegreLuis committed Jul 5, 2021
1 parent dbc286b commit e402155
Show file tree
Hide file tree
Showing 14 changed files with 386 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ check:
@vendor/bin/composer-require-checker check
@docker-compose run --rm tests vendor/bin/phpunit --testsuite 'Integration tests'
@vendor/bin/rector process --dry-run
@docker-compose run --rm -e XDEBUG_MODE=coverage tests php vendor/bin/infection --threads=4s
@docker-compose run --rm -e XDEBUG_MODE=coverage tests php vendor/bin/infection --threads=4
4 changes: 2 additions & 2 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"mutators": {
"@default": true
},
"minMsi": 87,
"minCoveredMsi": 87
"minMsi": 85,
"minCoveredMsi": 85
}
2 changes: 1 addition & 1 deletion src/Code/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class Definition implements Named, HasNodeIdentifier
use ObjectHashIdentifier;

/** @var Method[] */
protected $methods;
protected array $methods;

/** @param Method[] $methods */
public function __construct(Name $name, array $methods = [])
Expand Down
6 changes: 6 additions & 0 deletions tests/resources/.code/classes/processor/graphviz/name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

class plStyleName
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@

class plGraphvizProcessorDefaultStyle extends plGraphvizProcessorStyle
{
private plStyleName $name;

public function __construct(plStyleName $name)
{
}
}
Binary file modified tests/resources/images/graphviz-dot-classic-theme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/resources/images/graphviz-dot-php-theme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/resources/images/graphviz-dot-recursive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/resources/images/graphviz-neato-recursive.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
274 changes: 274 additions & 0 deletions tests/unit/Configuration/DigraphBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
<?php declare(strict_types=1);
/**
* PHP version 7.4
*
* This source file is subject to the license that is bundled with this package in the file LICENSE.
*/

namespace PhUml\Configuration;

use PHPUnit\Framework\TestCase;
use PhUml\Code\ClassDefinition;
use PhUml\Code\Name;
use PhUml\Parser\CodebaseDirectory;

final class DigraphBuilderTest extends TestCase
{
/** @test */
function it_builds_a_code_finder_that_searches_recursively()
{
$configuration = new DigraphConfiguration([
'recursive' => true,
'associations' => 'true',
'hide-private' => true,
'hide-protected' => 0,
'hide-attributes' => 1,
'hide-methods' => 'true',
'hide-empty-blocks' => '',
'theme' => 'phuml',
]);
$builder = new DigraphBuilder($configuration);
$directory = new CodebaseDirectory(__DIR__ . '/../../resources/.code/interfaces/processor');

$codeFinder = $builder->codeFinder($directory);

$this->assertCount(2, $codeFinder->files());
}

/** @test */
function it_builds_a_code_finder_that_does_not_search_recursively()
{
$configuration = new DigraphConfiguration([
'recursive' => false,
'associations' => 'true',
'hide-private' => true,
'hide-protected' => 0,
'hide-attributes' => 1,
'hide-methods' => 'true',
'hide-empty-blocks' => '',
'theme' => 'phuml',
]);
$builder = new DigraphBuilder($configuration);
$directory = new CodebaseDirectory(__DIR__ . '/../../resources/.code/interfaces/processor');

$codeFinder = $builder->codeFinder($directory);

$this->assertCount(1, $codeFinder->files());
}

/** @test */
function it_builds_a_parser_that_excludes_attributes()
{
$configuration = new DigraphConfiguration([
'recursive' => 0,
'associations' => 'true',
'hide-private' => true,
'hide-protected' => 0,
'hide-attributes' => true,
'hide-methods' => 'true',
'hide-empty-blocks' => '',
'theme' => 'phuml',
]);
$builder = new DigraphBuilder($configuration);
$directory = new CodebaseDirectory(__DIR__ . '/../../resources/.code/interfaces/processor/graphviz');
$finder = $builder->codeFinder($directory);

$parser = $builder->codeParser();

$codebase = $parser->parse($finder);

$this->assertCount(1, $codebase->definitions());
$className = new Name('plGraphvizProcessorStyle');
$this->assertTrue($codebase->has($className));
$this->assertFalse($codebase->get($className)->hasAttributes());
}

/** @test */
function it_builds_a_parser_that_includes_attributes()
{
$configuration = new DigraphConfiguration([
'recursive' => 0,
'associations' => 'true',
'hide-private' => 0,
'hide-protected' => 0,
'hide-attributes' => false,
'hide-methods' => 'true',
'hide-empty-blocks' => '',
'theme' => 'phuml',
]);
$builder = new DigraphBuilder($configuration);
$directory = new CodebaseDirectory(__DIR__ . '/../../resources/.code/interfaces/processor/graphviz');
$finder = $builder->codeFinder($directory);

$parser = $builder->codeParser();

$codebase = $parser->parse($finder);

$this->assertCount(1, $codebase->definitions());
$className = new Name('plGraphvizProcessorStyle');
$this->assertTrue($codebase->has($className));
$this->assertTrue($codebase->get($className)->hasAttributes());
}

/** @test */
function it_builds_a_parser_that_excludes_methods()
{
$configuration = new DigraphConfiguration([
'recursive' => 0,
'associations' => 'true',
'hide-private' => [],
'hide-protected' => 0,
'hide-attributes' => false,
'hide-methods' => 'true',
'hide-empty-blocks' => '',
'theme' => 'phuml',
]);
$builder = new DigraphBuilder($configuration);
$directory = new CodebaseDirectory(__DIR__ . '/../../resources/.code/interfaces/processor/graphviz');
$finder = $builder->codeFinder($directory);

$parser = $builder->codeParser();

$codebase = $parser->parse($finder);

$this->assertCount(1, $codebase->definitions());
$className = new Name('plGraphvizProcessorStyle');
$this->assertTrue($codebase->has($className));
$this->assertEmpty($codebase->get($className)->methods());
}

/** @test */
function it_builds_a_parser_that_includes_methods()
{
$configuration = new DigraphConfiguration([
'recursive' => 0,
'associations' => 'true',
'hide-private' => [],
'hide-protected' => 0,
'hide-attributes' => null,
'hide-methods' => false,
'hide-empty-blocks' => '',
'theme' => 'phuml',
]);
$builder = new DigraphBuilder($configuration);
$directory = new CodebaseDirectory(__DIR__ . '/../../resources/.code/interfaces/processor/graphviz');
$finder = $builder->codeFinder($directory);

$parser = $builder->codeParser();

$codebase = $parser->parse($finder);

$this->assertCount(1, $codebase->definitions());
$className = new Name('plGraphvizProcessorStyle');
$this->assertTrue($codebase->has($className));
$this->assertCount(1, $codebase->get($className)->methods());
}

/** @test */
function it_builds_a_parser_that_excludes_protected_members()
{
$configuration = new DigraphConfiguration([
'recursive' => 0,
'associations' => 'true',
'hide-private' => [],
'hide-protected' => true,
'hide-attributes' => false,
'hide-methods' => 'true',
'hide-empty-blocks' => '',
'theme' => 'phuml',
]);
$builder = new DigraphBuilder($configuration);
$directory = new CodebaseDirectory(__DIR__ . '/../../resources/.code/exceptions/base');
$finder = $builder->codeFinder($directory);

$parser = $builder->codeParser();

$codebase = $parser->parse($finder);

$this->assertCount(2, $codebase->definitions());
$className = new Name('plBasePropertyException');
$this->assertTrue($codebase->has($className));
/** @var ClassDefinition $definition */
$definition = $codebase->get($className);
$this->assertCount(3, $definition->constants());
}

/** @test */
function it_builds_a_parser_that_includes_protected_members()
{
$configuration = new DigraphConfiguration([
'recursive' => 0,
'associations' => 'true',
'hide-private' => [],
'hide-protected' => false,
'hide-attributes' => null,
'hide-methods' => 'true',
'hide-empty-blocks' => '',
'theme' => 'phuml',
]);
$builder = new DigraphBuilder($configuration);
$directory = new CodebaseDirectory(__DIR__ . '/../../resources/.code/exceptions/base');
$finder = $builder->codeFinder($directory);

$parser = $builder->codeParser();

$codebase = $parser->parse($finder);

$this->assertCount(2, $codebase->definitions());
$className = new Name('plBasePropertyException');
$this->assertTrue($codebase->has($className));
/** @var ClassDefinition $definition */
$definition = $codebase->get($className);
$this->assertCount(4, $definition->constants());
}

/** @test */
function it_builds_a_parser_that_includes_relationships_from_attributes_and_constructor_parameters()
{
$configuration = new DigraphConfiguration([
'recursive' => 0,
'associations' => true,
'hide-private' => 0,
'hide-protected' => null,
'hide-attributes' => [],
'hide-methods' => false,
'hide-empty-blocks' => '',
'theme' => 'phuml',
]);
$builder = new DigraphBuilder($configuration);
$directory = new CodebaseDirectory(__DIR__ . '/../../resources/.code/classes/processor/graphviz/style');
$finder = $builder->codeFinder($directory);

$parser = $builder->codeParser();

$codebase = $parser->parse($finder);

$this->assertCount(3, $codebase->definitions());
$this->assertTrue($codebase->has(new Name('plStyleName')));
}

/** @test */
function it_builds_a_parser_that_excludes_relationships_from_attributes_and_constructor_parameters()
{
$configuration = new DigraphConfiguration([
'recursive' => 0,
'associations' => false,
'hide-private' => 0,
'hide-protected' => null,
'hide-attributes' => [],
'hide-methods' => false,
'hide-empty-blocks' => '',
'theme' => 'phuml',
]);
$builder = new DigraphBuilder($configuration);
$directory = new CodebaseDirectory(__DIR__ . '/../../resources/.code/classes/processor/graphviz/style');
$finder = $builder->codeFinder($directory);

$parser = $builder->codeParser();

$codebase = $parser->parse($finder);

$this->assertCount(2, $codebase->definitions());
$this->assertFalse($codebase->has(new Name('plStyleName')));
}
}
23 changes: 23 additions & 0 deletions tests/unit/Configuration/DigraphConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ function it_knows_which_theme_has_been_set()
$this->assertEquals('php', $configuration->theme()->name());
}

/** @test */
function it_casts_to_bool_all_options_but_theme()
{
$options = $this->options([
'recursive' => 0,
'associations' => 'true',
'hide-private' => 1,
'hide-protected' => [],
'hide-attributes' => '',
'hide-methods' => 'true',
'hide-empty-blocks' => null,
]);
$configuration = new DigraphConfiguration($options);

$this->assertFalse($configuration->searchRecursively());
$this->assertTrue($configuration->extractAssociations());
$this->assertTrue($configuration->hidePrivate());
$this->assertFalse($configuration->hideProtected());
$this->assertFalse($configuration->hideAttributes());
$this->assertTrue($configuration->hideMethods());
$this->assertFalse($configuration->hideEmptyBlocks());
}

private function options(array $override)
{
return array_merge([
Expand Down
Loading

0 comments on commit e402155

Please sign in to comment.