|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of the Sulu CMS. |
| 4 | + * |
| 5 | + * (c) MASSIVE ART WebServices GmbH |
| 6 | + * |
| 7 | + * This source file is subject to the MIT license that is bundled |
| 8 | + * with this source code in the file LICENSE. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Massive\Bundle\SearchBundle\Behat; |
| 12 | + |
| 13 | +use Behat\Behat\Context\SnippetAcceptingContext; |
| 14 | +use Behat\Gherkin\Node\PyStringNode; |
| 15 | +use Behat\Symfony2Extension\Context\KernelAwareContext; |
| 16 | +use Massive\Bundle\SearchBundle\Tests\Resources\app\AppKernel; |
| 17 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 18 | +use PHPUnit_Framework_Assert as Assert; |
| 19 | +use Massive\Bundle\SearchBundle\Search\SearchManager; |
| 20 | + |
| 21 | +/** |
| 22 | + * Behat context for search manager features |
| 23 | + */ |
| 24 | +class SearchManagerContext implements SnippetAcceptingContext, KernelAwareContext |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + private $adapterId; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var AppKernel |
| 33 | + */ |
| 34 | + private $kernel; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var mixed |
| 38 | + */ |
| 39 | + private $lastResult; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var string[] |
| 43 | + */ |
| 44 | + private $entityClasses; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var object[] |
| 48 | + */ |
| 49 | + private $entities = array(); |
| 50 | + |
| 51 | + /** |
| 52 | + * @var Exception |
| 53 | + */ |
| 54 | + private $lastException = null; |
| 55 | + |
| 56 | + /** |
| 57 | + * @param string $adapterId |
| 58 | + */ |
| 59 | + public function __construct($adapterId) |
| 60 | + { |
| 61 | + $this->adapterId = $adapterId; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @BeforeSuite |
| 66 | + */ |
| 67 | + public static function clearCache() |
| 68 | + { |
| 69 | + AppKernel::clearData(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @BeforeScenario |
| 74 | + */ |
| 75 | + public function setUp() |
| 76 | + { |
| 77 | + $this->kernel->shutdown(); |
| 78 | + $this->kernel->boot(); |
| 79 | + AppKernel::resetEnvironment(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * {@inheritDoc} |
| 84 | + */ |
| 85 | + public function setKernel(KernelInterface $kernel) |
| 86 | + { |
| 87 | + $this->kernel = $kernel; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @Given that the following mapping for :mappingName exists: |
| 92 | + */ |
| 93 | + public function thatTheFollowingMappingExists($mappingName, PyStringNode $mappingXml) |
| 94 | + { |
| 95 | + file_put_contents(AppKernel::getMappingDir() . '/' . $mappingName . '.xml', $mappingXml->getRaw()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @Given the entity ":name" exists: |
| 100 | + */ |
| 101 | + public function theFollowingEntityExists($name, PyStringNode $string) |
| 102 | + { |
| 103 | + $this->entityClasses[$name] = 'Massive\Bundle\SearchBundle\Tests\Resources\TestBundle\Entity\\' . $name; |
| 104 | + file_put_contents(AppKernel::getEntityDir() . '/' . $name . '.php', $string->getRaw()); |
| 105 | + $this->pause(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @Given I get the index names |
| 110 | + */ |
| 111 | + public function iGetTheIndexNames() |
| 112 | + { |
| 113 | + $this->getSearchManager()->flush(); |
| 114 | + $this->pause(); |
| 115 | + $this->lastResult = $this->getSearchManager()->getIndexNames(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @Then the result should be the following array: |
| 120 | + */ |
| 121 | + public function theResultShouldBeTheFollowingArray(PyStringNode $string) |
| 122 | + { |
| 123 | + $expected = json_decode($string->getRaw(), true); |
| 124 | + Assert::assertEquals($expected, $this->lastResult); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @Then I should have the following documents: |
| 129 | + */ |
| 130 | + public function iShouldHaveTheFollowingDocuments(PyStringNode $string) |
| 131 | + { |
| 132 | + $expected = json_decode($string->getRaw(), true); |
| 133 | + $documents = array(); |
| 134 | + foreach ($this->lastResult as $hit) { |
| 135 | + $documents[] = $hit->getDocument()->jsonSerialize(); |
| 136 | + } |
| 137 | + Assert::assertEquals($expected, $documents); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @When I index the following ":className" objects |
| 142 | + */ |
| 143 | + public function whenIIndexTheFollowingObjects($className, PyStringNode $string) |
| 144 | + { |
| 145 | + try { |
| 146 | + $this->doIndexTheFollowingObjects($className, $string); |
| 147 | + } catch (\Exception $e) { |
| 148 | + $this->lastException = $e; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @Given the following ":className" objects have been indexed |
| 154 | + */ |
| 155 | + public function givenIIndexTheFollowingObjects($className, PyStringNode $string) |
| 156 | + { |
| 157 | + $this->doIndexTheFollowingObjects($className, $string); |
| 158 | + } |
| 159 | + |
| 160 | + private function doIndexTheFollowingObjects($className, PyStringNode $string) |
| 161 | + { |
| 162 | + $objectsData = json_decode($string->getRaw(), true); |
| 163 | + Assert::assertArrayHasKey($className, $this->entityClasses, 'Entity exists'); |
| 164 | + Assert::assertNotNull($objectsData); |
| 165 | + |
| 166 | + foreach ($objectsData as $objectData) { |
| 167 | + $object = new $this->entityClasses[$className](); |
| 168 | + foreach ($objectData as $key => $value) { |
| 169 | + $object->$key = $value; |
| 170 | + } |
| 171 | + $this->entities[$object->id] = $object; |
| 172 | + $this->getSearchManager()->index($object); |
| 173 | + } |
| 174 | + |
| 175 | + $this->getSearchManager()->flush(); |
| 176 | + |
| 177 | + $this->pause(); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * @Given I search for :query |
| 182 | + */ |
| 183 | + public function iSearchFor($query) |
| 184 | + { |
| 185 | + $this->lastResult = $this->getSearchManager()->createSearch($query)->execute(); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * @Given I search for :query in locale :locale |
| 190 | + */ |
| 191 | + public function iSearchForInLocale($query, $locale) |
| 192 | + { |
| 193 | + $this->lastResult = $this->getSearchManager()->createSearch($query)->locale($locale)->execute(); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * @Given I search for :query in locale :locale with category :category |
| 198 | + */ |
| 199 | + public function iSearchForInLocaleWIthCategory($query, $locale, $category) |
| 200 | + { |
| 201 | + $this->lastResult = $this->getSearchManager()->createSearch($query)->locale($locale)->category($category)->execute(); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * @Given I search for :query in category :category |
| 206 | + */ |
| 207 | + public function iSearchForInCategory($query, $category) |
| 208 | + { |
| 209 | + try { |
| 210 | + $this->lastResult = $this->getSearchManager()->createSearch($query)->category($category)->execute(); |
| 211 | + } catch (\Exception $e) { |
| 212 | + $this->lastException = $e; |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * @When I search for :query in index :index |
| 218 | + */ |
| 219 | + public function iSearchForInIndex($query, $index) |
| 220 | + { |
| 221 | + try { |
| 222 | + $this->lastResult = $this->getSearchManager()->createSearch($query)->index($index)->execute(); |
| 223 | + } catch (\Exception $e) { |
| 224 | + $this->lastException = $e; |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * @When I search for something with both a category and an index |
| 230 | + */ |
| 231 | + public function iSearchForInCategoryAndIndex() |
| 232 | + { |
| 233 | + try { |
| 234 | + $this->lastResult = $this->getSearchManager()->createSearch('something')->index('foo')->category('bar')->execute(); |
| 235 | + } catch (\Exception $e) { |
| 236 | + $this->lastException = $e; |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * @Then an exception with message :message should be thrown |
| 242 | + */ |
| 243 | + public function thenAnExceptionWithMessageShouldBeThrown($message) |
| 244 | + { |
| 245 | + Assert::assertNotNull($this->lastException, 'An exception has been thrown'); |
| 246 | + Assert::assertContains($message, $this->lastException->getMessage()); |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * @Given I search for :query in locale :locale with index :index |
| 251 | + */ |
| 252 | + public function iSearchForInLocaleForIndex($query, $locale, $index) |
| 253 | + { |
| 254 | + $this->lastResult = $this->getSearchManager()->createSearch($query)->index($index)->locale($locale)->execute(); |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * @Then there should be :nbResults results |
| 259 | + */ |
| 260 | + public function thereShouldBeResults($nbResults) |
| 261 | + { |
| 262 | + Assert::assertCount((integer) $nbResults, $this->lastResult); |
| 263 | + } |
| 264 | + |
| 265 | + /** |
| 266 | + * @Given I purge the index :indexName |
| 267 | + */ |
| 268 | + public function iPurgeTheIndex($indexName) |
| 269 | + { |
| 270 | + $this->getSearchManager()->purge($indexName); |
| 271 | + $this->pause(); |
| 272 | + } |
| 273 | + |
| 274 | + /** |
| 275 | + * @Given I deindex the object with id :id |
| 276 | + */ |
| 277 | + public function iDeindexTheObjectWithId($id) |
| 278 | + { |
| 279 | + Assert::arrayHasKey($id, $this->entities); |
| 280 | + $entity = $this->entities[$id]; |
| 281 | + $this->getSearchManager()->deindex($entity); |
| 282 | + $this->getSearchManager()->flush(); |
| 283 | + $this->pause(); |
| 284 | + } |
| 285 | + |
| 286 | + /** |
| 287 | + * @Given I get the status |
| 288 | + */ |
| 289 | + public function iGetTheStatus() |
| 290 | + { |
| 291 | + $this->lastResult = $this->getSearchManager()->getStatus(); |
| 292 | + } |
| 293 | + |
| 294 | + /** |
| 295 | + * @Then the result should be an array |
| 296 | + */ |
| 297 | + public function theResultShouldBeAnArray() |
| 298 | + { |
| 299 | + Assert::assertInternalType('array', $this->lastResult); |
| 300 | + } |
| 301 | + |
| 302 | + /** |
| 303 | + * Return the search manager using the configured adapter ID |
| 304 | + */ |
| 305 | + protected function getSearchManager() |
| 306 | + { |
| 307 | + return new SearchManager( |
| 308 | + $this->kernel->getContainer()->get($this->adapterId), |
| 309 | + $this->kernel->getContainer()->get('massive_search.metadata.factory'), |
| 310 | + $this->kernel->getContainer()->get('massive_search.object_to_document_converter'), |
| 311 | + $this->kernel->getContainer()->get('event_dispatcher'), |
| 312 | + $this->kernel->getContainer()->get('massive_search.localization_strategy') |
| 313 | + ); |
| 314 | + } |
| 315 | + |
| 316 | + /** |
| 317 | + * There is a timing issue, we need to pause for a while |
| 318 | + * after flusing for subsequent requests to be consistent. |
| 319 | + */ |
| 320 | + protected function pause() |
| 321 | + { |
| 322 | + usleep(50000); |
| 323 | + } |
| 324 | +} |
0 commit comments