INFRA-132 add full stop after comments
[civicrm-core.git] / tests / phpunit / CRM / Queue / Queue / SqlTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
38 /* ----------------------- Queue providers ----------------------- */
39
40 /* Define a list of queue providers which should be tested */
41
42 /**
43 * Return a list of persistent and transient queue providers.
44 */
45 public function getQueueSpecs() {
46 $queueSpecs = array();
47 $queueSpecs[] = array(
48 array(
49 'type' => 'Sql',
50 'name' => 'test-queue',
51 ),
52 );
53 return $queueSpecs;
54 }
55
56 /**
57 * Per-provider tests
58 *
59 */
60 public function setUp() {
61 parent::setUp();
62 $this->queueService = CRM_Queue_Service::singleton(TRUE);
63 }
64
65 public function tearDown() {
66 CRM_Utils_Time::resetTime();
67
68 $tablesToTruncate = array('civicrm_queue_item');
69 $this->quickCleanup($tablesToTruncate);
70 }
71
72 /**
73 * Create a few queue items; alternately enqueue and dequeue various
74 *
75 * @dataProvider getQueueSpecs
76 * @param $queueSpec
77 */
78 public function testPriorities($queueSpec) {
79 $this->queue = $this->queueService->create($queueSpec);
80 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
81
82 $this->queue->createItem(array(
83 'test-key' => 'a',
84 ));
85 $this->queue->createItem(array(
86 'test-key' => 'b',
87 ));
88 $this->queue->createItem(array(
89 'test-key' => 'c',
90 ));
91
92 $this->assertEquals(3, $this->queue->numberOfItems());
93 $item = $this->queue->claimItem();
94 $this->assertEquals('a', $item->data['test-key']);
95 $this->queue->deleteItem($item);
96
97 $this->assertEquals(2, $this->queue->numberOfItems());
98 $item = $this->queue->claimItem();
99 $this->assertEquals('b', $item->data['test-key']);
100 $this->queue->deleteItem($item);
101
102 $this->queue->createItem(
103 array(
104 'test-key' => 'start',
105 ),
106 array(
107 'weight' => -1,
108 )
109 );
110 $this->queue->createItem(
111 array(
112 'test-key' => 'end',
113 ),
114 array(
115 'weight' => 1,
116 )
117 );
118 $this->queue->createItem(array(
119 'test-key' => 'd',
120 ));
121
122 $this->assertEquals(4, $this->queue->numberOfItems());
123 $item = $this->queue->claimItem();
124 $this->assertEquals('start', $item->data['test-key']);
125 $this->queue->deleteItem($item);
126
127 $this->assertEquals(3, $this->queue->numberOfItems());
128 $item = $this->queue->claimItem();
129 $this->assertEquals('c', $item->data['test-key']);
130 $this->queue->deleteItem($item);
131
132 $this->assertEquals(2, $this->queue->numberOfItems());
133 $item = $this->queue->claimItem();
134 $this->assertEquals('d', $item->data['test-key']);
135 $this->queue->deleteItem($item);
136
137 $this->assertEquals(1, $this->queue->numberOfItems());
138 $item = $this->queue->claimItem();
139 $this->assertEquals('end', $item->data['test-key']);
140 $this->queue->deleteItem($item);
141
142 $this->assertEquals(0, $this->queue->numberOfItems());
143 }
144
145 }