1 | <?php |
---|
2 | /** |
---|
3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions are met: |
---|
7 | * |
---|
8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
9 | * this list of conditions and the following disclaimer. |
---|
10 | * |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | * this listof conditions and the following disclaimer in the documentation |
---|
13 | * and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | * |
---|
27 | * The views and conclusions contained in the software and documentation are |
---|
28 | * those of the authors and should not be interpreted as representing official |
---|
29 | * policies, either expressed or implied, of the copyright holder. |
---|
30 | */ |
---|
31 | |
---|
32 | class Solarium_Plugin_BufferedAddTest extends PHPUnit_Framework_TestCase |
---|
33 | { |
---|
34 | /** |
---|
35 | * @var Solarium_Plugin_BufferedAdd |
---|
36 | */ |
---|
37 | protected $_plugin; |
---|
38 | |
---|
39 | public function setUp() |
---|
40 | { |
---|
41 | $this->_plugin = new Solarium_Plugin_BufferedAdd(); |
---|
42 | $this->_plugin->init(new Solarium_Client(), array()); |
---|
43 | } |
---|
44 | |
---|
45 | public function testSetAndGetBufferSize() |
---|
46 | { |
---|
47 | $this->_plugin->setBufferSize(500); |
---|
48 | $this->assertEquals(500, $this->_plugin->getBufferSize()); |
---|
49 | } |
---|
50 | |
---|
51 | public function testAddDocument() |
---|
52 | { |
---|
53 | $doc = new Solarium_Document_ReadWrite(); |
---|
54 | $doc->id = '123'; |
---|
55 | $doc->name = 'test'; |
---|
56 | |
---|
57 | $this->_plugin->addDocument($doc); |
---|
58 | |
---|
59 | $this->assertEquals(array($doc), $this->_plugin->getDocuments()); |
---|
60 | } |
---|
61 | |
---|
62 | public function testCreateDocument() |
---|
63 | { |
---|
64 | $data = array('id' => '123', 'name' => 'test'); |
---|
65 | $doc = new Solarium_Document_ReadWrite($data); |
---|
66 | |
---|
67 | $this->_plugin->createDocument($data); |
---|
68 | |
---|
69 | $this->assertEquals(array($doc), $this->_plugin->getDocuments()); |
---|
70 | } |
---|
71 | |
---|
72 | public function testAddDocuments() |
---|
73 | { |
---|
74 | $doc1 = new Solarium_Document_ReadWrite(); |
---|
75 | $doc1->id = '123'; |
---|
76 | $doc1->name = 'test'; |
---|
77 | |
---|
78 | $doc2 = new Solarium_Document_ReadWrite(); |
---|
79 | $doc2->id = '234'; |
---|
80 | $doc2->name = 'test2'; |
---|
81 | |
---|
82 | $docs = array($doc1, $doc2); |
---|
83 | |
---|
84 | $this->_plugin->addDocuments($docs); |
---|
85 | |
---|
86 | $this->assertEquals($docs, $this->_plugin->getDocuments()); |
---|
87 | } |
---|
88 | |
---|
89 | public function testAddDocumentAutoFlush() |
---|
90 | { |
---|
91 | $observer = $this->getMock('Solarium_Plugin_BufferedAdd', array('flush')); |
---|
92 | $observer->expects($this->once())->method('flush'); |
---|
93 | $observer->setBufferSize(1); |
---|
94 | |
---|
95 | $doc1 = new Solarium_Document_ReadWrite(); |
---|
96 | $doc1->id = '123'; |
---|
97 | $doc1->name = 'test'; |
---|
98 | |
---|
99 | $doc2 = new Solarium_Document_ReadWrite(); |
---|
100 | $doc2->id = '234'; |
---|
101 | $doc2->name = 'test2'; |
---|
102 | |
---|
103 | $docs = array($doc1, $doc2); |
---|
104 | |
---|
105 | $observer->addDocuments($docs); |
---|
106 | } |
---|
107 | |
---|
108 | public function testClear() |
---|
109 | { |
---|
110 | $doc = new Solarium_Document_ReadWrite(); |
---|
111 | $doc->id = '123'; |
---|
112 | $doc->name = 'test'; |
---|
113 | |
---|
114 | $this->_plugin->addDocument($doc); |
---|
115 | $this->_plugin->clear(); |
---|
116 | |
---|
117 | $this->assertEquals(array(), $this->_plugin->getDocuments()); |
---|
118 | } |
---|
119 | |
---|
120 | public function testFlushEmptyBuffer() |
---|
121 | { |
---|
122 | $this->assertEquals(false, $this->_plugin->flush()); |
---|
123 | } |
---|
124 | |
---|
125 | public function testFlush() |
---|
126 | { |
---|
127 | $data = array('id' => '123', 'name' => 'test'); |
---|
128 | $doc = new Solarium_Document_ReadWrite($data); |
---|
129 | |
---|
130 | $mockUpdate = $this->getMock('Solarium_Query_Update', array('addDocuments')); |
---|
131 | $mockUpdate->expects($this->once())->method('addDocuments')->with($this->equalTo(array($doc)),$this->equalTo(true),$this->equalTo(12)); |
---|
132 | |
---|
133 | $mockClient = $this->getMock('Solarium_Client', array('createUpdate', 'update', 'triggerEvent')); |
---|
134 | $mockClient->expects($this->exactly(2))->method('createUpdate')->will($this->returnValue($mockUpdate)); |
---|
135 | $mockClient->expects($this->once())->method('update')->will($this->returnValue('dummyResult')); |
---|
136 | $mockClient->expects($this->exactly(2))->method('triggerEvent'); |
---|
137 | |
---|
138 | $plugin = new Solarium_Plugin_BufferedAdd(); |
---|
139 | $plugin->init($mockClient, array()); |
---|
140 | $plugin->addDocument($doc); |
---|
141 | |
---|
142 | $this->assertEquals('dummyResult', $plugin->flush(true,12)); |
---|
143 | } |
---|
144 | |
---|
145 | public function testCommit() |
---|
146 | { |
---|
147 | $data = array('id' => '123', 'name' => 'test'); |
---|
148 | $doc = new Solarium_Document_ReadWrite($data); |
---|
149 | |
---|
150 | $mockUpdate = $this->getMock('Solarium_Query_Update', array('addDocuments', 'addCommit')); |
---|
151 | $mockUpdate->expects($this->once())->method('addDocuments')->with($this->equalTo(array($doc)),$this->equalTo(true)); |
---|
152 | $mockUpdate->expects($this->once())->method('addCommit')->with($this->equalTo(false),$this->equalTo(true),$this->equalTo(false)); |
---|
153 | |
---|
154 | $mockClient = $this->getMock('Solarium_Client', array('createUpdate', 'update', 'triggerEvent')); |
---|
155 | $mockClient->expects($this->exactly(2))->method('createUpdate')->will($this->returnValue($mockUpdate)); |
---|
156 | $mockClient->expects($this->once())->method('update')->will($this->returnValue('dummyResult')); |
---|
157 | $mockClient->expects($this->exactly(2))->method('triggerEvent'); |
---|
158 | |
---|
159 | $plugin = new Solarium_Plugin_BufferedAdd(); |
---|
160 | $plugin->init($mockClient, array()); |
---|
161 | $plugin->addDocument($doc); |
---|
162 | |
---|
163 | $this->assertEquals('dummyResult', $plugin->commit(true, false, true, false)); |
---|
164 | } |
---|
165 | |
---|
166 | } |
---|