|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace PhpSchool\LearnYouPhpTest\Exercise; |
|
5
|
|
|
|
|
6
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
|
7
|
|
|
use PhpSchool\PhpWorkshop\Solution\SolutionInterface; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use PhpSchool\LearnYouPhp\Exercise\FilteredLs; |
|
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Aydin Hassan <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class FilteredLsTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Filesystem |
|
20
|
|
|
*/ |
|
21
|
|
|
private $filesystem; |
|
22
|
|
|
|
|
23
|
|
|
public function setUp() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->filesystem = new Filesystem; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testFilteredLsExercise() |
|
29
|
|
|
{ |
|
30
|
|
|
$e = new FilteredLs($this->filesystem); |
|
31
|
|
|
$this->assertEquals('Filtered LS', $e->getName()); |
|
32
|
|
|
$this->assertEquals('Read files in a folder and filter by a given extension', $e->getDescription()); |
|
33
|
|
|
$this->assertEquals(ExerciseType::CLI, $e->getType()); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertInstanceOf(SolutionInterface::class, $e->getSolution()); |
|
36
|
|
|
$this->assertFileExists(realpath($e->getProblem())); |
|
37
|
|
|
$this->assertNull($e->tearDown()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testGetArgsCreatesFilesAndReturnsRandomExt() |
|
41
|
|
|
{ |
|
42
|
|
|
$e = new FilteredLs($this->filesystem); |
|
43
|
|
|
$args = $e->getArgs(); |
|
44
|
|
|
$path = $args[0]; |
|
45
|
|
|
$this->assertFileExists($path); |
|
46
|
|
|
|
|
47
|
|
|
$files = [ |
|
48
|
|
|
"learnyouphp.dat", |
|
49
|
|
|
"learnyouphp.txt", |
|
50
|
|
|
"learnyouphp.sql", |
|
51
|
|
|
"api.html", |
|
52
|
|
|
"README.md", |
|
53
|
|
|
"CHANGELOG.md", |
|
54
|
|
|
"LICENCE.md", |
|
55
|
|
|
"md", |
|
56
|
|
|
"data.json", |
|
57
|
|
|
"data.dat", |
|
58
|
|
|
"words.dat", |
|
59
|
|
|
"w00t.dat", |
|
60
|
|
|
"w00t.txt", |
|
61
|
|
|
"wrrrrongdat", |
|
62
|
|
|
"dat", |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
array_walk($files, function ($file) use ($path) { |
|
66
|
|
|
$this->assertFileExists(sprintf('%s/%s', $path, $file)); |
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
$extensions = array_unique(array_map(function ($file) { |
|
70
|
|
|
return pathinfo($file, PATHINFO_EXTENSION); |
|
71
|
|
|
}, $files)); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertTrue(in_array($args[1], $extensions)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testTearDownRemovesFile() |
|
77
|
|
|
{ |
|
78
|
|
|
$e = new FilteredLs($this->filesystem); |
|
79
|
|
|
$args = $e->getArgs(); |
|
80
|
|
|
$path = $args[0]; |
|
81
|
|
|
$this->assertFileExists($path); |
|
82
|
|
|
|
|
83
|
|
|
$e->tearDown(); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertFileNotExists($path); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|