|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\PageBuilder\Test\Unit\Model; |
| 9 | + |
| 10 | +use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface; |
| 11 | +use Magento\Framework\Exception\CouldNotDeleteException; |
| 12 | +use Magento\Framework\Exception\LocalizedException; |
| 13 | +use Magento\Framework\Filesystem; |
| 14 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
| 15 | +use Magento\Framework\Image\Adapter\AdapterInterface; |
| 16 | +use Magento\Framework\Image\Factory; |
| 17 | +use Magento\PageBuilder\Api\Data\TemplateSearchResultsInterfaceFactory; |
| 18 | +use Magento\PageBuilder\Model\ResourceModel\Template as ResourceTemplate; |
| 19 | +use Magento\PageBuilder\Model\ResourceModel\Template\CollectionFactory; |
| 20 | +use Magento\PageBuilder\Model\Template; |
| 21 | +use Magento\PageBuilder\Model\TemplateFactory; |
| 22 | +use Magento\PageBuilder\Model\TemplateRepository; |
| 23 | +use Magento\MediaStorage\Helper\File\Storage\Database; |
| 24 | +use PHPUnit\Framework\MockObject\Exception; |
| 25 | +use PHPUnit\Framework\MockObject\MockObject; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | + |
| 28 | +/** |
| 29 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 30 | + */ |
| 31 | +class TemplateRepositoryTest extends TestCase |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var ResourceTemplate|MockObject |
| 35 | + */ |
| 36 | + private $resourceMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var TemplateFactory|MockObject |
| 40 | + */ |
| 41 | + private $templateFactoryMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var CollectionFactory|MockObject |
| 45 | + */ |
| 46 | + private $templateCollectionFactoryMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var TemplateSearchResultsInterfaceFactory|MockObject |
| 50 | + */ |
| 51 | + private $searchResultsFactoryMock; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var CollectionProcessorInterface|MockObject |
| 55 | + */ |
| 56 | + private $collectionProcessorMock; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var Filesystem|MockObject |
| 60 | + */ |
| 61 | + private $filesystemMock; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var Database|MockObject |
| 65 | + */ |
| 66 | + private $mediaStorageMock; |
| 67 | + |
| 68 | + /** |
| 69 | + * @var Factory|MockObject |
| 70 | + */ |
| 71 | + private $imageFactoryMock; |
| 72 | + |
| 73 | + /** |
| 74 | + * @var TemplateRepository |
| 75 | + */ |
| 76 | + private $templateRepository; |
| 77 | + |
| 78 | + protected function setUp(): void |
| 79 | + { |
| 80 | + $this->resourceMock = $this->createMock(ResourceTemplate::class); |
| 81 | + $this->templateFactoryMock = $this->createMock(TemplateFactory::class); |
| 82 | + $this->templateCollectionFactoryMock = $this->createMock(CollectionFactory::class); |
| 83 | + $this->searchResultsFactoryMock = $this->createMock(TemplateSearchResultsInterfaceFactory::class); |
| 84 | + $this->collectionProcessorMock = $this->createMock(CollectionProcessorInterface::class); |
| 85 | + $this->filesystemMock = $this->createMock(Filesystem::class); |
| 86 | + $this->mediaStorageMock = $this->createMock(Database::class); |
| 87 | + $this->imageFactoryMock = $this->createMock(Factory::class); |
| 88 | + |
| 89 | + $this->templateRepository = new TemplateRepository( |
| 90 | + $this->resourceMock, |
| 91 | + $this->templateFactoryMock, |
| 92 | + $this->templateCollectionFactoryMock, |
| 93 | + $this->searchResultsFactoryMock, |
| 94 | + $this->collectionProcessorMock, |
| 95 | + $this->filesystemMock, |
| 96 | + $this->mediaStorageMock, |
| 97 | + $this->imageFactoryMock |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Test for delete method |
| 103 | + * |
| 104 | + * @return void |
| 105 | + * @throws LocalizedException |
| 106 | + * @throws Exception |
| 107 | + */ |
| 108 | + public function testDelete() |
| 109 | + { |
| 110 | + $templateId = 1; |
| 111 | + $templateMock =$this->getMockBuilder(Template::class) |
| 112 | + ->disableOriginalConstructor() |
| 113 | + ->addMethods(['getTemplateId']) |
| 114 | + ->onlyMethods(['getPreviewImage', 'getPreviewThumbnailImage']) |
| 115 | + ->getMock(); |
| 116 | + ; |
| 117 | + $templateMock->expects($this->once()) |
| 118 | + ->method('getTemplateId') |
| 119 | + ->willReturn($templateId); |
| 120 | + $templateMock->expects($this->once()) |
| 121 | + ->method('getPreviewImage') |
| 122 | + ->willReturn('preview_image.jpg'); |
| 123 | + $templateMock->expects($this->once()) |
| 124 | + ->method('getPreviewThumbnailImage') |
| 125 | + ->willReturn('preview_thumb_image.jpg'); |
| 126 | + |
| 127 | + $this->templateFactoryMock->method('create')->willReturn($templateMock); |
| 128 | + $this->resourceMock->expects($this->once())->method('load')->with($templateMock, $templateId); |
| 129 | + $this->resourceMock->expects($this->once())->method('delete')->with($templateMock); |
| 130 | + |
| 131 | + $mediaDirMock = $this->createMock(WriteInterface::class); |
| 132 | + $this->filesystemMock->method('getDirectoryWrite')->willReturn($mediaDirMock); |
| 133 | + $mediaDirMock->method('isExist')->willReturn(true); |
| 134 | + $mediaDirMock->method('delete')->willReturn(true); |
| 135 | + $this->imageFactoryMock->expects($this->exactly(2)) |
| 136 | + ->method('create') |
| 137 | + ->willReturnCallback( |
| 138 | + function ($path) { |
| 139 | + $this->assertEquals('preview_image.jpg', $path); |
| 140 | + return $this->createMock(AdapterInterface::class); |
| 141 | + } |
| 142 | + ); |
| 143 | + $this->mediaStorageMock->expects($this->exactly(2))->method('deleteFile'); |
| 144 | + |
| 145 | + $this->assertTrue($this->templateRepository->delete($templateMock)); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Test for delete method when throws exception |
| 150 | + * |
| 151 | + * @return void |
| 152 | + * @throws LocalizedException |
| 153 | + */ |
| 154 | + public function testDeleteThrowsException() |
| 155 | + { |
| 156 | + $this->expectException(CouldNotDeleteException::class); |
| 157 | + |
| 158 | + $templateMock =$this->getMockBuilder(Template::class) |
| 159 | + ->disableOriginalConstructor() |
| 160 | + ->addMethods(['getTemplateId']) |
| 161 | + ->getMock(); |
| 162 | + $templateMock->expects($this->once()) |
| 163 | + ->method('getTemplateId') |
| 164 | + ->willReturn(1); |
| 165 | + |
| 166 | + $this->templateFactoryMock->method('create')->willReturn($templateMock); |
| 167 | + $this->resourceMock->method('load')->willThrowException(new \Exception('Error')); |
| 168 | + |
| 169 | + $this->templateRepository->delete($templateMock); |
| 170 | + } |
| 171 | +} |
0 commit comments