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