1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace PhpSchool\LearnYouPhpTest\Exercise; |
5
|
|
|
|
6
|
|
|
use Faker\Factory; |
7
|
|
|
use Faker\Generator; |
8
|
|
|
use PhpSchool\LearnYouPhp\Exercise\ArrayWeGo; |
9
|
|
|
use PhpSchool\PhpWorkshop\Solution\SolutionInterface; |
10
|
|
|
use PHPUnit_Framework_TestCase; |
11
|
|
|
use PhpSchool\LearnYouPhp\Exercise\MyFirstIo; |
12
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ArrayWeGoTest |
16
|
|
|
* @package PhpSchool\LearnYouPhpTest\Exercise |
17
|
|
|
* @author Michael Woodward <[email protected]> |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
class ArrayWeGoTest extends PHPUnit_Framework_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
|
|
|
|
39
|
|
|
public function testArrWeGoExercise() |
40
|
|
|
{ |
41
|
|
|
$e = new ArrayWeGo($this->filesystem, $this->faker); |
42
|
|
|
$this->assertEquals('Array We Go!', $e->getName()); |
43
|
|
|
$this->assertEquals('Filter an array of file paths and map to SplFile objects', $e->getDescription()); |
44
|
|
|
|
45
|
|
|
$this->assertInstanceOf(SolutionInterface::class, $e->getSolution()); |
46
|
|
|
$this->assertFileExists(realpath($e->getProblem())); |
47
|
|
|
$this->assertNull($e->tearDown()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testGetArgsCreateAtLeastOneExistingFile() |
51
|
|
|
{ |
52
|
|
|
$e = new ArrayWeGo($this->filesystem, $this->faker); |
53
|
|
|
$args = $e->getArgs(); |
54
|
|
|
|
55
|
|
|
$existingFiles = array_filter($args, 'file_exists'); |
56
|
|
|
|
57
|
|
|
foreach ($existingFiles as $file) { |
58
|
|
|
$this->assertFileExists($file); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->assertGreaterThanOrEqual(1, count($existingFiles)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testGetArgsHasAtLeastOneNonExistingFile() |
65
|
|
|
{ |
66
|
|
|
$e = new ArrayWeGo($this->filesystem, $this->faker); |
67
|
|
|
$args = $e->getArgs(); |
68
|
|
|
|
69
|
|
|
$nonExistingFiles = array_filter($args, function ($arg) { |
70
|
|
|
return !file_exists($arg); |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
foreach ($nonExistingFiles as $file) { |
74
|
|
|
$this->assertFileNotExists($file); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->assertGreaterThanOrEqual(1, count($nonExistingFiles)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testTearDownRemovesFile() |
81
|
|
|
{ |
82
|
|
|
$e = new ArrayWeGo($this->filesystem, $this->faker); |
83
|
|
|
$args = $e->getArgs(); |
84
|
|
|
|
85
|
|
|
$existingFiles = array_filter($args, 'file_exists'); |
86
|
|
|
|
87
|
|
|
$this->assertFileExists($existingFiles[0]); |
88
|
|
|
|
89
|
|
|
$e->tearDown(); |
90
|
|
|
|
91
|
|
|
$this->assertFileNotExists($existingFiles[0]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testFunctionRequirements() |
95
|
|
|
{ |
96
|
|
|
$e = new ArrayWeGo($this->filesystem, $this->faker); |
97
|
|
|
$this->assertEquals(['array_shift', 'array_filter', 'array_map'], $e->getRequiredFunctions()); |
98
|
|
|
$this->assertEquals(['basename'], $e->getBannedFunctions()); |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|