BuilderTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Rule\Model\Condition\Sql;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
  9. use Magento\CatalogWidget\Model\RuleFactory;
  10. use Magento\CatalogWidget\Model\Rule\Condition\Combine as CombineCondition;
  11. use Magento\CatalogWidget\Model\Rule\Condition\Product as ProductCondition;
  12. /**
  13. * Test for Magento\Rule\Model\Condition\Sql\Builder
  14. */
  15. class BuilderTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var Builder
  19. */
  20. private $model;
  21. protected function setUp()
  22. {
  23. $this->model = Bootstrap::getObjectManager()->create(Builder::class);
  24. }
  25. /**
  26. * @return void
  27. */
  28. public function testAttachConditionToCollection(): void
  29. {
  30. /** @var ProductCollectionFactory $collectionFactory */
  31. $collectionFactory = Bootstrap::getObjectManager()->create(ProductCollectionFactory::class);
  32. $collection = $collectionFactory->create();
  33. /** @var RuleFactory $ruleFactory */
  34. $ruleFactory = Bootstrap::getObjectManager()->create(RuleFactory::class);
  35. $rule = $ruleFactory->create();
  36. $ruleConditionArray = [
  37. 'conditions' => [
  38. '1' => [
  39. 'type' => CombineCondition::class,
  40. 'aggregator' => 'all',
  41. 'value' => '1',
  42. 'new_child' => '',
  43. ],
  44. '1--1' => [
  45. 'type' => ProductCondition::class,
  46. 'attribute' => 'category_ids',
  47. 'operator' => '==',
  48. 'value' => '3',
  49. ],
  50. '1--2' => [
  51. 'type' => ProductCondition::class,
  52. 'attribute' => 'special_to_date',
  53. 'operator' => '==',
  54. 'value' => '2017-09-15',
  55. ],
  56. ],
  57. ];
  58. $rule->loadPost($ruleConditionArray);
  59. $this->model->attachConditionToCollection($collection, $rule->getConditions());
  60. $whereString = "/\(category_id IN \('3'\).+\(IFNULL\(`e`\.`entity_id`,.+\) = '2017-09-15'\)/";
  61. $this->assertNotFalse(preg_match($whereString, $collection->getSelectSql(true)));
  62. }
  63. }