Skip to content

Commit

Permalink
Created testing environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
boenrobot committed Oct 8, 2011
1 parent 7b2f520 commit f59f8f5
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 18 deletions.
2 changes: 1 addition & 1 deletion nbproject/project.properties
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include.path=\
php.version=PHP_53 php.version=PHP_53
phpunit.bootstrap= phpunit.bootstrap=
phpunit.bootstrap.create.tests=false phpunit.bootstrap.create.tests=false
phpunit.configuration=tests/configuration.xml phpunit.configuration=tests/phpunit.xml
phpunit.run.test.files=false phpunit.run.test.files=false
phpunit.suite= phpunit.suite=
source.encoding=UTF-8 source.encoding=UTF-8
Expand Down
2 changes: 1 addition & 1 deletion src/PEAR2/Net/Transmitter/SocketClientTransmitter.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SocketClientTransmitter extends StreamTransmitter
* connection. * connection.
* @param resource $context A context for the socket. * @param resource $context A context for the socket.
*/ */
public function __construct($host, $port = 8728, $persist = false, public function __construct($host, $port, $persist = false,
$timeout = null, $key = '', $context = null $timeout = null, $key = '', $context = null
) { ) {
$flags = STREAM_CLIENT_CONNECT; $flags = STREAM_CLIENT_CONNECT;
Expand Down
15 changes: 1 addition & 14 deletions src/PEAR2/Net/Transmitter/SocketServerConnectionTransmitter.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SocketServerConnectionTransmitter extends StreamTransmitter
*/ */
public function __construct($server, $timeout = null) public function __construct($server, $timeout = null)
{ {
if (!self::isServer($server)) { if (!self::isStream($server)) {
throw $this->createException('Invalid server supplied.', 8); throw $this->createException('Invalid server supplied.', 8);
} }
$timeout $timeout
Expand All @@ -74,19 +74,6 @@ public function __construct($server, $timeout = null)
} }
} }


/**
* Checks whether a cetain variable is a socket server.
*
* @param mixed $var The variable to check.
*
* @return bool TRUE on success, FALSE on failure.
*/
public static function isServer($var)
{
return is_resource($var)
&& (bool) preg_match('#\s?server\s?#sm', get_resource_type($var));
}

/** /**
* Gets the IP address of the connected client. * Gets the IP address of the connected client.
* *
Expand Down
32 changes: 32 additions & 0 deletions tests/ClientTest.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace PEAR2\Net\Transmitter;

class ClientTest extends \PHPUnit_Framework_TestCase
{

/**
* @var SocketClientTransmitter
*/
protected $client;

public function setUp()
{
$this->client = new SocketClientTransmitter(
REMOTE_HOSTNAME, REMOTE_PORT
);
}

public function tearDown()
{
unset($this->client);
}

public function testOneByteEcho()
{
$byte = 't';
$this->client->send($byte);
$this->assertEquals(
$byte, $this->client->receive(1), 'Wrong byte echoed.'
);
}
}
58 changes: 58 additions & 0 deletions tests/ServerTest.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace PEAR2\Net\Transmitter;

class ServerTest extends \PHPUnit_Framework_TestCase
{

/**
* @var resource
*/
protected static $server;

/**
* @var int
*/
protected static $errorno;

/**
* @var string
*/
protected static $errstr;

/**
* @var SocketServerConnectionTransmitter
*/
protected $conn;

public static function setUpBeforeClass()
{
self::$server = stream_socket_server(
'tcp://' . LOCAL_HOSTNAME . ':' . LOCAL_PORT,
self::$errorno, self::$errstr
);
}

public static function tearDownAfterClass()
{
fclose(self::$server);
}

public function setUp()
{
$this->conn = new SocketServerConnectionTransmitter(
self::$server, 1/*h*/ * 60/*m*/ * 60/*s*/
);
}

public function tearDown()
{
unset($this->conn);
}

public function testOneByteEcho()
{
$this->assertEquals(
1, $this->conn->send($this->conn->receive(1)), 'Wrong byte echoed.'
);
}
}
4 changes: 2 additions & 2 deletions tests/StreamTransmitterTest.php
Original file line number Original file line Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php <?php
namespace PEAR2\Net\Transmitter; namespace PEAR2\Net\Transmitter;


class RequestHandlingTest extends \PHPUnit_Framework_TestCase class StreamTransmitterTest extends \PHPUnit_Framework_TestCase
{ {
public function testDefaultStreamTransmitterException() public function testDefaultStreamTransmitterException()
{ {
try { try {
$trans = new StreamTransmitter('invalid arg'); $trans = new StreamTransmitter('invalid arg');
$this->fail('Transmitter initialization had to fail.'); $this->fail('Transmitter initialization had to fail.');
} catch (\Exception $e) { } catch (StreamException $e) {
$this->assertEquals(1, $e->getCode(), 'Improper exception code.'); $this->assertEquals(1, $e->getCode(), 'Improper exception code.');
} }
} }
Expand Down
14 changes: 14 additions & 0 deletions tests/configuration.xml → tests/phpunit.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,8 +16,22 @@
convertWarningsToExceptions="false" convertWarningsToExceptions="false"
> >
--> -->
<php>
<!--
The name and port of secondaryPeer.xml
-->
<const name="REMOTE_HOSTNAME" value="127.0.0.1" />
<const name="REMOTE_PORT" value="6666" />
<!--
The name and port secondaryPeer.xml must use.
-->
<const name="LOCAL_HOSTNAME" value="127.0.0.1" />
<const name="LOCAL_PORT" value="6667" />
</php>
<testsuites> <testsuites>
<testsuite name="All tests"> <testsuite name="All tests">
<file>ClientTest.php</file>
<file>ServerTest.php</file>
<file>StreamTransmitterTest.php</file> <file>StreamTransmitterTest.php</file>
</testsuite> </testsuite>
</testsuites> </testsuites>
Expand Down
1 change: 1 addition & 0 deletions tests/secondaryPeer.bat
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
phpunit --configuration secondaryPeer.xml
42 changes: 42 additions & 0 deletions tests/secondaryPeer.xml
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="bootstrap.php"
colors="false"
stopOnFailure="false"
verbose="true"
strict="true"

convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<!--
convertErrorsToExceptions="false"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false"
>
-->
<php>
<!--
The name and port of phpunit.xml
-->
<const name="REMOTE_HOSTNAME" value="127.0.0.1" />
<const name="REMOTE_PORT" value="6667" />
<!--
The name and port phpunit.xml must use.
-->
<const name="LOCAL_HOSTNAME" value="127.0.0.1" />
<const name="LOCAL_PORT" value="6666" />
</php>
<testsuites>
<testsuite name="All tests">
<file>ServerTest.php</file>
<file>ClientTest.php</file>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">../src/PEAR2/Net/Transmitter</directory>
</whitelist>
</filter>
</phpunit>

0 comments on commit f59f8f5

Please sign in to comment.