|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\QuoteGraphQl\Test\Unit\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Framework\Exception\LocalizedException; |
| 11 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 12 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 13 | +use Magento\GraphQl\Model\Query\Context; |
| 14 | +use Magento\Quote\Model\Quote; |
| 15 | +use Magento\Quote\Model\QuoteIdMask; |
| 16 | +use Magento\Quote\Model\QuoteIdMaskFactory; |
| 17 | +use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; |
| 18 | +use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResourceModel; |
| 19 | +use Magento\QuoteGraphQl\Model\Resolver\Cart; |
| 20 | +use Magento\QuoteGraphQl\Model\Resolver\MaskedCartId; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | + |
| 24 | +class MaskedCartIdTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var MaskedCartId |
| 28 | + */ |
| 29 | + private MaskedCartId $maskedCartId; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var QuoteIdToMaskedQuoteIdInterface|MockObject |
| 33 | + */ |
| 34 | + private QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var \Magento\QuoteGraphQl\Test\Unit\Model\Resolver\QuoteIdMaskFactory|MockObject |
| 38 | + */ |
| 39 | + private QuoteIdMaskFactory $quoteIdMaskFactory; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var QuoteIdMaskResourceModel|MockObject |
| 43 | + */ |
| 44 | + private QuoteIdMaskResourceModel $quoteIdMaskResourceModelMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var Field|MockObject |
| 48 | + */ |
| 49 | + private Field $fieldMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var ResolveInfo|MockObject |
| 53 | + */ |
| 54 | + private ResolveInfo $resolveInfoMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var Context|MockObject |
| 58 | + */ |
| 59 | + private Context $contextMock; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var Quote|MockObject |
| 63 | + */ |
| 64 | + private Quote $quoteMock; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var QuoteIdMask|MockObject |
| 68 | + */ |
| 69 | + private QuoteIdMask $quoteIdMask; |
| 70 | + |
| 71 | + /** |
| 72 | + * @var array |
| 73 | + */ |
| 74 | + private array $valueMock = []; |
| 75 | + |
| 76 | + protected function setUp(): void |
| 77 | + { |
| 78 | + $this->fieldMock = $this->createMock(Field::class); |
| 79 | + $this->resolveInfoMock = $this->createMock(ResolveInfo::class); |
| 80 | + $this->contextMock = $this->createMock(Context::class); |
| 81 | + $this->quoteIdToMaskedQuoteId = $this->createPartialMock( |
| 82 | + QuoteIdToMaskedQuoteIdInterface::class, |
| 83 | + ['execute'] |
| 84 | + ); |
| 85 | + $this->quoteIdMaskFactory = $this->createPartialMock( |
| 86 | + QuoteIdMaskFactory::class, |
| 87 | + ['create'] |
| 88 | + ); |
| 89 | + $this->quoteIdMaskResourceModelMock = $this->getMockBuilder(QuoteIdMaskResourceModel::class) |
| 90 | + ->disableOriginalConstructor() |
| 91 | + ->addMethods( |
| 92 | + [ |
| 93 | + 'setQuoteId', |
| 94 | + ] |
| 95 | + ) |
| 96 | + ->onlyMethods(['save']) |
| 97 | + ->getMock(); |
| 98 | + $this->maskedCartId = new MaskedCartId( |
| 99 | + $this->quoteIdToMaskedQuoteId, |
| 100 | + $this->quoteIdMaskFactory, |
| 101 | + $this->quoteIdMaskResourceModelMock |
| 102 | + ); |
| 103 | + $this->quoteMock = $this->getMockBuilder(Quote::class) |
| 104 | + ->disableOriginalConstructor() |
| 105 | + ->getMock(); |
| 106 | + $this->quoteIdMask = $this->getMockBuilder(QuoteIdMask::class) |
| 107 | + ->disableOriginalConstructor() |
| 108 | + ->getMock(); |
| 109 | + } |
| 110 | + |
| 111 | + public function testResolveWithoutModelInValueParameter(): void |
| 112 | + { |
| 113 | + $this->expectException(LocalizedException::class); |
| 114 | + $this->expectExceptionMessage('"model" value should be specified'); |
| 115 | + $this->maskedCartId->resolve($this->fieldMock, $this->contextMock, $this->resolveInfoMock, $this->valueMock); |
| 116 | + } |
| 117 | + |
| 118 | + public function testResolve(): void |
| 119 | + { |
| 120 | + $this->valueMock = ['model' => $this->quoteMock]; |
| 121 | + $cartId = 1; |
| 122 | + $this->quoteMock |
| 123 | + ->expects($this->once()) |
| 124 | + ->method('getId') |
| 125 | + ->willReturn($cartId); |
| 126 | + $this->quoteIdMaskFactory |
| 127 | + ->expects($this->once()) |
| 128 | + ->method('create') |
| 129 | + ->willReturn($this->quoteIdMask); |
| 130 | + $this->quoteIdMask->setQuoteId($cartId); |
| 131 | + $this->quoteIdMaskResourceModelMock |
| 132 | + ->expects($this->once()) |
| 133 | + ->method('save') |
| 134 | + ->with($this->quoteIdMask); |
| 135 | + $this->maskedCartId->resolve($this->fieldMock, $this->contextMock, $this->resolveInfoMock, $this->valueMock); |
| 136 | + } |
| 137 | +} |
0 commit comments