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