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\Result\Failure; |
14
|
|
|
use PhpSchool\PhpWorkshop\Result\StdOutFailure; |
15
|
|
|
use PhpSchool\PhpWorkshop\Result\Success; |
16
|
|
|
use PHPUnit_Framework_TestCase; |
17
|
|
|
use PhpSchool\LearnYouPhp\Exercise\MyFirstIo; |
18
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class TimeServerTest |
22
|
|
|
* @package PhpSchool\LearnYouPhpTest\Exercise |
23
|
|
|
* @author Michael Woodward <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class TimeServerTest extends PHPUnit_Framework_TestCase |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TimeServer |
30
|
|
|
*/ |
31
|
|
|
private $exercise; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var TcpSocketFactory |
35
|
|
|
*/ |
36
|
|
|
private $socketFactory; |
37
|
|
|
|
38
|
|
|
public function setUp() |
39
|
|
|
{ |
40
|
|
|
$this->socketFactory = $this->getMock(TcpSocketFactory::class); |
41
|
|
|
$this->exercise = new TimeServer($this->socketFactory); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testGetters() |
45
|
|
|
{ |
46
|
|
|
$this->assertEquals('Time Server', $this->exercise->getName()); |
47
|
|
|
$this->assertEquals('Build a Time Server!', $this->exercise->getDescription()); |
48
|
|
|
|
49
|
|
|
$this->assertFileExists(realpath($this->exercise->getSolution())); |
50
|
|
|
$this->assertFileExists(realpath($this->exercise->getProblem())); |
51
|
|
|
$this->assertNull($this->exercise->tearDown()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testFailureIsReturnedIfCannotConnect() |
55
|
|
|
{ |
56
|
|
|
$this->socketFactory |
|
|
|
|
57
|
|
|
->expects($this->once()) |
58
|
|
|
->method('createClient') |
59
|
|
|
->with('127.0.0.1', $this->logicalAnd( |
60
|
|
|
$this->greaterThan(1024), |
61
|
|
|
$this->lessThan(655356) |
62
|
|
|
)) |
63
|
|
|
->will($this->returnValue(new Client('tcp://127.0.0.1:655355'))); |
64
|
|
|
|
65
|
|
|
$failure = $this->exercise->check('program.php'); |
66
|
|
|
|
67
|
|
|
$this->assertInstanceOf(Failure::class, $failure); |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
71
|
|
|
$reason = '/^Client returns an error \(number \d+\): No connection could be made because'; |
72
|
|
|
$reason .= ' the target machine actively refused it\.\r\n'; |
73
|
|
|
$reason .= ' while trying to join tcp:\/\/127\.0\.0\.1:655355\.$/'; |
74
|
|
|
} else { |
75
|
|
|
$reason = '/^Client returns an error \(number \d+\): Connection refused'; |
76
|
|
|
$reason .= ' while trying to join tcp:\/\/127\.0\.0\.1:655355\.$/'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->assertRegExp($reason, $failure->getReason()); |
|
|
|
|
80
|
|
|
$this->assertEquals('Time Server', $failure->getCheckName()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function testFailureIsReturnedIfOutputWasNotCorrect() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$e = new TimeServer(new TcpSocketFactory); |
86
|
|
|
$failure = $e->check(__DIR__ . '/../res/time-server/solution-wrong.php'); |
87
|
|
|
$this->assertInstanceOf(StdOutFailure::class, $failure); |
88
|
|
|
$this->assertNotEquals($failure->getExpectedOutput(), $failure->getActualOutput()); |
|
|
|
|
89
|
|
|
$this->assertEquals('Time Server', $failure->getCheckName()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testSuccessIsReturnedIfOutputIsCorrect() |
93
|
|
|
{ |
94
|
|
|
$e = new TimeServer(new TcpSocketFactory); |
95
|
|
|
$success = $e->check(__DIR__ . '/../res/time-server/solution.php'); |
96
|
|
|
$this->assertInstanceOf(Success::class, $success); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function testProcessIsStoppedIfStillRunning() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$e = new TimeServer(new TcpSocketFactory); |
102
|
|
|
$failure = $e->check(__DIR__ . '/../res/time-server/solution-keep-running.php'); |
103
|
|
|
$this->assertInstanceOf(Failure::class, $failure); |
104
|
|
|
$this->assertEquals('', $failure->getReason()); |
|
|
|
|
105
|
|
|
$this->assertEquals('Time Server', $failure->getCheckName()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
View Code Duplication |
public function testFailureIsReturnedIfProcessWasNotSuccessful() |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$e = new TimeServer(new TcpSocketFactory); |
111
|
|
|
$failure = $e->check(__DIR__ . '/../res/time-server/solution-bad-exit.php'); |
112
|
|
|
$this->assertInstanceOf(Failure::class, $failure); |
113
|
|
|
$this->assertEquals('', $failure->getReason()); |
|
|
|
|
114
|
|
|
$this->assertEquals('Time Server', $failure->getCheckName()); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.