|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Copyright © Magento, Inc. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\GiftMessageGraphQl\Test\Unit\Model\Resolver\Order; |
| 10 | + |
| 11 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 12 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 13 | +use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; |
| 14 | +use Magento\Framework\GraphQl\Query\Resolver\Value; |
| 15 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 16 | +use Magento\Framework\GraphQl\Query\Uid; |
| 17 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 18 | +use Magento\GiftMessage\Api\Data\MessageInterface; |
| 19 | +use Magento\GiftMessage\Api\OrderRepositoryInterface; |
| 20 | +use Magento\GiftMessageGraphQl\Model\Resolver\Order\GiftMessage; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | +use Psr\Log\LoggerInterface; |
| 24 | + |
| 25 | +class GiftMessageTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var GiftMessage |
| 29 | + */ |
| 30 | + private GiftMessage $giftMessage; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Field|MockObject |
| 34 | + */ |
| 35 | + private Field $fieldMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var ContextInterface|MockObject |
| 39 | + */ |
| 40 | + private ContextInterface $contextMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ResolverInterface|MockObject |
| 44 | + */ |
| 45 | + private ResolverInterface $resolverMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var ResolveInfo|MockObject |
| 49 | + */ |
| 50 | + private ResolveInfo $resolveInfoMock; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var OrderRepositoryInterface|MockObject |
| 54 | + */ |
| 55 | + private OrderRepositoryInterface $orderRepositoryMock; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var MessageInterface|MockObject |
| 59 | + */ |
| 60 | + private MessageInterface $messageMock; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var array |
| 64 | + */ |
| 65 | + private array $valueMock = []; |
| 66 | + |
| 67 | + protected function setUp(): void |
| 68 | + { |
| 69 | + $this->fieldMock = $this->createMock(Field::class); |
| 70 | + $this->contextMock = $this->createMock(ContextInterface::class); |
| 71 | + $this->resolverMock = $this->createMock(ResolverInterface::class); |
| 72 | + $this->resolveInfoMock = $this->createMock(ResolveInfo::class); |
| 73 | + $this->orderRepositoryMock =$this->createMock(OrderRepositoryInterface::class); |
| 74 | + $logger = $this->createMock(LoggerInterface::class); |
| 75 | + $uidEncoder = $this->createMock(Uid::class); |
| 76 | + $this->messageMock = $this->createMock(MessageInterface::class); |
| 77 | + $this->giftMessage = new GiftMessage( |
| 78 | + $this->orderRepositoryMock, |
| 79 | + $logger, |
| 80 | + $uidEncoder |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @throws GraphQlInputException |
| 86 | + */ |
| 87 | + public function testResolveWithoutIDInValueParameter(): void |
| 88 | + { |
| 89 | + $this->expectException(GraphQlInputException::class); |
| 90 | + $this->expectExceptionMessage('"id" value should be specified'); |
| 91 | + $this->giftMessage->resolve($this->fieldMock, $this->contextMock, $this->resolveInfoMock, $this->valueMock); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @throws GraphQlInputException |
| 96 | + */ |
| 97 | + public function testResolve(): void |
| 98 | + { |
| 99 | + $this->valueMock = ['id' => "111"]; |
| 100 | + $this->orderRepositoryMock |
| 101 | + ->expects($this->once()) |
| 102 | + ->method('get') |
| 103 | + ->willReturn($this->messageMock); |
| 104 | + |
| 105 | + $this->messageMock |
| 106 | + ->expects($this->once()) |
| 107 | + ->method('getGiftMessageId') |
| 108 | + ->willReturn(null); |
| 109 | + |
| 110 | + $this->assertEquals( |
| 111 | + null, |
| 112 | + $this->giftMessage->resolve($this->fieldMock, $this->contextMock, $this->resolveInfoMock, $this->valueMock) |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @return array|Value|mixed |
| 118 | + * @throws GraphQlInputException |
| 119 | + */ |
| 120 | + public function testResolveWithMessageId(): void |
| 121 | + { |
| 122 | + $this->valueMock = ['id' => "112"]; |
| 123 | + $this->orderRepositoryMock |
| 124 | + ->expects($this->once()) |
| 125 | + ->method('get') |
| 126 | + ->willReturn($this->messageMock); |
| 127 | + |
| 128 | + $this->messageMock |
| 129 | + ->expects($this->once()) |
| 130 | + ->method('getGiftMessageId') |
| 131 | + ->willReturn(1); |
| 132 | + |
| 133 | + $this->assertEquals( |
| 134 | + [ |
| 135 | + 'to' => '', |
| 136 | + 'from' => '', |
| 137 | + 'message' =>'' |
| 138 | + ], |
| 139 | + $this->giftMessage->resolve($this->fieldMock, $this->contextMock, $this->resolveInfoMock, $this->valueMock) |
| 140 | + ); |
| 141 | + } |
| 142 | +} |
0 commit comments