Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SynchronizerTest.php
Go to the documentation of this file.
1 <?php
7 
11 
15 class SynchronizerTest extends \PHPUnit\Framework\TestCase
16 {
18  protected $model;
19 
22 
24  protected $sessionMock;
25 
27  protected $visitorMock;
28 
31 
33  protected $entityManagerMock;
34 
37 
40 
41  protected function setUp()
42  {
43  $this->sessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
44  ->disableOriginalConstructor()
45  ->getMock();
46  $this->visitorMock = $this->getMockBuilder(\Magento\Customer\Model\Visitor::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49  $this->productFrontendActionFactoryMock = $this
50  ->getMockBuilder(\Magento\Catalog\Model\ProductFrontendActionFactory::class)
51  ->disableOriginalConstructor()
52  ->setMethods(['create'])
53  ->getMock();
54  $this->entityManagerMock = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityManager::class)
55  ->disableOriginalConstructor()
56  ->getMock();
57  $this->collectionFactoryMock = $this
58  ->getMockBuilder(\Magento\Catalog\Model\ResourceModel\ProductFrontendAction\CollectionFactory::class)
59  ->disableOriginalConstructor()
60  ->setMethods(['create'])
61  ->getMock();
62  $this->frontendStorageConfigurationPoolMock = $this
63  ->getMockBuilder(\Magento\Catalog\Model\FrontendStorageConfigurationPool::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66 
67  $this->objectManagerHelper = new ObjectManagerHelper($this);
68  $this->model = $this->objectManagerHelper->getObject(
69  \Magento\Catalog\Model\Product\ProductFrontendAction\Synchronizer::class,
70  [
71  'session' => $this->sessionMock,
72  'visitor' => $this->visitorMock,
73  'productFrontendActionFactory' => $this->productFrontendActionFactoryMock,
74  'entityManager' => $this->entityManagerMock,
75  'collectionFactory' => $this->collectionFactoryMock,
76  'frontendStorageConfigurationPool' => $this->frontendStorageConfigurationPoolMock
77  ]
78  );
79  }
80 
81  public function testFilterProductActions()
82  {
83  $productsData = [
84  1 => [
85  'added_at' => 12,
86  'product_id' => 1,
87  ],
88  2 => [
89  'added_at' => 13,
90  'product_id' => 2,
91  ],
92  3 => [
93  'added_at' => 14,
94  'product_id' => 3,
95  ]
96  ];
97  $frontendConfiguration = $this->createMock(\Magento\Catalog\Model\FrontendStorageConfigurationInterface::class);
98  $frontendConfiguration->expects($this->once())
99  ->method('get')
100  ->willReturn([
101  'lifetime' => 2
102  ]);
103  $this->frontendStorageConfigurationPoolMock->expects($this->once())
104  ->method('get')
105  ->with('recently_compared_product')
106  ->willReturn($frontendConfiguration);
107  $action1 = $this->getMockBuilder(ProductFrontendActionInterface::class)
108  ->disableOriginalConstructor()
109  ->getMockForAbstractClass();
110  $action2 = $this->getMockBuilder(ProductFrontendActionInterface::class)
111  ->getMockForAbstractClass();
112 
113  $frontendAction = $this->createMock(ProductFrontendActionInterface::class);
114  $collection = $this->getMockBuilder(Collection::class)
115  ->disableOriginalConstructor()
116  ->getMock();
117  $this->sessionMock->expects($this->any())
118  ->method('getCustomerId')
119  ->willReturn(1);
120  $this->visitorMock->expects($this->exactly(2))
121  ->method('getId')
122  ->willReturn(34);
123  $this->collectionFactoryMock->expects($this->once())
124  ->method('create')
125  ->willReturn($collection);
126  $collection->expects($this->once())
127  ->method('addFilterByUserIdentities')
128  ->with(1, 34);
129  $collection->expects($this->any())
130  ->method('addFieldToFilter')
131  ->withConsecutive(['type_id'], ['product_id']);
132 
133  $iterator = new \IteratorIterator(new \ArrayIterator([$frontendAction]));
134  $collection->expects($this->once())
135  ->method('getIterator')
136  ->willReturn($iterator);
137  $this->entityManagerMock->expects($this->once())
138  ->method('delete')
139  ->with($frontendAction);
140  $this->productFrontendActionFactoryMock->expects($this->exactly(2))
141  ->method('create')
142  ->withConsecutive(
143  [
144  [
145  'data' => [
146  'visitor_id' => null,
147  'customer_id' => 1,
148  'added_at' => 12,
149  'product_id' => 1,
150  'type_id' => 'recently_compared_product'
151  ]
152  ]
153  ],
154  [
155  [
156  'data' => [
157  'visitor_id' => null,
158  'customer_id' => 1,
159  'added_at' => 13,
160  'product_id' => 2,
161  'type_id' => 'recently_compared_product'
162  ]
163  ]
164  ]
165  )
166  ->willReturnOnConsecutiveCalls($action1, $action2);
167  $this->entityManagerMock->expects($this->exactly(2))
168  ->method('save')
169  ->withConsecutive([$action1], [$action2]);
170  $this->model->syncActions($productsData, 'recently_compared_product');
171  }
172 }
$productsData
Definition: products.php:19