/** * @param int $productId * @param \Magento\Review\Model\ResourceModel\Review\Collection $reviewsCollection * @return \Magento\Review\Model\Review[] */ public function getReviewsWithPurchaseDate($productId, $reviewsCollection) { $customersWithPurchaseDate = $this->resource->getCustomersWithWithPurchaseDate($productId, $this->getCustomerList($reviewsCollection)); /** @var \Magento\Review\Model\Review $review */ foreach ($reviewsCollection as $review) { if (isset($customersWithPurchaseDate[$review->getData('customer_id')])) { $review->setData('purchase_date', $customersWithPurchaseDate[$review->getData('customer_id')]); } } return $reviewsCollection; }
public function testGetCustomersWithWithPurchaseDate() { $productId = 12; $customerIds = [4, 6, 9]; $result = [4 => 'date 4', 6 => 'date 6', 9 => 'date 9']; $customerOrderItems = [$this->createOrderItem(4, 'date 4'), $this->createOrderItem(6, 'date 6'), $this->createOrderItem(9, 'date 9')]; $this->orderItemCollection->expects($this->once())->method('join')->with(['order' => 'sales_order'], 'main_table.order_id = order.entity_id', ['customer_id' => 'customer_id', 'purchase_date' => 'order.updated_at'])->willReturnSelf(); $this->orderItemCollection->expects($this->exactly(3))->method('addFieldToFilter')->withConsecutive(['main_table.product_id', $productId], ['order.status', 'complete'], ['order.customer_id', ['in' => $customerIds]])->willReturnSelf(); $this->orderItemCollection->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator($customerOrderItems)); $this->assertEquals($result, $this->buyer->getCustomersWithWithPurchaseDate($productId, $customerIds)); }
public function testGetReviewsWithPurchaseDate() { $productId = 15; $customerIds = [2, 4, null, 5, 9]; $customersWithPurchaseDate = [4 => 'date 4', 9 => 'date 9']; $reviews = [$this->createReviewModel(2, null), $this->createReviewModel(4, 'date 4'), $this->createReviewModel(null, null), $this->createReviewModel(5, null), $this->createReviewModel(9, 'date 9')]; $reviewCollection = $this->getMockBuilder('Magento\\Review\\Model\\ResourceModel\\Review\\Collection')->disableOriginalConstructor()->getMock(); $reviewCollection->expects($this->once())->method('getColumnValues')->with('customer_id')->willReturn($customerIds); $reviewCollection->expects($this->any())->method('getIterator')->willReturn(new \ArrayIterator($reviews)); $this->resource->expects($this->once())->method('getCustomersWithWithPurchaseDate')->with($productId, array_filter($customerIds))->willReturn($customersWithPurchaseDate); $this->assertSame($reviewCollection, $this->buyer->getReviewsWithPurchaseDate($productId, $reviewCollection)); }