This repository has been archived by the owner on Apr 5, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Create reusable unittest action "StartServer" for FTP and remote in…
…tegration tests # See xp-framework/rfc#272
- Loading branch information
Showing
4 changed files
with
130 additions
and
161 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
core/src/test/php/net/xp_framework/unittest/StartServer.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php namespace net\xp_framework\unittest; | ||
|
||
use lang\Runtime; | ||
use lang\IllegalStateException; | ||
|
||
/** | ||
* Starts a server for integration tests | ||
*/ | ||
class StartServer extends \lang\Object implements \unittest\TestClassAction { | ||
protected $serverProcess; | ||
protected $mainClass; | ||
protected $connected; | ||
protected $shutdown; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param string $mainClass Server process main class | ||
* @param string $connected Name of method to invoke once connected | ||
* @param string $shutdown Name of method to invoke to shut down | ||
* @param string[] $arguments Arguments to server process | ||
*/ | ||
public function __construct($mainClass, $connected, $shutdown, $arguments= array()) { | ||
$this->mainClass= $mainClass; | ||
$this->connected= $connected; | ||
$this->shutdown= $shutdown; | ||
$this->arguments= $arguments; | ||
} | ||
|
||
/** | ||
* Starts server | ||
* | ||
* @param lang.XPClass $c | ||
* @return void | ||
* @throws unittest.PrerequisitesNotMetError | ||
*/ | ||
public function beforeTestClass(\lang\XPClass $c) { | ||
|
||
// Start server process | ||
$this->serverProcess= Runtime::getInstance()->newInstance(null, 'class', $this->mainClass, $this->arguments); | ||
$this->serverProcess->in->close(); | ||
|
||
// Check if startup succeeded | ||
$status= $this->serverProcess->out->readLine(); | ||
if (1 != sscanf($status, '+ Service %[0-9.:]', $bindAddress)) { | ||
try { | ||
$this->afterTestClass($c); | ||
} catch (IllegalStateException $e) { | ||
$status.= $e->getMessage(); | ||
} | ||
throw new \unittest\PrerequisitesNotMetError('Cannot start server: '.$status, null); | ||
} | ||
|
||
$c->getMethod($this->connected)->invoke(null, array($bindAddress)); | ||
} | ||
|
||
/** | ||
* Shuts down server | ||
* | ||
* @param lang.XPClass $c | ||
* @return void | ||
*/ | ||
public function afterTestClass(\lang\XPClass $c) { | ||
|
||
// Tell the server to shut down | ||
try { | ||
$c->getMethod($this->shutdown)->invoke(null, array()); | ||
} catch (\lang\Throwable $ignored) { | ||
// Fall through, below should terminate the process anyway | ||
} | ||
|
||
$status= $this->serverProcess->out->readLine(); | ||
if (!strlen($status) || '+' != $status{0}) { | ||
while ($l= $this->serverProcess->out->readLine()) { | ||
$status.= $l; | ||
} | ||
while ($l= $this->serverProcess->err->readLine()) { | ||
$status.= $l; | ||
} | ||
$this->serverProcess->close(); | ||
throw new IllegalStateException($status); | ||
} | ||
$this->serverProcess->close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.