tests/phpunit/** - Remove unnecessary "require_once" statements
[civicrm-core.git] / tests / phpunit / CRM / Queue / Queue / SqlTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
6a488035
TO
28/**
29 * Ensure that the extended interface for SQL-backed queues
30 * work. For example, the createItem() interface supports
31 * priority-queueing.
32 */
33class CRM_Queue_Queue_SqlTest extends CiviUnitTestCase {
6a488035
TO
34
35 /* ----------------------- Queue providers ----------------------- */
36
37 /* Define a list of queue providers which should be tested */
38
39 /**
eceb18cc 40 * Return a list of persistent and transient queue providers.
6a488035 41 */
00be9182 42 public function getQueueSpecs() {
6a488035
TO
43 $queueSpecs = array();
44 $queueSpecs[] = array(
45 array(
46 'type' => 'Sql',
47 'name' => 'test-queue',
79d7553f 48 ),
92915c55 49 );
6a488035
TO
50 return $queueSpecs;
51 }
52
79d7553f 53 /**
54 * Per-provider tests
55 *
56 */
00be9182 57 public function setUp() {
6a488035
TO
58 parent::setUp();
59 $this->queueService = CRM_Queue_Service::singleton(TRUE);
60 }
61
00be9182 62 public function tearDown() {
6a488035
TO
63 CRM_Utils_Time::resetTime();
64
65 $tablesToTruncate = array('civicrm_queue_item');
66 $this->quickCleanup($tablesToTruncate);
67 }
68
69 /**
70 * Create a few queue items; alternately enqueue and dequeue various
71 *
72 * @dataProvider getQueueSpecs
1e1fdcf6 73 * @param $queueSpec
6a488035 74 */
00be9182 75 public function testPriorities($queueSpec) {
6a488035
TO
76 $this->queue = $this->queueService->create($queueSpec);
77 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
78
79 $this->queue->createItem(array(
92915c55
TO
80 'test-key' => 'a',
81 ));
6a488035 82 $this->queue->createItem(array(
92915c55
TO
83 'test-key' => 'b',
84 ));
6a488035 85 $this->queue->createItem(array(
92915c55
TO
86 'test-key' => 'c',
87 ));
6a488035
TO
88
89 $this->assertEquals(3, $this->queue->numberOfItems());
90 $item = $this->queue->claimItem();
91 $this->assertEquals('a', $item->data['test-key']);
92 $this->queue->deleteItem($item);
93
94 $this->assertEquals(2, $this->queue->numberOfItems());
95 $item = $this->queue->claimItem();
96 $this->assertEquals('b', $item->data['test-key']);
97 $this->queue->deleteItem($item);
98
99 $this->queue->createItem(
100 array(
101 'test-key' => 'start',
b6708aeb 102 ),
6a488035
TO
103 array(
104 'weight' => -1,
105 )
106 );
107 $this->queue->createItem(
108 array(
109 'test-key' => 'end',
b6708aeb 110 ),
6a488035
TO
111 array(
112 'weight' => 1,
113 )
114 );
115 $this->queue->createItem(array(
92915c55
TO
116 'test-key' => 'd',
117 ));
6a488035
TO
118
119 $this->assertEquals(4, $this->queue->numberOfItems());
120 $item = $this->queue->claimItem();
121 $this->assertEquals('start', $item->data['test-key']);
122 $this->queue->deleteItem($item);
123
124 $this->assertEquals(3, $this->queue->numberOfItems());
125 $item = $this->queue->claimItem();
126 $this->assertEquals('c', $item->data['test-key']);
127 $this->queue->deleteItem($item);
128
129 $this->assertEquals(2, $this->queue->numberOfItems());
130 $item = $this->queue->claimItem();
131 $this->assertEquals('d', $item->data['test-key']);
132 $this->queue->deleteItem($item);
133
134 $this->assertEquals(1, $this->queue->numberOfItems());
135 $item = $this->queue->claimItem();
136 $this->assertEquals('end', $item->data['test-key']);
137 $this->queue->deleteItem($item);
138
139 $this->assertEquals(0, $this->queue->numberOfItems());
140 }
141
142}