|
| 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\Downloadable\Test\Unit\Model\Sample; |
| 10 | + |
| 11 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 12 | +use Magento\Catalog\Model\Product; |
| 13 | +use Magento\Downloadable\Api\Data\SampleInterface; |
| 14 | +use Magento\Downloadable\Api\SampleRepositoryInterface as SampleRepository; |
| 15 | +use Magento\Downloadable\Model\Product\Type; |
| 16 | +use Magento\Downloadable\Model\Sample\DeleteHandler; |
| 17 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +/** |
| 22 | + * Unit Test for \Magento\Downloadable\Model\Sample\DeleteHandler |
| 23 | + */ |
| 24 | +class DeleteHandlerTest extends TestCase |
| 25 | +{ |
| 26 | + const STUB_PRODUCT_TYPE = 'simple'; |
| 27 | + const STUB_PRODUCT_SKU = 'sku'; |
| 28 | + const STUB_SAMPLE_ID = 1; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var ProductInterface|MockObject |
| 32 | + */ |
| 33 | + private $entityMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var SampleRepository|MockObject |
| 37 | + */ |
| 38 | + private $sampleRepositoryMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var DeleteHandler |
| 42 | + */ |
| 43 | + private $deleteHandler; |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritDoc |
| 47 | + */ |
| 48 | + protected function setUp() |
| 49 | + { |
| 50 | + $this->entityMock = $this->createMock(Product::class); |
| 51 | + $this->sampleRepositoryMock = $this->getMockBuilder(SampleRepository::class) |
| 52 | + ->disableOriginalConstructor() |
| 53 | + ->setMethods(['getList', 'delete']) |
| 54 | + ->getMockForAbstractClass(); |
| 55 | + |
| 56 | + $this->deleteHandler = (new ObjectManagerHelper($this))->getObject( |
| 57 | + DeleteHandler::class, |
| 58 | + ['sampleRepository' => $this->sampleRepositoryMock] |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Test case when provided Product has type Downloadable. |
| 64 | + */ |
| 65 | + public function testExecuteWithDownloadableProduct() |
| 66 | + { |
| 67 | + $this->entityMock->expects($this->once()) |
| 68 | + ->method('getTypeId') |
| 69 | + ->willReturn(Type::TYPE_DOWNLOADABLE); |
| 70 | + $this->entityMock->expects($this->once()) |
| 71 | + ->method('getSku') |
| 72 | + ->willReturn(self::STUB_PRODUCT_SKU); |
| 73 | + |
| 74 | + $sampleMock = $this->createMock(SampleInterface::class); |
| 75 | + $sampleMock->expects($this->once()) |
| 76 | + ->method('getId') |
| 77 | + ->willReturn(self::STUB_SAMPLE_ID); |
| 78 | + |
| 79 | + $this->sampleRepositoryMock->expects($this->once())->method('delete'); |
| 80 | + $this->sampleRepositoryMock->expects($this->once()) |
| 81 | + ->method('getList') |
| 82 | + ->willReturn([$sampleMock]); |
| 83 | + |
| 84 | + $this->assertSame($this->entityMock, $this->deleteHandler->execute($this->entityMock)); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Test case when provided Product is not Downloadable. |
| 89 | + */ |
| 90 | + public function testExecuteWithOtherProduct() |
| 91 | + { |
| 92 | + $this->entityMock->expects($this->once()) |
| 93 | + ->method('getTypeId') |
| 94 | + ->willReturn(self::STUB_PRODUCT_TYPE); |
| 95 | + |
| 96 | + $this->sampleRepositoryMock->expects($this->never())->method('getList'); |
| 97 | + $this->sampleRepositoryMock->expects($this->never())->method('delete'); |
| 98 | + $this->assertSame($this->entityMock, $this->deleteHandler->execute($this->entityMock)); |
| 99 | + } |
| 100 | +} |
0 commit comments