storeMock = $this->createMock(Store::class); $storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) ->disableOriginalConstructor() ->getMockForAbstractClass(); $storeManagerMock ->method('getStore') ->willReturn($this->storeMock); $objectManager = new ObjectManager($this); $this->getRelativePathFromUrl = $objectManager->getObject( GetRelativePathFromUrl::class, [ 'storeManager' => $storeManagerMock, ] ); } public function testEmptyUrl(): void { $this->storeMock ->method('getBaseUrl') ->willReturn('https://example.shop.com'); $this->assertSame('/', $this->getRelativePathFromUrl->execute('')); } public function testBaseUrl(): void { $this->storeMock ->method('getBaseUrl') ->willReturn('https://example.shop.com'); $this->assertSame('/', $this->getRelativePathFromUrl->execute('https://example.shop.com')); } /** * @dataProvider urlsProvider * * @param string $result * @param string $baseUrl * @param string $url * @param bool $removeGetParams * @param bool $removeFragment */ public function testNotHaveConsent( string $result, string $baseUrl, string $url, bool $removeGetParams, bool $removeFragment ): void { $this->storeMock ->method('getBaseUrl') ->willReturn($baseUrl); $this->assertSame($result, $this->getRelativePathFromUrl->execute($url, $removeGetParams, $removeFragment)); } /** * @return \Generator */ public function urlsProvider(): \Generator { yield [ 'result' => '/test/', 'baseUrl' => 'https://example.shop.com', 'url' => 'https://example.shop.com/test/', 'removeGetParams' => true, 'removeFragment' => true, ]; yield [ 'result' => '/test/', 'baseUrl' => 'https://example.shop.com', 'url' => '/test/', 'removeGetParams' => true, 'removeFragment' => true, ]; yield [ 'result' => '/test', 'baseUrl' => 'https://example.shop.com', 'url' => '/test', 'removeGetParams' => true, 'removeFragment' => true, ]; yield [ 'result' => '/test', 'baseUrl' => 'https://example.shop.com', 'url' => '/test?a=1', 'removeGetParams' => true, 'removeFragment' => true, ]; yield [ 'result' => '/test', 'baseUrl' => 'https://example.shop.com', 'url' => '/test?a', 'removeGetParams' => true, 'removeFragment' => true, ]; yield [ 'result' => '/test', 'baseUrl' => 'https://example.shop.com', 'url' => '/test#anchor', 'removeGetParams' => true, 'removeFragment' => true, ]; yield [ 'result' => '/test/', 'baseUrl' => 'https://example.shop.com', 'url' => '/test/?a=1#anchor', 'removeGetParams' => true, 'removeFragment' => true, ]; yield [ 'result' => '/test/?a=1', 'baseUrl' => 'https://example.shop.com', 'url' => '/test/?a=1#anchor', 'removeGetParams' => false, 'removeFragment' => true, ]; yield [ 'result' => '/test/#anchor', 'baseUrl' => 'https://example.shop.com', 'url' => '/test/?a=1#anchor', 'removeGetParams' => true, 'removeFragment' => false, ]; yield [ 'result' => '/test/?a=1#anchor', 'baseUrl' => 'https://example.shop.com', 'url' => '/test/?a=1#anchor', 'removeGetParams' => false, 'removeFragment' => false, ]; } }