forked from CoderKungfu/php-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseTest.php
More file actions
101 lines (90 loc) · 3 KB
/
Copy pathBaseTest.php
File metadata and controls
101 lines (90 loc) · 3 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
<?php
namespace PHPQueue;
class BaseTest extends \PHPUnit_Framework_TestCase
{
public function __construct($name = NULL, array $data = array(), $dataName = '')
{
parent::__construct($name, $data, $dataName);
if (file_exists('/tmp/sample_data.ser')) {
@unlink('/tmp/sample_data.ser');
}
}
protected function getSampleQueue()
{
return Base::getQueue('Sample');
}
public function testCanGetQueue()
{
$result = $this->getSampleQueue();
$this->assertInstanceOf('\\PHPQueue\\JobQueue', $result);
}
/**
* @expectedException \PHPQueue\Exception\QueueNotFoundException
*/
public function testCanFailWhenInvalidQueueNameAreGiven()
{
Base::getQueue('NonExistent');
}
public function testAddJob()
{
$queue = $this->getSampleQueue();
$result = Base::addJob($queue, array('var1'=>"Hello, world!"));
$this->assertTrue($result);
$this->assertEquals(1, $queue->getQueueSize());
$result = Base::getJob($queue); //clear
}
public function testNoNullJob()
{
$queue = $this->getSampleQueue();
try {
Base::addJob($queue, null);
$this->fail("Should not be able to add to Queue");
} catch (\Exception $ex) {
$this->assertStringStartsWith("Invalid job data.", $ex->getMessage());
}
}
public function testGetJob()
{
$queue = $this->getSampleQueue();
$result = Base::addJob($queue, array('var1'=>"Hello, world!"));
$queue = $this->getSampleQueue();
$result = Base::getJob($queue);
$this->assertInstanceOf('\\PHPQueue\\Job', $result);
$this->assertEquals(0, $queue->getQueueSize());
}
public function testNoMoreJob()
{
$queue = $this->getSampleQueue();
$result = Base::addJob($queue, array('var1'=>"Hello, world!"));
$result = Base::getJob($queue); //clear
try {
$result = Base::getJob($queue);
$this->fail("Should not be able to get job from Queue");
} catch (\Exception $ex) {
$this->assertStringStartsWith("No more jobs.", $ex->getMessage());
}
}
/**
* @expectedException \PHPQueue\Exception\WorkerNotFoundException
*/
public function testCanFailWhenInvalidWorkerNameAreGiven()
{
Base::getWorker('NonExistent');
}
public function testCanGetWorker()
{
$result = Base::getWorker('Sample');
$this->assertInstanceOf('\\PHPQueue\\Worker', $result);
}
public function testWorkJob()
{
$worker = Base::getWorker('Sample');
$job = new Job();
$job->worker = 'Sample';
$job->data = array('var1'=>'Hello, world!');
$result = Base::workJob($worker, $job);
$this->assertEquals(array('var1'=>'Hello, world!', 'var2'=>"Welcome back!"), $result->result_data);
$this->assertEquals(Job::OK, $job->status);
$this->assertTrue($job->isSuccessful());
}
}