1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpSchool\LearnYouPhpTest\Exercise; |
4
|
|
|
|
5
|
|
|
use Faker\Factory; |
6
|
|
|
use Faker\Generator; |
7
|
|
|
use PhpSchool\LearnYouPhp\Exercise\DependencyHeaven; |
8
|
|
|
use PhpSchool\PhpWorkshop\Check\ComposerCheck; |
9
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
10
|
|
|
use PhpSchool\PhpWorkshop\ExerciseDispatcher; |
11
|
|
|
use PhpSchool\PhpWorkshop\Solution\SolutionInterface; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Psr\Http\Message\RequestInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Michael Woodward <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class DependencyHeavenTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Generator |
23
|
|
|
*/ |
24
|
|
|
private $faker; |
25
|
|
|
|
26
|
|
|
public function setUp() |
27
|
|
|
{ |
28
|
|
|
$this->faker = Factory::create('Fr_fr'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testDependencyHeavenExercise() |
32
|
|
|
{ |
33
|
|
|
$e = new DependencyHeaven($this->faker); |
34
|
|
|
$this->assertEquals('Dependency Heaven', $e->getName()); |
35
|
|
|
$this->assertEquals('An introduction to Composer dependency management', $e->getDescription()); |
36
|
|
|
$this->assertEquals(ExerciseType::CGI, $e->getType()); |
37
|
|
|
|
38
|
|
|
$this->assertInstanceOf(SolutionInterface::class, $e->getSolution()); |
39
|
|
|
$this->assertFileExists(realpath($e->getSolution()->getEntryPoint())); |
40
|
|
|
$this->assertFileExists(realpath($e->getProblem())); |
41
|
|
|
$this->assertNull($e->tearDown()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testGetRequiredPackages() |
45
|
|
|
{ |
46
|
|
|
$this->assertSame( |
47
|
|
|
['klein/klein', 'danielstjules/stringy'], |
48
|
|
|
(new DependencyHeaven($this->faker))->getRequiredPackages() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testGetRequests() |
53
|
|
|
{ |
54
|
|
|
$e = new DependencyHeaven($this->faker); |
55
|
|
|
|
56
|
|
|
$requests = $e->getRequests(); |
57
|
|
|
|
58
|
|
|
foreach ($requests as $request) { |
59
|
|
|
$this->assertInstanceOf(RequestInterface::class, $request); |
60
|
|
|
$this->assertSame('POST', $request->getMethod()); |
61
|
|
|
$this->assertSame(['application/x-www-form-urlencoded'], $request->getHeader('Content-Type')); |
62
|
|
|
$this->assertNotEmpty($request->getBody()); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testGetRequestsReturnsMultipleRequestsForEachEndpoint() |
67
|
|
|
{ |
68
|
|
|
$e = new DependencyHeaven($this->faker); |
69
|
|
|
|
70
|
|
|
$endPoints = array_map(function (RequestInterface $request) { |
71
|
|
|
return $request->getUri()->getPath(); |
72
|
|
|
}, $e->getRequests()); |
73
|
|
|
|
74
|
|
|
$counts = array_count_values($endPoints); |
75
|
|
|
foreach (['/reverse', '/swapcase', '/titleize'] as $endPoint) { |
76
|
|
|
$this->assertTrue(isset($counts[$endPoint])); |
77
|
|
|
$this->assertGreaterThan(1, $counts[$endPoint]); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testConfigure() |
82
|
|
|
{ |
83
|
|
|
$dispatcher = $this->getMockBuilder(ExerciseDispatcher::class) |
84
|
|
|
->disableOriginalConstructor() |
85
|
|
|
->getMock(); |
86
|
|
|
|
87
|
|
|
$dispatcher |
88
|
|
|
->expects($this->once()) |
89
|
|
|
->method('requireCheck') |
90
|
|
|
->with(ComposerCheck::class); |
91
|
|
|
|
92
|
|
|
$e = new DependencyHeaven($this->faker); |
93
|
|
|
$e->configure($dispatcher); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|