![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/module-url-rewrite/Test/Unit/Model/Storage/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ declare(strict_types=1); namespace Magento\UrlRewrite\Test\Unit\Model\Storage; use Magento\Framework\Api\DataObjectHelper; use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException; use Magento\UrlRewrite\Model\Storage\AbstractStorage; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; class AbstractStorageTest extends TestCase { /** * @var UrlRewriteFactory|MockObject */ protected $urlRewriteFactory; /** * @var DataObjectHelper|MockObject */ protected $dataObjectHelper; /** * @var AbstractStorage|MockObject */ protected $storage; /** * @inheritdoc */ protected function setUp(): void { $this->urlRewriteFactory = $this->getMockBuilder(UrlRewriteFactory::class) ->onlyMethods(['create']) ->disableOriginalConstructor() ->getMock(); $this->dataObjectHelper = $this->getMockBuilder(DataObjectHelper::class) ->disableOriginalConstructor() ->getMock(); $this->storage = $this->getMockForAbstractClass( AbstractStorage::class, [$this->urlRewriteFactory, $this->dataObjectHelper], '', true, true, true ); } /** * @return void */ public function testFindAllByData(): void { $data = [['field1' => 'value1']]; $rows = [['row1'], ['row2']]; $urlRewrites = [['urlRewrite1'], ['urlRewrite2']]; $this->storage->expects($this->once()) ->method('doFindAllByData') ->with($data) ->willReturn($rows); $this->dataObjectHelper ->method('populateWithArray') ->withConsecutive( [$urlRewrites[0], $rows[0], UrlRewrite::class], [$urlRewrites[1], $rows[1], UrlRewrite::class] ) ->willReturnOnConsecutiveCalls($this->dataObjectHelper, $this->dataObjectHelper); $this->urlRewriteFactory ->method('create') ->willReturnOnConsecutiveCalls($urlRewrites[0], $urlRewrites[1]); $this->assertEquals($urlRewrites, $this->storage->findAllByData($data)); } /** * @return void */ public function testFindOneByDataIfNotFound(): void { $data = [['field1' => 'value1']]; $this->storage->expects($this->once()) ->method('doFindOneByData') ->with($data) ->willReturn(null); $this->assertNull($this->storage->findOneByData($data)); } /** * @return void */ public function testFindOneByDataIfFound(): void { $data = [['field1' => 'value1']]; $row = ['row1']; $urlRewrite = ['urlRewrite1']; $this->storage->expects($this->once()) ->method('doFindOneByData') ->with($data) ->willReturn($row); $this->dataObjectHelper->expects($this->once()) ->method('populateWithArray') ->with($urlRewrite, $row, UrlRewrite::class)->willReturnSelf(); $this->urlRewriteFactory->expects($this->any()) ->method('create') ->willReturn($urlRewrite); $this->assertEquals($urlRewrite, $this->storage->findOneByData($data)); } /** * @return void */ public function testReplaceIfUrlsAreEmpty(): void { $this->storage->expects($this->never())->method('doReplace'); $this->storage->replace([]); } /** * @return void */ public function testReplaceIfThrewDuplicateEntryExceptionWithCustomMessage(): void { $this->expectException('Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException'); $this->expectExceptionMessage('Custom storage message'); $this->storage ->expects($this->once()) ->method('doReplace') ->willThrowException(new UrlAlreadyExistsException(__('Custom storage message'))); $this->storage->replace([['UrlRewrite1']]); } /** * @return void */ public function testReplaceIfThrewDuplicateEntryExceptionDefaultMessage(): void { $this->expectException('Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException'); $this->expectExceptionMessage('URL key for specified store already exists'); $this->storage ->expects($this->once()) ->method('doReplace') ->willThrowException(new UrlAlreadyExistsException()); $this->storage->replace([['UrlRewrite1']]); } /** * @return void */ public function testReplace(): void { $urls = [['UrlRewrite1'], ['UrlRewrite2']]; $this->storage ->expects($this->once()) ->method('doReplace') ->with($urls); $this->storage->replace($urls); } }