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