|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace PhpSchool\LearnYouPhpTest\Exercise; |
|
5
|
|
|
|
|
6
|
|
|
use Colors\Color; |
|
7
|
|
|
use Faker\Factory; |
|
8
|
|
|
use Faker\Generator; |
|
9
|
|
|
use Hoa\Core\Exception\Exception; |
|
10
|
|
|
use Hoa\Socket\Client; |
|
11
|
|
|
use PhpSchool\CliMenu\Terminal\TerminalInterface; |
|
12
|
|
|
use PhpSchool\CliMenu\Terminal\UnixTerminal; |
|
13
|
|
|
use PhpSchool\LearnYouPhp\Exercise\ArrayWeGo; |
|
14
|
|
|
use PhpSchool\LearnYouPhp\Exercise\TimeServer; |
|
15
|
|
|
use PhpSchool\LearnYouPhp\TcpSocketFactory; |
|
16
|
|
|
use PhpSchool\PhpWorkshop\Check\CheckRepository; |
|
17
|
|
|
use PhpSchool\PhpWorkshop\Event\EventDispatcher; |
|
18
|
|
|
use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
|
19
|
|
|
use PhpSchool\PhpWorkshop\ExerciseDispatcher; |
|
20
|
|
|
use PhpSchool\PhpWorkshop\Factory\RunnerFactory; |
|
21
|
|
|
use PhpSchool\PhpWorkshop\Output\StdOutput; |
|
22
|
|
|
use PhpSchool\PhpWorkshop\Result\Failure; |
|
23
|
|
|
use PhpSchool\PhpWorkshop\Result\StdOutFailure; |
|
24
|
|
|
use PhpSchool\PhpWorkshop\Result\Success; |
|
25
|
|
|
use PhpSchool\PhpWorkshop\ResultAggregator; |
|
26
|
|
|
use PhpSchool\PhpWorkshop\Solution\SolutionInterface; |
|
27
|
|
|
use PHPUnit_Framework_TestCase; |
|
28
|
|
|
use PhpSchool\LearnYouPhp\Exercise\MyFirstIo; |
|
29
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Class TimeServerTest |
|
33
|
|
|
* @package PhpSchool\LearnYouPhpTest\Exercise |
|
34
|
|
|
* @author Michael Woodward <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
class TimeServerTest extends PHPUnit_Framework_TestCase |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var TimeServer |
|
41
|
|
|
*/ |
|
42
|
|
|
private $exercise; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var ExerciseDispatcher |
|
46
|
|
|
*/ |
|
47
|
|
|
private $exerciseDispatcher; |
|
48
|
|
|
|
|
49
|
|
|
public function setUp() |
|
50
|
|
|
{ |
|
51
|
|
|
$results = new ResultAggregator; |
|
52
|
|
|
$this->exerciseDispatcher = new ExerciseDispatcher( |
|
53
|
|
|
new RunnerFactory, |
|
54
|
|
|
$results, |
|
55
|
|
|
new EventDispatcher($results), |
|
56
|
|
|
new CheckRepository([]) |
|
57
|
|
|
); |
|
58
|
|
|
$this->exercise = new TimeServer(new TcpSocketFactory); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testGetters() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->assertEquals('Time Server', $this->exercise->getName()); |
|
64
|
|
|
$this->assertEquals('Build a Time Server!', $this->exercise->getDescription()); |
|
65
|
|
|
$this->assertEquals(ExerciseType::CLI, $this->exercise->getType()); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertInstanceOf(SolutionInterface::class, $this->exercise->getSolution()); |
|
68
|
|
|
$this->assertFileExists(realpath($this->exercise->getProblem())); |
|
69
|
|
|
$this->assertNull($this->exercise->tearDown()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testFailureIsReturnedIfCannotConnect() |
|
73
|
|
|
{ |
|
74
|
|
|
$results = $this->exerciseDispatcher->verify($this->exercise, 'failure.php'); |
|
75
|
|
|
$this->assertCount(2, $results); |
|
76
|
|
|
|
|
77
|
|
|
$failure = iterator_to_array($results)[0]; |
|
78
|
|
|
$this->assertInstanceOf(Failure::class, $failure); |
|
79
|
|
|
|
|
80
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
|
81
|
|
|
$reason = '/^Client returns an error \(number \d+\): No connection could be made because'; |
|
82
|
|
|
$reason .= ' the target machine actively refused it\.\r\n'; |
|
83
|
|
|
$reason .= ' while trying to join tcp:\/\/127\.0\.0\.1:\d+\.$/'; |
|
84
|
|
|
} else { |
|
85
|
|
|
$reason = '/^Client returns an error \(number \d+\): Connection refused'; |
|
86
|
|
|
$reason .= ' while trying to join tcp:\/\/127\.0\.0\.1:\d+\.$/'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->assertRegExp($reason, $failure->getReason()); |
|
90
|
|
|
$this->assertEquals('Time Server', $failure->getCheckName()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testFailureIsReturnedIfOutputWasNotCorrect() |
|
94
|
|
|
{ |
|
95
|
|
|
$results = $this->exerciseDispatcher->verify( |
|
96
|
|
|
$this->exercise, |
|
97
|
|
|
__DIR__ . '/../res/time-server/solution-wrong.php' |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertCount(2, $results); |
|
101
|
|
|
$failure = iterator_to_array($results)[0]; |
|
102
|
|
|
|
|
103
|
|
|
$this->assertInstanceOf(StdOutFailure::class, $failure); |
|
104
|
|
|
$this->assertNotEquals($failure->getExpectedOutput(), $failure->getActualOutput()); |
|
105
|
|
|
$this->assertEquals('Time Server', $failure->getCheckName()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testSuccessIsReturnedIfOutputIsCorrect() |
|
109
|
|
|
{ |
|
110
|
|
|
$results = $this->exerciseDispatcher->verify( |
|
111
|
|
|
$this->exercise, |
|
112
|
|
|
__DIR__ . '/../res/time-server/solution.php' |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertCount(2, $results); |
|
116
|
|
|
$success = iterator_to_array($results)[0]; |
|
117
|
|
|
$this->assertInstanceOf(Success::class, $success); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testRun() |
|
121
|
|
|
{ |
|
122
|
|
|
$color = new Color; |
|
123
|
|
|
$color->setForceStyle(true); |
|
124
|
|
|
$output = new StdOutput($color, $terminal = $this->getMock(TerminalInterface::class)); |
|
125
|
|
|
|
|
126
|
|
|
$outputRegEx = "/\n"; |
|
127
|
|
|
$outputRegEx .= '\[1m\[4mArguments\[0m\[0m'; |
|
128
|
|
|
$outputRegEx .= "\n"; |
|
129
|
|
|
$outputRegEx .= '127.0.0.1, \d+'; |
|
130
|
|
|
$outputRegEx .= "\n"; |
|
131
|
|
|
$outputRegEx .= '\[1m\[4mOutput\[0m\[0m'; |
|
132
|
|
|
$outputRegEx .= "\n"; |
|
133
|
|
|
$outputRegEx .= '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'; |
|
134
|
|
|
$outputRegEx .= "\n/"; |
|
135
|
|
|
$this->expectOutputRegex($outputRegEx); |
|
136
|
|
|
|
|
137
|
|
|
$this->exerciseDispatcher->run( |
|
138
|
|
|
$this->exercise, |
|
139
|
|
|
__DIR__ . '/../res/time-server/solution.php', |
|
140
|
|
|
$output |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|