|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\CustomerGraphQl\Test\Unit\Model\Resolver; |
| 10 | + |
| 11 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 12 | +use Magento\CustomerGraphQl\Model\Resolver\IsSubscribed; |
| 13 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 14 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 15 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 16 | +use Magento\GraphQl\Model\Query\ContextExtensionInterface; |
| 17 | +use Magento\GraphQl\Model\Query\ContextInterface; |
| 18 | +use Magento\Newsletter\Model\Subscriber; |
| 19 | +use Magento\Newsletter\Model\SubscriberFactory; |
| 20 | +use Magento\Store\Api\Data\StoreInterface; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | + |
| 24 | +/** |
| 25 | + * Test class for \Magento\CustomerGraphQl\Model\Resolver\IsSubscribed |
| 26 | + */ |
| 27 | +class IsSubscribedTest extends TestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * Object Manager Instance |
| 31 | + * |
| 32 | + * @var ObjectManager |
| 33 | + */ |
| 34 | + private $objectManager; |
| 35 | + |
| 36 | + /** |
| 37 | + * Testable Object |
| 38 | + * |
| 39 | + * @var IsSubscribed |
| 40 | + */ |
| 41 | + private $resolver; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var ContextInterface|MockObject |
| 45 | + */ |
| 46 | + private $contextMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var ContextExtensionInterface|MockObject |
| 50 | + */ |
| 51 | + private $contextExtensionMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var CustomerInterface|MockObject |
| 55 | + */ |
| 56 | + private $customerMock; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var SubscriberFactory|MockObject |
| 60 | + */ |
| 61 | + private $subscriberFactory; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var Subscriber|MockObject |
| 65 | + */ |
| 66 | + private $subscriber; |
| 67 | + |
| 68 | + /** |
| 69 | + * @var StoreInterface|MockObject |
| 70 | + */ |
| 71 | + private $storeMock; |
| 72 | + |
| 73 | + /** |
| 74 | + * @var Field|MockObject |
| 75 | + */ |
| 76 | + private $fieldMock; |
| 77 | + |
| 78 | + /** |
| 79 | + * @var ResolveInfo|MockObject |
| 80 | + */ |
| 81 | + private $resolveInfoMock; |
| 82 | + |
| 83 | + /** |
| 84 | + * @var array |
| 85 | + */ |
| 86 | + private array $valueMock = []; |
| 87 | + |
| 88 | + /** |
| 89 | + * @inheritdoc |
| 90 | + */ |
| 91 | + protected function setUp(): void |
| 92 | + { |
| 93 | + $this->objectManager = new ObjectManager($this); |
| 94 | + |
| 95 | + $this->contextMock = $this->getMockBuilder(ContextInterface::class) |
| 96 | + ->disableOriginalConstructor() |
| 97 | + ->getMockForAbstractClass(); |
| 98 | + |
| 99 | + $this->contextExtensionMock = $this->getMockBuilder(ContextExtensionInterface::class) |
| 100 | + ->disableOriginalConstructor() |
| 101 | + ->getMockForAbstractClass(); |
| 102 | + |
| 103 | + $this->customerMock = $this->getMockBuilder(CustomerInterface::class) |
| 104 | + ->disableOriginalConstructor() |
| 105 | + ->getMockForAbstractClass(); |
| 106 | + |
| 107 | + $this->storeMock = $this->getMockBuilder(StoreInterface::class) |
| 108 | + ->disableOriginalConstructor() |
| 109 | + ->getMockForAbstractClass(); |
| 110 | + |
| 111 | + $this->fieldMock = $this->getMockBuilder(Field::class) |
| 112 | + ->disableOriginalConstructor() |
| 113 | + ->getMock(); |
| 114 | + |
| 115 | + $this->subscriberFactory = $this->createMock(SubscriberFactory::class); |
| 116 | + $this->subscriber = $this->createMock(Subscriber::class); |
| 117 | + |
| 118 | + $this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class) |
| 119 | + ->disableOriginalConstructor() |
| 120 | + ->getMock(); |
| 121 | + |
| 122 | + $this->resolver = $this->objectManager->getObject( |
| 123 | + IsSubscribed::class, |
| 124 | + [ |
| 125 | + 'subscriberFactory' => $this->subscriberFactory |
| 126 | + ] |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Test customer is subscribed |
| 132 | + */ |
| 133 | + public function testCustomerIsSubscribed() |
| 134 | + { |
| 135 | + $subscriber = $this->createMock(Subscriber::class); |
| 136 | + $this->valueMock = ['model' => $this->customerMock]; |
| 137 | + $this->contextMock |
| 138 | + ->expects($this->once()) |
| 139 | + ->method('getExtensionAttributes') |
| 140 | + ->willReturn($this->contextExtensionMock); |
| 141 | + |
| 142 | + $this->contextExtensionMock |
| 143 | + ->expects($this->once()) |
| 144 | + ->method('getStore') |
| 145 | + ->willReturn($this->storeMock); |
| 146 | + |
| 147 | + $this->customerMock->expects($this->once()) |
| 148 | + ->method('getId') |
| 149 | + ->willReturn(1); |
| 150 | + |
| 151 | + $this->storeMock->expects($this->any()) |
| 152 | + ->method('getWebsiteId') |
| 153 | + ->willReturn(1); |
| 154 | + |
| 155 | + $this->assertNotNull($this->storeMock->getWebsiteId()); |
| 156 | + |
| 157 | + $this->subscriberFactory->expects($this->once()) |
| 158 | + ->method('create') |
| 159 | + ->willReturn($subscriber); |
| 160 | + |
| 161 | + $subscriber->expects($this->once()) |
| 162 | + ->method('loadByCustomer') |
| 163 | + ->willReturn($this->subscriber); |
| 164 | + |
| 165 | + $this->subscriber->expects($this->once()) |
| 166 | + ->method('isSubscribed') |
| 167 | + ->willReturn(true); |
| 168 | + |
| 169 | + $this->assertEquals( |
| 170 | + true, |
| 171 | + $this->resolver->resolve( |
| 172 | + $this->fieldMock, |
| 173 | + $this->contextMock, |
| 174 | + $this->resolveInfoMock, |
| 175 | + $this->valueMock |
| 176 | + ) |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Test subscription status will return false if store not found |
| 182 | + */ |
| 183 | + public function testCustomerIsSubscribedWithoutStore() |
| 184 | + { |
| 185 | + $this->valueMock = ['model' => $this->customerMock]; |
| 186 | + $this->contextMock |
| 187 | + ->expects($this->once()) |
| 188 | + ->method('getExtensionAttributes') |
| 189 | + ->willReturn($this->contextExtensionMock); |
| 190 | + |
| 191 | + $this->assertEquals( |
| 192 | + false, |
| 193 | + $this->resolver->resolve( |
| 194 | + $this->fieldMock, |
| 195 | + $this->contextMock, |
| 196 | + $this->resolveInfoMock, |
| 197 | + $this->valueMock |
| 198 | + ) |
| 199 | + ); |
| 200 | + } |
| 201 | +} |
0 commit comments