|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\WishlistGraphQl\Test\Unit\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 11 | +use Magento\GraphQl\Model\Query\ContextInterface; |
| 12 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 13 | +use Magento\GraphQl\Model\Query\ContextExtensionInterface; |
| 14 | +use Magento\Store\Api\Data\StoreInterface; |
| 15 | +use Magento\Store\Model\StoreManagerInterface; |
| 16 | +use Magento\Wishlist\Model\ResourceModel\Item; |
| 17 | +use Magento\Wishlist\Model\ResourceModel\Item\Collection as WishlistItemCollection; |
| 18 | +use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistItemCollectionFactory; |
| 19 | +use Magento\Wishlist\Model\Wishlist; |
| 20 | +use Magento\WishlistGraphQl\Model\Resolver\WishlistItems; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | + |
| 24 | +class WishlistItemsTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var WishlistItemCollectionFactory|MockObject |
| 28 | + */ |
| 29 | + private WishlistItemCollectionFactory $wishlistItemCollectionFactory; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var StoreManagerInterface|MockObject |
| 33 | + */ |
| 34 | + private StoreManagerInterface $storeManager; |
| 35 | + |
| 36 | + /** |
| 37 | + * @return void |
| 38 | + * @throws \PHPUnit\Framework\MockObject\Exception |
| 39 | + */ |
| 40 | + protected function setUp(): void |
| 41 | + { |
| 42 | + $this->wishlistItemCollectionFactory = $this->createMock(WishlistItemCollectionFactory::class); |
| 43 | + $this->storeManager = $this->createMock(StoreManagerInterface::class); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @return void |
| 48 | + * @throws \PHPUnit\Framework\MockObject\Exception |
| 49 | + */ |
| 50 | + public function testResolve(): void |
| 51 | + { |
| 52 | + $storeId = $itemId = 1; |
| 53 | + |
| 54 | + $field = $this->createMock(Field::class); |
| 55 | + $context = $this->getMockBuilder(ContextInterface::class) |
| 56 | + ->disableOriginalConstructor() |
| 57 | + ->getMock(); |
| 58 | + $store = $this->createMock(StoreInterface::class); |
| 59 | + $store->expects($this->once())->method('getId')->willReturn($storeId); |
| 60 | + |
| 61 | + $extensionAttributes = $this->getMockBuilder(ContextExtensionInterface::class) |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->addMethods(['getStore']) |
| 64 | + ->getMock(); |
| 65 | + $extensionAttributes->expects($this->exactly(2)) |
| 66 | + ->method('getStore') |
| 67 | + ->willReturn($store); |
| 68 | + $context->expects($this->exactly(2)) |
| 69 | + ->method('getExtensionAttributes') |
| 70 | + ->willReturn($extensionAttributes); |
| 71 | + $info = $this->createMock(ResolveInfo::class); |
| 72 | + $wishlist = $this->createMock(Wishlist::class); |
| 73 | + |
| 74 | + $item = $this->getMockBuilder(Item::class) |
| 75 | + ->addMethods(['getId', 'getData', 'getDescription', 'getAddedAt', 'getProduct']) |
| 76 | + ->disableOriginalConstructor() |
| 77 | + ->getMock(); |
| 78 | + $item->expects($this->once())->method('getId')->willReturn($itemId); |
| 79 | + $item->expects($this->once())->method('getData')->with('qty'); |
| 80 | + $item->expects($this->once())->method('getDescription'); |
| 81 | + $item->expects($this->once())->method('getAddedAt'); |
| 82 | + $item->expects($this->once())->method('getProduct'); |
| 83 | + |
| 84 | + $wishlistCollection = $this->createMock(WishlistItemCollection::class); |
| 85 | + $wishlistCollection->expects($this->once()) |
| 86 | + ->method('addWishlistFilter') |
| 87 | + ->willReturnSelf(); |
| 88 | + $wishlistCollection->expects($this->once()) |
| 89 | + ->method('addStoreFilter') |
| 90 | + ->with($storeId) |
| 91 | + ->willReturnSelf(); |
| 92 | + $wishlistCollection->expects($this->once())->method('setVisibilityFilter')->willReturnSelf(); |
| 93 | + $wishlistCollection->expects($this->once())->method('setCurPage')->willReturnSelf(); |
| 94 | + $wishlistCollection->expects($this->once())->method('setPageSize')->willReturnSelf(); |
| 95 | + $wishlistCollection->expects($this->once())->method('getItems')->willReturn([$item]); |
| 96 | + $wishlistCollection->expects($this->once())->method('getCurPage'); |
| 97 | + $wishlistCollection->expects($this->once())->method('getPageSize'); |
| 98 | + $wishlistCollection->expects($this->once())->method('getLastPageNumber'); |
| 99 | + $this->wishlistItemCollectionFactory->expects($this->once()) |
| 100 | + ->method('create') |
| 101 | + ->willReturn($wishlistCollection); |
| 102 | + |
| 103 | + $resolver = new WishlistItems($this->wishlistItemCollectionFactory, $this->storeManager); |
| 104 | + $resolver->resolve($field, $context, $info, ['model' => $wishlist]); |
| 105 | + } |
| 106 | +} |
0 commit comments