MyFirstIoTest::testConfigure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace PhpSchool\LearnYouPhpTest\Exercise;
5
6
use Faker\Factory;
7
use Faker\Generator;
8
use PhpSchool\PhpWorkshop\Check\FunctionRequirementsCheck;
9
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
10
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
11
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
12
use PHPUnit\Framework\TestCase;
13
use PhpSchool\LearnYouPhp\Exercise\MyFirstIo;
14
use Symfony\Component\Filesystem\Filesystem;
15
16
/**
17
 * @author Aydin Hassan <[email protected]>
18
 */
19
class MyFirstIoTest 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 testMyFirstIoExercise()
39
    {
40
        $e = new MyFirstIo($this->filesystem, $this->faker);
41
        $this->assertEquals('My First IO', $e->getName());
42
        $this->assertEquals('Read a file from the file system', $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 testGetArgsCreatesFileWithRandomContentFromFake()
51
    {
52
53
        $e = new MyFirstIo($this->filesystem, $this->faker);
54
        $args = $e->getArgs();
55
        $path = $args[0];
56
        $this->assertFileExists($path);
57
58
        $content1 = file_get_contents($path);
59
        unlink($path);
60
61
        $args = $e->getArgs();
62
        $path = $args[0];
63
        $this->assertFileExists($path);
64
65
        $content2 = file_get_contents($path);
66
        $this->assertNotEquals($content1, $content2);
67
    }
68
69
    public function testTearDownRemovesFile()
70
    {
71
        $e = new MyFirstIo($this->filesystem, $this->faker);
72
        $args = $e->getArgs();
73
        $path = $args[0];
74
        $this->assertFileExists($path);
75
76
        $e->tearDown();
77
78
        $this->assertFileNotExists($path);
79
    }
80
81
    public function testFunctionRequirements()
82
    {
83
        $e = new MyFirstIo($this->filesystem, $this->faker);
84
        $this->assertEquals(['file_get_contents'], $e->getRequiredFunctions());
85
        $this->assertEquals(['file'], $e->getBannedFunctions());
86
    }
87
88
    public function testConfigure()
89
    {
90
        $dispatcher = $this->getMockBuilder(ExerciseDispatcher::class)
91
            ->disableOriginalConstructor()
92
            ->getMock();
93
94
        $dispatcher
95
            ->expects($this->once())
96
            ->method('requireCheck')
97
            ->with(FunctionRequirementsCheck::class);
98
99
        $e = new MyFirstIo($this->filesystem, $this->faker);
100
        $e->configure($dispatcher);
101
    }
102
}
103