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
|
|
|
* Class MyFirstIoTest |
18
|
|
|
* @package PhpSchool\LearnYouPhpTest\Exercise |
19
|
|
|
* @author Aydin Hassan <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class MyFirstIoTest extends PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Generator |
26
|
|
|
*/ |
27
|
|
|
private $faker; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Filesystem |
31
|
|
|
*/ |
32
|
|
|
private $filesystem; |
33
|
|
|
|
34
|
|
|
public function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->faker = Factory::create(); |
37
|
|
|
$this->filesystem = new Filesystem; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testMyFirstIoExercise() |
41
|
|
|
{ |
42
|
|
|
$e = new MyFirstIo($this->filesystem, $this->faker); |
43
|
|
|
$this->assertEquals('My First IO', $e->getName()); |
44
|
|
|
$this->assertEquals('Read a file from the file system', $e->getDescription()); |
45
|
|
|
$this->assertEquals(ExerciseType::CLI, $e->getType()); |
46
|
|
|
|
47
|
|
|
$this->assertInstanceOf(SolutionInterface::class, $e->getSolution()); |
48
|
|
|
$this->assertFileExists(realpath($e->getProblem())); |
49
|
|
|
$this->assertNull($e->tearDown()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testGetArgsCreatesFileWithRandomContentFromFake() |
53
|
|
|
{ |
54
|
|
|
|
55
|
|
|
$e = new MyFirstIo($this->filesystem, $this->faker); |
56
|
|
|
$args = $e->getArgs(); |
57
|
|
|
$path = $args[0]; |
58
|
|
|
$this->assertFileExists($path); |
59
|
|
|
|
60
|
|
|
$content1 = file_get_contents($path); |
61
|
|
|
unlink($path); |
62
|
|
|
|
63
|
|
|
$args = $e->getArgs(); |
64
|
|
|
$path = $args[0]; |
65
|
|
|
$this->assertFileExists($path); |
66
|
|
|
|
67
|
|
|
$content2 = file_get_contents($path); |
68
|
|
|
$this->assertNotEquals($content1, $content2); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testTearDownRemovesFile() |
72
|
|
|
{ |
73
|
|
|
$e = new MyFirstIo($this->filesystem, $this->faker); |
74
|
|
|
$args = $e->getArgs(); |
75
|
|
|
$path = $args[0]; |
76
|
|
|
$this->assertFileExists($path); |
77
|
|
|
|
78
|
|
|
$e->tearDown(); |
79
|
|
|
|
80
|
|
|
$this->assertFileNotExists($path); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testFunctionRequirements() |
84
|
|
|
{ |
85
|
|
|
$e = new MyFirstIo($this->filesystem, $this->faker); |
86
|
|
|
$this->assertEquals(['file_get_contents'], $e->getRequiredFunctions()); |
87
|
|
|
$this->assertEquals(['file'], $e->getBannedFunctions()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testConfigure() |
91
|
|
|
{ |
92
|
|
|
$dispatcher = $this->getMockBuilder(ExerciseDispatcher::class) |
93
|
|
|
->disableOriginalConstructor() |
94
|
|
|
->getMock(); |
95
|
|
|
|
96
|
|
|
$dispatcher |
97
|
|
|
->expects($this->once()) |
98
|
|
|
->method('requireCheck') |
99
|
|
|
->with(FunctionRequirementsCheck::class); |
100
|
|
|
|
101
|
|
|
$e = new MyFirstIo($this->filesystem, $this->faker); |
102
|
|
|
$e->configure($dispatcher); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|