Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06a1bc01 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06a1bc01 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | ||
29 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
30 | ||
31 | /** | |
32 | * Ensure that the extended interface for SQL-backed queues | |
33 | * work. For example, the createItem() interface supports | |
34 | * priority-queueing. | |
35 | */ | |
36 | class CRM_Queue_Queue_SqlTest extends CiviUnitTestCase { | |
37 | function get_info() { | |
38 | return array( | |
39 | 'name' => 'SQL Queue', | |
40 | 'description' => 'Test SQL-backed queue items', | |
41 | 'group' => 'Queue', | |
42 | ); | |
43 | } | |
44 | ||
45 | /* ----------------------- Queue providers ----------------------- */ | |
46 | ||
47 | /* Define a list of queue providers which should be tested */ | |
48 | ||
49 | /** | |
50 | * Return a list of persistent and transient queue providers | |
51 | */ | |
52 | function getQueueSpecs() { | |
53 | $queueSpecs = array(); | |
54 | $queueSpecs[] = array( | |
55 | array( | |
56 | 'type' => 'Sql', | |
57 | 'name' => 'test-queue', | |
58 | )); | |
59 | return $queueSpecs; | |
60 | } | |
61 | ||
62 | /* ----------------------- Per-provider tests ----------------------- */ | |
63 | function setUp() { | |
64 | parent::setUp(); | |
65 | $this->queueService = CRM_Queue_Service::singleton(TRUE); | |
66 | } | |
67 | ||
68 | function tearDown() { | |
69 | CRM_Utils_Time::resetTime(); | |
70 | ||
71 | $tablesToTruncate = array('civicrm_queue_item'); | |
72 | $this->quickCleanup($tablesToTruncate); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Create a few queue items; alternately enqueue and dequeue various | |
77 | * | |
78 | * @dataProvider getQueueSpecs | |
79 | */ | |
80 | function testPriorities($queueSpec) { | |
81 | $this->queue = $this->queueService->create($queueSpec); | |
82 | $this->assertTrue($this->queue instanceof CRM_Queue_Queue); | |
83 | ||
84 | $this->queue->createItem(array( | |
85 | 'test-key' => 'a', | |
86 | )); | |
87 | $this->queue->createItem(array( | |
88 | 'test-key' => 'b', | |
89 | )); | |
90 | $this->queue->createItem(array( | |
91 | 'test-key' => 'c', | |
92 | )); | |
93 | ||
94 | $this->assertEquals(3, $this->queue->numberOfItems()); | |
95 | $item = $this->queue->claimItem(); | |
96 | $this->assertEquals('a', $item->data['test-key']); | |
97 | $this->queue->deleteItem($item); | |
98 | ||
99 | $this->assertEquals(2, $this->queue->numberOfItems()); | |
100 | $item = $this->queue->claimItem(); | |
101 | $this->assertEquals('b', $item->data['test-key']); | |
102 | $this->queue->deleteItem($item); | |
103 | ||
104 | $this->queue->createItem( | |
105 | array( | |
106 | 'test-key' => 'start', | |
b6708aeb | 107 | ), |
6a488035 TO |
108 | array( |
109 | 'weight' => -1, | |
110 | ) | |
111 | ); | |
112 | $this->queue->createItem( | |
113 | array( | |
114 | 'test-key' => 'end', | |
b6708aeb | 115 | ), |
6a488035 TO |
116 | array( |
117 | 'weight' => 1, | |
118 | ) | |
119 | ); | |
120 | $this->queue->createItem(array( | |
121 | 'test-key' => 'd', | |
122 | )); | |
123 | ||
124 | $this->assertEquals(4, $this->queue->numberOfItems()); | |
125 | $item = $this->queue->claimItem(); | |
126 | $this->assertEquals('start', $item->data['test-key']); | |
127 | $this->queue->deleteItem($item); | |
128 | ||
129 | $this->assertEquals(3, $this->queue->numberOfItems()); | |
130 | $item = $this->queue->claimItem(); | |
131 | $this->assertEquals('c', $item->data['test-key']); | |
132 | $this->queue->deleteItem($item); | |
133 | ||
134 | $this->assertEquals(2, $this->queue->numberOfItems()); | |
135 | $item = $this->queue->claimItem(); | |
136 | $this->assertEquals('d', $item->data['test-key']); | |
137 | $this->queue->deleteItem($item); | |
138 | ||
139 | $this->assertEquals(1, $this->queue->numberOfItems()); | |
140 | $item = $this->queue->claimItem(); | |
141 | $this->assertEquals('end', $item->data['test-key']); | |
142 | $this->queue->deleteItem($item); | |
143 | ||
144 | $this->assertEquals(0, $this->queue->numberOfItems()); | |
145 | } | |
146 | ||
147 | } | |
148 |