ConcernedAboutSeparationTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 107
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testConcernedAboutSeparationExercise() 0 11 1
A testTearDownRemovesFile() 0 11 1
A testGetArgsCreatesFilesAndReturnsRandomExt() 0 35 1
A testCheckReturnsFailureIfNoIncludeFoundInSolution() 0 11 1
A testCheckReturnsSuccessIfIncludeFound() 0 10 1
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\Exercise\ExerciseType;
10
use PhpSchool\PhpWorkshop\Input\Input;
11
use PhpSchool\PhpWorkshop\Result\Failure;
12
use PhpSchool\PhpWorkshop\Result\Success;
13
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
14
use PHPUnit\Framework\TestCase;
15
use Symfony\Component\Filesystem\Filesystem;
16
17
/**
18
 * @author Aydin Hassan <[email protected]>
19
 */
20
class ConcernedAboutSeparationTest extends TestCase
21
{
22
    /**
23
     * @var Filesystem
24
     */
25
    private $filesystem;
26
27
    /**
28
     * @var Generator
29
     */
30
    private $faker;
31
32
    /**
33
     * @var Parser
34
     */
35
    private $parser;
36
37
    public function setUp()
38
    {
39
        $this->filesystem = new Filesystem;
40
        $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...
41
        $this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
42
    }
43
44
    public function testConcernedAboutSeparationExercise()
45
    {
46
        $e = new ConcernedAboutSeparation($this->filesystem, $this->faker, $this->parser);
47
        $this->assertEquals('Concerned about Separation?', $e->getName());
48
        $this->assertEquals('Separate code and utilise files and classes', $e->getDescription());
49
        $this->assertEquals(ExerciseType::CLI, $e->getType());
50
51
        $this->assertInstanceOf(SolutionInterface::class, $e->getSolution());
52
        $this->assertFileExists(realpath($e->getProblem()));
53
        $this->assertNull($e->tearDown());
54
    }
55
56
    public function testGetArgsCreatesFilesAndReturnsRandomExt()
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
    public function testCheckReturnsFailureIfNoIncludeFoundInSolution()
105
    {
106
        $e = new ConcernedAboutSeparation($this->filesystem, $this->faker, $this->parser);
107
        $failure = $e->check(
108
            new Input('learnyouphp', ['program' => __DIR__ . '/../res/concerned-about-separation/no-include.php'])
109
        );
110
        
111
        $this->assertInstanceOf(Failure::class, $failure);
112
        $this->assertEquals('No require statement found', $failure->getReason());
113
        $this->assertEquals('Concerned about Separation?', $failure->getCheckName());
114
    }
115
116
    public function testCheckReturnsSuccessIfIncludeFound()
117
    {
118
        $e = new ConcernedAboutSeparation($this->filesystem, $this->faker, $this->parser);
119
        $success = $e->check(
120
            new Input('learnyouphp', ['program' => __DIR__ . '/../res/concerned-about-separation/include.php'])
121
        );
122
123
        $this->assertInstanceOf(Success::class, $success);
124
        $this->assertEquals('Concerned about Separation?', $success->getCheckName());
125
    }
126
}
127