|
| 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\CatalogGraphQl\Test\Unit\Model\Resolver\Product; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\Catalog\Model\Product\Gallery\Entry; |
| 13 | +use Magento\CatalogGraphQl\Model\Resolver\Product\MediaGallery; |
| 14 | +use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; |
| 15 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 19 | + |
| 20 | +class MediaGalleryTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var Field|MockObject |
| 24 | + */ |
| 25 | + private Field|MockObject $fieldMock; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ContextInterface|MockObject |
| 29 | + */ |
| 30 | + private ContextInterface|MockObject $contextMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ResolveInfo|MockObject |
| 34 | + */ |
| 35 | + private ResolveInfo|MockObject $infoMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Product|MockObject |
| 39 | + */ |
| 40 | + private Product|MockObject $productMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var MediaGallery |
| 44 | + */ |
| 45 | + private MediaGallery $mediaGallery; |
| 46 | + |
| 47 | + protected function setUp(): void |
| 48 | + { |
| 49 | + $this->fieldMock = $this->createMock(Field::class); |
| 50 | + $this->contextMock = $this->getMockForAbstractClass(ContextInterface::class); |
| 51 | + $this->infoMock = $this->createMock(ResolveInfo::class); |
| 52 | + $this->productMock = $this->getMockBuilder(Product::class) |
| 53 | + ->disableOriginalConstructor() |
| 54 | + ->getMock(); |
| 55 | + $this->mediaGallery = new MediaGallery(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @dataProvider dataProviderForResolve |
| 60 | + * @param $expected |
| 61 | + * @param $productName |
| 62 | + * @return void |
| 63 | + * @throws Exception |
| 64 | + */ |
| 65 | + public function testResolve($expected, $productName): void |
| 66 | + { |
| 67 | + $existingEntryMock = $this->getMockBuilder(Entry::class) |
| 68 | + ->disableOriginalConstructor() |
| 69 | + ->setMethods(['getName', 'getData', 'getExtensionAttributes']) |
| 70 | + ->getMock(); |
| 71 | + $existingEntryMock->expects($this->any())->method('getData')->willReturn($expected); |
| 72 | + $existingEntryMock->expects($this->any())->method( |
| 73 | + 'getExtensionAttributes' |
| 74 | + )->willReturn(false); |
| 75 | + $this->productMock->expects($this->any())->method('getName')->willReturn($productName); |
| 76 | + $this->productMock->expects($this->any())->method('getMediaGalleryEntries') |
| 77 | + ->willReturn([$existingEntryMock]); |
| 78 | + $result = $this->mediaGallery->resolve( |
| 79 | + $this->fieldMock, |
| 80 | + $this->contextMock, |
| 81 | + $this->infoMock, |
| 82 | + [ |
| 83 | + 'model' => $this->productMock |
| 84 | + ], |
| 85 | + [] |
| 86 | + ); |
| 87 | + $this->assertNotEmpty($result); |
| 88 | + $this->assertEquals($productName, $result[0]['label']); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return array |
| 93 | + */ |
| 94 | + public function dataProviderForResolve(): array |
| 95 | + { |
| 96 | + return [ |
| 97 | + [ |
| 98 | + [ |
| 99 | + "file" => "/w/b/wb01-black-0.jpg", |
| 100 | + "media_type" => "image", |
| 101 | + "label" => null, |
| 102 | + "position" => "1", |
| 103 | + "disabled" => "0", |
| 104 | + "types" => [ |
| 105 | + "image", |
| 106 | + "small_image" |
| 107 | + ], |
| 108 | + "id" => "11" |
| 109 | + ], |
| 110 | + "TestImage" |
| 111 | + ], |
| 112 | + [ |
| 113 | + [ |
| 114 | + "file" => "/w/b/wb01-black-0.jpg", |
| 115 | + "media_type" => "image", |
| 116 | + "label" => "HelloWorld", |
| 117 | + "position" => "1", |
| 118 | + "disabled" => "0", |
| 119 | + "types" => [ |
| 120 | + "image", |
| 121 | + "small_image" |
| 122 | + ], |
| 123 | + "id" => "11" |
| 124 | + ], |
| 125 | + "HelloWorld" |
| 126 | + ] |
| 127 | + ]; |
| 128 | + } |
| 129 | +} |
0 commit comments