Skip to content

Commit b274b5c

Browse files
committed
Added send/receive testing to ClientTest
1 parent 061da83 commit b274b5c

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

lib/Wrench/Tests/ClientTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Wrench\Tests;
44

5+
use Wrench\Protocol\Protocol;
6+
57
use Wrench\Client;
68
use Wrench\Tests\Test;
79
use Wrench\Socket;
@@ -105,4 +107,67 @@ public function testConstructorOriginInvalid()
105107
{
106108
$w = new Client('ws://localhost:8000', 'NOTAVALIDURI');
107109
}
110+
111+
/**
112+
* @expectedException InvalidArgumentException
113+
*/
114+
public function testSendInvalidType()
115+
{
116+
$client = new Client('ws://localhost/test', 'http://example.org/');
117+
$client->sendData('blah', 9999);
118+
}
119+
120+
/**
121+
* @expectedException InvalidArgumentException
122+
*/
123+
public function testSendInvalidTypeString()
124+
{
125+
$client = new Client('ws://localhost/test', 'http://example.org/');
126+
$client->sendData('blah', 'fooey');
127+
}
128+
129+
public function testSend()
130+
{
131+
try {
132+
$helper = new ServerTestHelper();
133+
$helper->setUp();
134+
135+
/* @var $instance Wrench\Client */
136+
$instance = $this->getInstance($helper->getEchoConnectionString(), 'http://www.example.com/send');
137+
$instance->addRequestHeader('X-Test', 'Custom Request Header');
138+
139+
$this->assertFalse($instance->receive(), 'Receive before connect');
140+
141+
$success = $instance->connect();
142+
$this->assertTrue($success, 'Client can connect to test server');
143+
$this->assertTrue($instance->isConnected());
144+
145+
$this->assertFalse($instance->connect(), 'Double connect');
146+
147+
$this->assertFalse((boolean)$instance->receive(), 'No data');
148+
149+
$bytes = $instance->sendData('foobar', 'text');
150+
$this->assertTrue($bytes >= 6, 'sent text frame');
151+
sleep(1);
152+
153+
$bytes = $instance->sendData('baz', Protocol::TYPE_TEXT);
154+
$this->assertTrue($bytes >= 3, 'sent text frame');
155+
sleep(1);
156+
157+
$responses = $instance->receive();
158+
$this->assertTrue(is_array($responses));
159+
$this->assertCount(2, $responses);
160+
$this->assertInstanceOf('Wrench\\Payload\\Payload', $responses[0]);
161+
$this->assertInstanceOf('Wrench\\Payload\\Payload', $responses[1]);
162+
163+
$instance->disconnect();
164+
165+
$this->assertFalse($instance->isConnected());
166+
} catch (\Exception $e) {
167+
$helper->tearDown();
168+
throw $e;
169+
}
170+
171+
$helper->tearDown();
172+
}
108173
}

0 commit comments

Comments
 (0)