10
10
use PhpSchool \PhpWorkshop \Exercise \ExerciseInterface ;
11
11
use PhpSchool \PhpWorkshop \Exercise \ExerciseType ;
12
12
use PhpSchool \PhpWorkshop \Exercise \Scenario \CliScenario ;
13
- use PhpSchool \PhpWorkshop \ExerciseDispatcher ;
14
13
use PhpSchool \PhpWorkshop \Output \OutputInterface ;
15
14
use PhpSchool \PhpWorkshop \Result \ComparisonFailure ;
16
15
use PhpSchool \PhpWorkshop \Result \Failure ;
@@ -31,43 +30,50 @@ public function getDescription(): string
31
30
32
31
public function defineListeners (EventDispatcher $ eventDispatcher ): void
33
32
{
34
- $ appendArgsListener = function (CliExecuteEvent $ event ) {
35
- $ event ->appendArg ('127.0.0.1 ' );
36
- $ event ->appendArg ($ this ->getRandomPort ());
37
- };
38
-
39
- $ eventDispatcher ->listen ('cli.verify.reference-execute.pre ' , $ appendArgsListener );
40
- $ eventDispatcher ->listen ('cli.verify.student-execute.pre ' , $ appendArgsListener );
41
- $ eventDispatcher ->listen ('cli.run.student-execute.pre ' , $ appendArgsListener );
42
-
43
- $ eventDispatcher ->listen ('cli.verify.reference.executing ' , function (CliExecuteEvent $ event ) {
44
- $ args = $ event ->getArgs ()->getArrayCopy ();
33
+ $ referencePort = $ this ->getRandomPort ();
34
+ $ studentPort = $ this ->getRandomPort ();
35
+
36
+ $ eventDispatcher ->listen ('cli.verify.reference-execute.pre ' ,
37
+ function (CliExecuteEvent $ event ) use ($ referencePort ) {
38
+ $ event ->appendArg ('127.0.0.1 ' );
39
+ $ event ->appendArg ($ referencePort );
40
+ $ event ->getScenario ()->exposePort ($ referencePort );
41
+ }
42
+ );
43
+ $ eventDispatcher ->listen (
44
+ ['cli.verify.student-execute.pre ' , 'cli.run.student-execute.pre ' ],
45
+ function (CliExecuteEvent $ event ) use ($ studentPort ) {
46
+ $ event ->appendArg ('127.0.0.1 ' );
47
+ $ event ->appendArg ($ studentPort );
48
+ $ event ->getScenario ()->exposePort ($ studentPort );
49
+ }
50
+ );
45
51
52
+ $ eventDispatcher ->listen ('cli.verify.reference.executing ' , function (CliExecuteEvent $ event ) use ($ referencePort ) {
46
53
//wait for server to boot
47
54
usleep (100000 );
48
55
49
56
$ socket = $ this ->createSocket ();
50
- socket_connect ($ socket , $ args [0 ], (int ) $ args [1 ]);
57
+
58
+ $ connectResult = @socket_connect ($ socket , '127.0.0.1 ' , $ referencePort );
51
59
socket_read ($ socket , 2048 , PHP_NORMAL_READ );
52
60
53
61
//wait for shutdown
54
62
usleep (100000 );
55
63
});
56
64
57
- $ eventDispatcher ->insertVerifier ('cli.verify.student.executing ' , function (CliExecuteEvent $ event ) {
58
- $ args = $ event ->getArgs ()->getArrayCopy ();
59
-
65
+ $ eventDispatcher ->insertVerifier ('cli.verify.student.executing ' , function (CliExecuteEvent $ event ) use ($ studentPort ) {
60
66
//wait for server to boot
61
67
usleep (100000 );
62
68
63
69
$ socket = $ this ->createSocket ();
64
- $ connectResult = @socket_connect ($ socket , $ args [ 0 ], ( int ) $ args [ 1 ] );
70
+ $ connectResult = @socket_connect ($ socket , ' 127.0.0.1 ' , $ studentPort );
65
71
66
72
if (!$ connectResult ) {
67
73
return Failure::fromNameAndReason ($ this ->getName (), sprintf (
68
74
"Client returns an error (number %d): Connection refused while trying to join tcp://127.0.0.1:%d. " ,
69
75
socket_last_error ($ socket ),
70
- $ args [ 1 ]
76
+ $ studentPort
71
77
));
72
78
}
73
79
@@ -86,16 +92,25 @@ public function defineListeners(EventDispatcher $eventDispatcher): void
86
92
return new Success ($ this ->getName ());
87
93
});
88
94
89
- $ eventDispatcher ->listen ('cli.run.student.executing ' , function (CliExecuteEvent $ event ) {
95
+ $ eventDispatcher ->listen ('cli.run.student.executing ' , function (CliExecuteEvent $ event ) use ( $ studentPort ) {
90
96
/** @var OutputInterface $output */
91
97
$ output = $ event ->getParameter ('output ' );
92
- $ args = $ event ->getArgs ()->getArrayCopy ();
93
98
94
99
//wait for server to boot
95
100
usleep (100000 );
96
101
97
102
$ socket = $ this ->createSocket ();
98
- socket_connect ($ socket , $ args [0 ], (int ) $ args [1 ]);
103
+ try {
104
+ $ connectResult = @socket_connect ($ socket , '127.0.0.1 ' , $ studentPort );
105
+ } catch (\ErrorException $ e ) {
106
+ $ output ->write ('Cannot connect ' );
107
+ return ;
108
+ }
109
+
110
+ if (false === $ connectResult ) {
111
+ $ output ->write ('Cannot connect ' );
112
+ return ;
113
+ }
99
114
$ out = (string ) socket_read ($ socket , 2048 , PHP_NORMAL_READ );
100
115
101
116
//wait for shutdown
@@ -107,7 +122,7 @@ public function defineListeners(EventDispatcher $eventDispatcher): void
107
122
108
123
private function getRandomPort (): string
109
124
{
110
- return ( string ) mt_rand (1025 , 65535 );
125
+ return mt_rand (8001 , 10000 );
111
126
}
112
127
113
128
public function getType (): ExerciseType
@@ -117,7 +132,8 @@ public function getType(): ExerciseType
117
132
118
133
public function defineTestScenario (): CliScenario
119
134
{
120
- return (new CliScenario ())->withExecution ();
135
+ return (new CliScenario ())
136
+ ->withExecution ();
121
137
}
122
138
123
139
private function createSocket (): Socket
0 commit comments