ExceptionalCodingTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 97
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testArrWeGoExercise() 0 11 1
A testGetArgsCreateAtleastOneExistingFile() 0 13 2
A testGetArgsHasAtleastOneNonExistingFile() 0 15 2
A testTearDownRemovesFile() 0 13 1
A testFunctionRequirements() 0 6 1
A testConfigure() 0 14 1
1
<?php
2
3
4
namespace PhpSchool\LearnYouPhpTest\Exercise;
5
6
use Faker\Factory;
7
use Faker\Generator;
8
use PhpSchool\LearnYouPhp\Exercise\ExceptionalCoding;
9
use PhpSchool\PhpWorkshop\Check\FunctionRequirementsCheck;
10
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
11
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
12
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
13
use PHPUnit\Framework\TestCase;
14
use Symfony\Component\Filesystem\Filesystem;
15
16
/**
17
 * @author Michael Woodward <[email protected]>
18
 */
19
class ExceptionalCodingTest extends TestCase
20
{
21
22
    /**
23
     * @var Generator
24
     */
25
    private $faker;
26
27
    /**
28
     * @var Filesystem
29
     */
30
    private $filesystem;
31
32
    public function setUp()
33
    {
34
        $this->faker = Factory::create();
35
        $this->filesystem = new Filesystem;
36
    }
37
38
    public function testArrWeGoExercise()
39
    {
40
        $e = new ExceptionalCoding($this->filesystem, $this->faker);
41
        $this->assertEquals('Exceptional Coding', $e->getName());
42
        $this->assertEquals('Introduction to Exceptions', $e->getDescription());
43
        $this->assertEquals(ExerciseType::CLI, $e->getType());
44
45
        $this->assertInstanceOf(SolutionInterface::class, $e->getSolution());
46
        $this->assertFileExists(realpath($e->getProblem()));
47
        $this->assertNull($e->tearDown());
48
    }
49
50
    public function testGetArgsCreateAtleastOneExistingFile()
51
    {
52
        $e = new ExceptionalCoding($this->filesystem, $this->faker);
53
        $args = $e->getArgs();
54
55
        $existingFiles = array_filter($args, 'file_exists');
56
57
        foreach ($existingFiles as $file) {
58
            $this->assertFileExists($file);
59
        }
60
61
        $this->assertGreaterThanOrEqual(1, count($existingFiles));
62
    }
63
64
    public function testGetArgsHasAtleastOneNonExistingFile()
65
    {
66
        $e = new ExceptionalCoding($this->filesystem, $this->faker);
67
        $args = $e->getArgs();
68
69
        $nonExistingFiles = array_filter($args, function ($arg) {
70
            return !file_exists($arg);
71
        });
72
73
        foreach ($nonExistingFiles as $file) {
74
            $this->assertFileNotExists($file);
75
        }
76
77
        $this->assertGreaterThanOrEqual(1, count($nonExistingFiles));
78
    }
79
80
    public function testTearDownRemovesFile()
81
    {
82
        $e = new ExceptionalCoding($this->filesystem, $this->faker);
83
        $args = $e->getArgs();
84
85
        $existingFiles = array_filter($args, 'file_exists');
86
87
        $this->assertFileExists($existingFiles[0]);
88
89
        $e->tearDown();
90
91
        $this->assertFileNotExists($existingFiles[0]);
92
    }
93
94
    public function testFunctionRequirements()
95
    {
96
        $e = new ExceptionalCoding($this->filesystem, $this->faker);
97
        $this->assertEquals([], $e->getRequiredFunctions());
98
        $this->assertEquals(['array_filter', 'file_exists'], $e->getBannedFunctions());
99
    }
100
101
    public function testConfigure()
102
    {
103
        $dispatcher = $this->getMockBuilder(ExerciseDispatcher::class)
104
            ->disableOriginalConstructor()
105
            ->getMock();
106
107
        $dispatcher
108
            ->expects($this->once())
109
            ->method('requireCheck')
110
            ->with(FunctionRequirementsCheck::class);
111
112
        $e = new ExceptionalCoding($this->filesystem, $this->faker);
113
        $e->configure($dispatcher);
114
    }
115
}
116