Completed
Push — master ( 6d39a6...93f6e0 )
by Aydin
13:49 queued 07:20
created

testCheckReturnsFailureIfNoIncludeFoundInSolution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6667
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace PhpSchool\LearnYouPhpTest\Exercise;
4
5
use Faker\Factory;
6
use PhpParser\Parser;
7
use PhpParser\ParserFactory;
8
use PhpSchool\LearnYouPhp\Exercise\ConcernedAboutSeparation;
9
use PhpSchool\PhpWorkshop\Result\Failure;
10
use PhpSchool\PhpWorkshop\Result\FailureInterface;
11
use PhpSchool\PhpWorkshop\Result\Success;
12
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
13
use PHPUnit_Framework_TestCase;
14
use Symfony\Component\Filesystem\Filesystem;
15
16
/**
17
 * Class ConcernedAboutSeparationTest
18
 * @package PhpSchool\LearnYouPhpTest\Exercise
19
 * @author Aydin Hassan <[email protected]>
20
 */
21
class ConcernedAboutSeparationTest extends PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var Filesystem
25
     */
26
    private $filesystem;
27
28
    /**
29
     * @var Generator
30
     */
31
    private $faker;
32
33
    /**
34
     * @var Parser
35
     */
36
    private $parser;
37
38
    public function setUp()
39
    {
40
        $this->filesystem = new Filesystem;
41
        $this->faker = Factory::create();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Faker\Factory::create() of type object<Faker\Generator> is incompatible with the declared type object<PhpSchool\LearnYo...est\Exercise\Generator> of property $faker.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
        $this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
43
    }
44
45
    public function testConcernedAboutSeparationExercise()
46
    {
47
        $e = new ConcernedAboutSeparation($this->filesystem, $this->faker, $this->parser);
48
        $this->assertEquals('Concerned about Separation?', $e->getName());
49
        $this->assertEquals('Separate code and utilise files and classes', $e->getDescription());
50
51
        $this->assertInstanceOf(SolutionInterface::class, $e->getSolution());
52
        $this->assertFileExists(realpath($e->getProblem()));
53
        $this->assertNull($e->tearDown());
54
    }
55
56 View Code Duplication
    public function testGetArgsCreatesFilesAndReturnsRandomExt()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        $e = new ConcernedAboutSeparation($this->filesystem, $this->faker, $this->parser);
59
        $args = $e->getArgs();
60
        $path = $args[0];
61
        $this->assertFileExists($path);
62
63
        $files = [
64
            "learnyouphp.dat",
65
            "learnyouphp.txt",
66
            "learnyouphp.sql",
67
            "api.html",
68
            "README.md",
69
            "CHANGELOG.md",
70
            "LICENCE.md",
71
            "md",
72
            "data.json",
73
            "data.dat",
74
            "words.dat",
75
            "w00t.dat",
76
            "w00t.txt",
77
            "wrrrrongdat",
78
            "dat",
79
        ];
80
81
        array_walk($files, function ($file) use ($path) {
82
            $this->assertFileExists(sprintf('%s/%s', $path, $file));
83
        });
84
85
        $extensions = array_unique(array_map(function ($file) {
86
            return pathinfo($file, PATHINFO_EXTENSION);
87
        }, $files));
88
89
        $this->assertTrue(in_array($args[1], $extensions));
90
    }
91
92
    public function testTearDownRemovesFile()
93
    {
94
        $e = new ConcernedAboutSeparation($this->filesystem, $this->faker, $this->parser);
95
        $args = $e->getArgs();
96
        $path = $args[0];
97
        $this->assertFileExists($path);
98
99
        $e->tearDown();
100
101
        $this->assertFileNotExists($path);
102
    }
103
104 View Code Duplication
    public function testCheckReturnsFailureIfNoIncludeFoundInSolution()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        $e = new ConcernedAboutSeparation($this->filesystem, $this->faker, $this->parser);
107
        $failure = $e->check(__DIR__ . '/../res/concerned-about-separation/no-include.php');
108
        
109
        $this->assertInstanceOf(Failure::class, $failure);
110
        $this->assertEquals('No require statement found', $failure->getReason());
0 ignored issues
show
Bug introduced by
The method getReason does only exist in PhpSchool\PhpWorkshop\Result\Failure, but not in PhpSchool\PhpWorkshop\Result\Success.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
111
        $this->assertEquals('Concerned about Separation?', $failure->getCheckName());
112
    }
113
114 View Code Duplication
    public function testCheckReturnsSuccessIfIncludeFound()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116
        $e = new ConcernedAboutSeparation($this->filesystem, $this->faker, $this->parser);
117
        $success = $e->check(__DIR__ . '/../res/concerned-about-separation/include.php');
118
119
        $this->assertInstanceOf(Success::class, $success);
120
        $this->assertEquals('Concerned about Separation?', $success->getCheckName());
121
    }
122
}
123