phpcs - Fix error, "CONST keyword must be lowercase; expected const but found CONST"
[civicrm-core.git] / tests / phpunit / CRM / Queue / Queue / SqlTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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
29require_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 */
36class CRM_Queue_Queue_SqlTest extends CiviUnitTestCase {
6a488035
TO
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 function getQueueSpecs() {
46 $queueSpecs = array();
47 $queueSpecs[] = array(
48 array(
49 'type' => 'Sql',
50 'name' => 'test-queue',
51 ));
52 return $queueSpecs;
53 }
54
55 /* ----------------------- Per-provider tests ----------------------- */
56 function setUp() {
57 parent::setUp();
58 $this->queueService = CRM_Queue_Service::singleton(TRUE);
59 }
60
61 function tearDown() {
62 CRM_Utils_Time::resetTime();
63
64 $tablesToTruncate = array('civicrm_queue_item');
65 $this->quickCleanup($tablesToTruncate);
66 }
67
68 /**
69 * Create a few queue items; alternately enqueue and dequeue various
70 *
71 * @dataProvider getQueueSpecs
72 */
73 function testPriorities($queueSpec) {
74 $this->queue = $this->queueService->create($queueSpec);
75 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
76
77 $this->queue->createItem(array(
78 'test-key' => 'a',
79 ));
80 $this->queue->createItem(array(
81 'test-key' => 'b',
82 ));
83 $this->queue->createItem(array(
84 'test-key' => 'c',
85 ));
86
87 $this->assertEquals(3, $this->queue->numberOfItems());
88 $item = $this->queue->claimItem();
89 $this->assertEquals('a', $item->data['test-key']);
90 $this->queue->deleteItem($item);
91
92 $this->assertEquals(2, $this->queue->numberOfItems());
93 $item = $this->queue->claimItem();
94 $this->assertEquals('b', $item->data['test-key']);
95 $this->queue->deleteItem($item);
96
97 $this->queue->createItem(
98 array(
99 'test-key' => 'start',
b6708aeb 100 ),
6a488035
TO
101 array(
102 'weight' => -1,
103 )
104 );
105 $this->queue->createItem(
106 array(
107 'test-key' => 'end',
b6708aeb 108 ),
6a488035
TO
109 array(
110 'weight' => 1,
111 )
112 );
113 $this->queue->createItem(array(
114 'test-key' => 'd',
115 ));
116
117 $this->assertEquals(4, $this->queue->numberOfItems());
118 $item = $this->queue->claimItem();
119 $this->assertEquals('start', $item->data['test-key']);
120 $this->queue->deleteItem($item);
121
122 $this->assertEquals(3, $this->queue->numberOfItems());
123 $item = $this->queue->claimItem();
124 $this->assertEquals('c', $item->data['test-key']);
125 $this->queue->deleteItem($item);
126
127 $this->assertEquals(2, $this->queue->numberOfItems());
128 $item = $this->queue->claimItem();
129 $this->assertEquals('d', $item->data['test-key']);
130 $this->queue->deleteItem($item);
131
132 $this->assertEquals(1, $this->queue->numberOfItems());
133 $item = $this->queue->claimItem();
134 $this->assertEquals('end', $item->data['test-key']);
135 $this->queue->deleteItem($item);
136
137 $this->assertEquals(0, $this->queue->numberOfItems());
138 }
139
140}
141