Advertisement
Guest User

Untitled

a guest
Sep 12th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.48 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Oro\Bundle\WebsiteSearchBundle\Tests\Functional\Engine\ORM;
  4.  
  5. use Doctrine\Common\Persistence\ObjectManager;
  6.  
  7. use Oro\Bundle\FrontendBundle\Request\FrontendHelper;
  8. use Oro\Bundle\SearchBundle\Query\Criteria\Comparison;
  9. use Oro\Bundle\SearchBundle\Query\Criteria\Criteria;
  10. use Oro\Bundle\SearchBundle\Query\Query;
  11. use Oro\Bundle\SearchBundle\Query\Result;
  12. use Oro\Bundle\SearchBundle\Query\Result\Item;
  13. use Oro\Bundle\SearchBundle\Tests\Functional\Controller\DataFixtures\LoadSearchItemData;
  14. use Oro\Bundle\TestFrameworkBundle\Test\WebTestCase;
  15. use Oro\Bundle\WebsiteSearchBundle\Engine\ORM\OrmEngine;
  16. use Oro\Bundle\WebsiteSearchBundle\Event\IndexEntityEvent;
  17. use Oro\Bundle\WebsiteSearchBundle\Tests\Functional\SearchTestTrait;
  18. use Oro\Bundle\TestFrameworkBundle\Entity\Item as TestEntity;
  19.  
  20. /**
  21. * @dbIsolationPerTest
  22. */
  23. class OrmEngineTest extends WebTestCase
  24. {
  25. use SearchTestTrait;
  26.  
  27. /**
  28. * @var callable
  29. */
  30. protected $listener;
  31.  
  32. /**
  33. * @var OrmEngine
  34. */
  35. protected $ormEngine;
  36.  
  37. /**
  38. * @var ObjectManager
  39. */
  40. protected $manager;
  41.  
  42. /**
  43. * @var TestEntity[]
  44. */
  45. protected $expectedSearchItems = [];
  46.  
  47. /**
  48. * @var array
  49. */
  50. protected $expectedEntityConfig = [
  51. 'alias' => 'oro_test_item_WEBSITE_ID',
  52. 'fields' => [
  53. [
  54. 'name' => 'stringValue_LOCALIZATION_ID',
  55. 'type' => 'text',
  56. ],
  57. [
  58. 'name' => 'integerValue',
  59. 'type' => 'integer',
  60. ],
  61. [
  62. 'name' => 'decimalValue',
  63. 'type' => 'decimal',
  64. ],
  65. [
  66. 'name' => 'floatValue',
  67. 'type' => 'decimal',
  68. ],
  69. [
  70. 'name' => 'datetimeValue',
  71. 'type' => 'datetime',
  72. ],
  73. [
  74. 'name' => 'blobValue',
  75. 'type' => 'text',
  76. ],
  77. [
  78. 'name' => 'phone',
  79. 'type' => 'text',
  80. ],
  81. ],
  82. ];
  83.  
  84. protected function setUp()
  85. {
  86. $this->initClient();
  87.  
  88. if ($this->getContainer()->getParameter('oro_website_search.engine') != 'orm') {
  89. $this->markTestSkipped('Should be tested only with ORM search engine');
  90. }
  91.  
  92. /** @var FrontendHelper|\PHPUnit_Framework_MockObject_MockObject */
  93. $frontendHelperMock = $this->getMockBuilder(FrontendHelper::class)
  94. ->disableOriginalConstructor()
  95. ->getMock();
  96.  
  97. $frontendHelperMock->expects($this->any())
  98. ->method('isFrontendRequest')
  99. ->willReturn(true);
  100.  
  101. $this->getContainer()->set('orob2b_frontend.request.frontend_helper', $frontendHelperMock);
  102.  
  103. $this->loadFixtures([LoadSearchItemData::class]);
  104.  
  105. $this->listener = $this->setListener();
  106.  
  107. $indexer = $this->getContainer()->get('oro_website_search.indexer');
  108. $indexer->reindex(TestEntity::class, []);
  109.  
  110. $this->ormEngine = $this->getContainer()->get('oro_website_search.engine');
  111. $this->manager = $this->getContainer()->get('doctrine')->getManager();
  112.  
  113. $this->expectedSearchItems = [];
  114. $this->expectedSearchItems[] = $this->getReference('searchItem1');
  115. $this->expectedSearchItems[] = $this->getReference('searchItem2');
  116. $this->expectedSearchItems[] = $this->getReference('searchItem3');
  117. $this->expectedSearchItems[] = $this->getReference('searchItem4');
  118. $this->expectedSearchItems[] = $this->getReference('searchItem5');
  119. $this->expectedSearchItems[] = $this->getReference('searchItem6');
  120. $this->expectedSearchItems[] = $this->getReference('searchItem7');
  121. $this->expectedSearchItems[] = $this->getReference('searchItem8');
  122. $this->expectedSearchItems[] = $this->getReference('searchItem9');
  123. }
  124.  
  125. protected function tearDown()
  126. {
  127. $this->truncateIndexTextTable();
  128.  
  129. unset($this->listener, $this->ormEngine, $this->manager, $this->expectedSearchItems);
  130. }
  131.  
  132. /**
  133. * @return callable
  134. */
  135. private function setListener()
  136. {
  137. $listener = function (IndexEntityEvent $event) {
  138. $items = $this->getContainer()->get('doctrine')
  139. ->getRepository(TestEntity::class)
  140. ->findBy(['id' => $event->getEntityIds()]);
  141.  
  142. foreach ($items as $item) {
  143. $event->addField(
  144. $item->getId(),
  145. Query::TYPE_INTEGER,
  146. 'integerValue',
  147. $item->integerValue
  148. );
  149. $event->addField(
  150. $item->getId(),
  151. Query::TYPE_DECIMAL,
  152. 'decimalValue',
  153. $item->decimalValue
  154. );
  155. $event->addField(
  156. $item->getId(),
  157. Query::TYPE_DECIMAL,
  158. 'floatValue',
  159. $item->floatValue
  160. );
  161. $event->addField(
  162. $item->getId(),
  163. Query::TYPE_DATETIME,
  164. 'datetimeValue',
  165. $item->datetimeValue
  166. );
  167. $event->addField(
  168. $item->getId(),
  169. Query::TYPE_TEXT,
  170. 'stringValue_1',
  171. $item->stringValue
  172. );
  173. $event->addField(
  174. $item->getId(),
  175. Query::TYPE_TEXT,
  176. 'all_text_1',
  177. $item->stringValue
  178. );
  179. $event->addField(
  180. $item->getId(),
  181. Query::TYPE_TEXT,
  182. 'phone',
  183. $item->phone
  184. );
  185. $event->addField(
  186. $item->getId(),
  187. Query::TYPE_TEXT,
  188. 'blobValue',
  189. (string)$item->blobValue
  190. );
  191. }
  192. };
  193.  
  194. $this->getContainer()->get('event_dispatcher')->addListener(
  195. IndexEntityEvent::NAME,
  196. $listener,
  197. -255
  198. );
  199.  
  200. return $listener;
  201. }
  202.  
  203. public function testSearchAll()
  204. {
  205. $query = new Query();
  206. $query->from('*');
  207.  
  208. $expectedResult = new Result(
  209. $query,
  210. [
  211. new Item(
  212. $this->manager,
  213. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  214. $this->expectedSearchItems[0]->getId(),
  215. 'item1@mail.com',
  216. null,
  217. [],
  218. $this->expectedEntityConfig
  219. ),
  220. new Item(
  221. $this->manager,
  222. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  223. $this->expectedSearchItems[1]->getId(),
  224. 'item2@mail.com',
  225. null,
  226. [],
  227. $this->expectedEntityConfig
  228. ),
  229. new Item(
  230. $this->manager,
  231. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  232. $this->expectedSearchItems[2]->getId(),
  233. 'item3@mail.com',
  234. null,
  235. [],
  236. $this->expectedEntityConfig
  237. ),
  238. new Item(
  239. $this->manager,
  240. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  241. $this->expectedSearchItems[3]->getId(),
  242. 'item4@mail.com',
  243. null,
  244. [],
  245. $this->expectedEntityConfig
  246. ),
  247. new Item(
  248. $this->manager,
  249. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  250. $this->expectedSearchItems[4]->getId(),
  251. 'item5@mail.com',
  252. null,
  253. [],
  254. $this->expectedEntityConfig
  255. ),
  256. new Item(
  257. $this->manager,
  258. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  259. $this->expectedSearchItems[5]->getId(),
  260. 'item6@mail.com',
  261. null,
  262. [],
  263. $this->expectedEntityConfig
  264. ),
  265. new Item(
  266. $this->manager,
  267. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  268. $this->expectedSearchItems[6]->getId(),
  269. 'item7@mail.com',
  270. null,
  271. [],
  272. $this->expectedEntityConfig
  273. ),
  274. new Item(
  275. $this->manager,
  276. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  277. $this->expectedSearchItems[7]->getId(),
  278. 'item8@mail.com',
  279. null,
  280. [],
  281. $this->expectedEntityConfig
  282. ),
  283. new Item(
  284. $this->manager,
  285. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  286. $this->expectedSearchItems[8]->getId(),
  287. 'item9@mail.com',
  288. null,
  289. [],
  290. $this->expectedEntityConfig
  291. ),
  292. ],
  293. 9
  294. );
  295.  
  296. $this->assertEquals($expectedResult, $this->ormEngine->search($query));
  297. }
  298.  
  299. /**
  300. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  301. */
  302. public function testSearchByAliasWithSelect()
  303. {
  304. $query = new Query();
  305. $query->from('oro_test_item_website_WEBSITE_ID');
  306. $query->select('stringValue_1');
  307.  
  308. $expectedResult = new Result(
  309. $query,
  310. [
  311. new Item(
  312. $this->manager,
  313. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  314. $this->expectedSearchItems[0]->getId(),
  315. 'item1@mail.com',
  316. null,
  317. [
  318. 'stringValue_1' => 'item1@mail.com',
  319. ],
  320. $this->expectedEntityConfig
  321. ),
  322. new Item(
  323. $this->manager,
  324. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  325. $this->expectedSearchItems[1]->getId(),
  326. 'item2@mail.com',
  327. null,
  328. [
  329. 'stringValue_1' => 'item2@mail.com',
  330. ],
  331. $this->expectedEntityConfig
  332. ),
  333. new Item(
  334. $this->manager,
  335. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  336. $this->expectedSearchItems[2]->getId(),
  337. 'item3@mail.com',
  338. null,
  339. [
  340. 'stringValue_1' => 'item3@mail.com',
  341. ],
  342. $this->expectedEntityConfig
  343. ),
  344. new Item(
  345. $this->manager,
  346. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  347. $this->expectedSearchItems[3]->getId(),
  348. 'item4@mail.com',
  349. null,
  350. [
  351. 'stringValue_1' => 'item4@mail.com',
  352. ],
  353. $this->expectedEntityConfig
  354. ),
  355. new Item(
  356. $this->manager,
  357. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  358. $this->expectedSearchItems[4]->getId(),
  359. 'item5@mail.com',
  360. null,
  361. [
  362. 'stringValue_1' => 'item5@mail.com',
  363. ],
  364. $this->expectedEntityConfig
  365. ),
  366. new Item(
  367. $this->manager,
  368. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  369. $this->expectedSearchItems[5]->getId(),
  370. 'item6@mail.com',
  371. null,
  372. [
  373. 'stringValue_1' => 'item6@mail.com',
  374. ],
  375. $this->expectedEntityConfig
  376. ),
  377. new Item(
  378. $this->manager,
  379. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  380. $this->expectedSearchItems[6]->getId(),
  381. 'item7@mail.com',
  382. null,
  383. [
  384. 'stringValue_1' => 'item7@mail.com',
  385. ],
  386. $this->expectedEntityConfig
  387. ),
  388. new Item(
  389. $this->manager,
  390. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  391. $this->expectedSearchItems[7]->getId(),
  392. 'item8@mail.com',
  393. null,
  394. [
  395. 'stringValue_1' => 'item8@mail.com',
  396. ],
  397. $this->expectedEntityConfig
  398. ),
  399. new Item(
  400. $this->manager,
  401. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  402. $this->expectedSearchItems[8]->getId(),
  403. 'item9@mail.com',
  404. null,
  405. [
  406. 'stringValue_1' => 'item9@mail.com',
  407. ],
  408. $this->expectedEntityConfig
  409. ),
  410. ],
  411. 9
  412. );
  413.  
  414. $this->assertEquals($expectedResult, $this->ormEngine->search($query));
  415. }
  416.  
  417. public function testSearchByAliasWithCriteria()
  418. {
  419. $query = new Query();
  420. $query->from('oro_test_item_website_WEBSITE_ID');
  421. $expr = new Comparison("integer.integerValue", "=", 5000);
  422. $criteria = new Criteria();
  423. $criteria->where($expr);
  424. $query->setCriteria($criteria);
  425.  
  426. $expectedResult = new Result(
  427. $query,
  428. [
  429. new Item(
  430. $this->manager,
  431. 'Oro\Bundle\TestFrameworkBundle\Entity\Item',
  432. $this->expectedSearchItems[4]->getId(),
  433. 'item5@mail.com',
  434. null,
  435. [],
  436. $this->expectedEntityConfig
  437. ),
  438. ],
  439. 1
  440. );
  441.  
  442. $this->assertEquals($expectedResult, $this->ormEngine->search($query));
  443. }
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement