Repository navigation
Expand file tree
/
Copy pathFileTransferTest.php
More file actions
108 lines (87 loc) · 2.67 KB
/
Copy pathFileTransferTest.php
File metadata and controls
108 lines (87 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace PhpAmqpLib\Tests\Functional;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
use PHPUnit\Framework\TestCase;
class FileTransferTest extends TestCase
{
/**
* @var string
*/
protected $exchangeName = 'test_exchange';
/**
* @var string
*/
protected $queueName = null;
/**
* @var AMQPConnection
*/
protected $connection;
/**
* @var AMQPChannel
*/
protected $channel;
/**
* @var string
*/
protected $messageBody;
public function setUp()
{
$this->connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$this->channel = $this->connection->channel();
$this->channel->exchange_declare($this->exchangeName, 'direct', false, false, false);
list($this->queueName, ,) = $this->channel->queue_declare();
$this->channel->queue_bind($this->queueName, $this->exchangeName, $this->queueName);
}
private function generateRandomBytes($num_bytes)
{
// If random_bytes exists (PHP 7) or has been polyfilled, use it
if ( function_exists('random_bytes') ) {
return random_bytes($num_bytes);
}
// Otherwise, just make some noise quickly
else {
$data = '';
for ($i = 0; $i < $num_bytes; $i++) {
$data .= chr(rand(0, 255));
}
return $data;
}
}
public function testSendFile()
{
$this->messageBody = $this->generateRandomBytes(1024 * 1024);
$msg = new AMQPMessage($this->messageBody, array('delivery_mode' => AMQPMessage::DELIVERY_MODE_NON_PERSISTENT));
$this->channel->basic_publish($msg, $this->exchangeName, $this->queueName);
$this->channel->basic_consume(
$this->queueName,
'',
false,
false,
false,
false,
array($this, 'processMessage')
);
while (count($this->channel->callbacks)) {
$this->channel->wait();
}
}
public function processMessage($msg)
{
$delivery_info = $msg->delivery_info;
$delivery_info['channel']->basic_ack($delivery_info['delivery_tag']);
$delivery_info['channel']->basic_cancel($delivery_info['consumer_tag']);
$this->assertEquals($this->messageBody, $msg->body);
}
public function tearDown()
{
if ($this->channel) {
$this->channel->exchange_delete($this->exchangeName);
$this->channel->close();
}
if ($this->connection) {
$this->connection->close();
}
}
}