80 lines
3.2 KiB
PHP
80 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MageSail\Magesail\Test\Unit\Model;
|
|
|
|
use MageSail\Magesail\Model\TunnelHostnameMatcher;
|
|
use MageSail\Magesail\Model\TunnelRequestDetector;
|
|
use Magento\Framework\App\Request\Http;
|
|
use Magento\Framework\Exception\NoSuchEntityException;
|
|
use Magento\Store\Api\Data\StoreInterface;
|
|
use Magento\Store\Api\StoreRepositoryInterface;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class TunnelRequestDetectorTest extends TestCase
|
|
{
|
|
public function testQueryParamTakesPrecedenceAndValidatesStore(): void
|
|
{
|
|
$hostnameMatcher = $this->createMock(TunnelHostnameMatcher::class);
|
|
$hostnameMatcher->expects(self::never())->method('getStoreCodeIfRequestHostMatchesState');
|
|
|
|
$store = $this->createMock(StoreInterface::class);
|
|
$storeRepo = $this->createMock(StoreRepositoryInterface::class);
|
|
$storeRepo->method('get')->with('valid_code')->willReturn($store);
|
|
|
|
$detector = new TunnelRequestDetector($hostnameMatcher, $storeRepo);
|
|
$request = new Http();
|
|
$request->setParam('magesail_store', 'valid_code');
|
|
|
|
self::assertSame('valid_code', $detector->getTunnelStoreCode($request));
|
|
}
|
|
|
|
public function testInvalidQueryParamReturnsNullWithoutTryingHostname(): void
|
|
{
|
|
$hostnameMatcher = $this->createMock(TunnelHostnameMatcher::class);
|
|
$hostnameMatcher->expects(self::never())->method('getStoreCodeIfRequestHostMatchesState');
|
|
|
|
$storeRepo = $this->createMock(StoreRepositoryInterface::class);
|
|
$storeRepo->method('get')->with('bad_code')->willThrowException(new NoSuchEntityException());
|
|
|
|
$detector = new TunnelRequestDetector($hostnameMatcher, $storeRepo);
|
|
$request = new Http();
|
|
$request->setParam('magesail_store', 'bad_code');
|
|
|
|
self::assertNull($detector->getTunnelStoreCode($request));
|
|
}
|
|
|
|
public function testHeaderUsedWhenNoQueryParam(): void
|
|
{
|
|
$hostnameMatcher = $this->createMock(TunnelHostnameMatcher::class);
|
|
$hostnameMatcher->expects(self::never())->method('getStoreCodeIfRequestHostMatchesState');
|
|
|
|
$store = $this->createMock(StoreInterface::class);
|
|
$storeRepo = $this->createMock(StoreRepositoryInterface::class);
|
|
$storeRepo->method('get')->with('hdr')->willReturn($store);
|
|
|
|
$detector = new TunnelRequestDetector($hostnameMatcher, $storeRepo);
|
|
$request = new Http();
|
|
$request->setHeader('X-MageSail-Store-Code', 'hdr');
|
|
|
|
self::assertSame('hdr', $detector->getTunnelStoreCode($request));
|
|
}
|
|
|
|
public function testHostnameMatcherUsedWhenNoExplicitTunnelParams(): void
|
|
{
|
|
$hostnameMatcher = $this->createMock(TunnelHostnameMatcher::class);
|
|
$hostnameMatcher->method('getStoreCodeIfRequestHostMatchesState')->willReturn('from_host');
|
|
|
|
$store = $this->createMock(StoreInterface::class);
|
|
$storeRepo = $this->createMock(StoreRepositoryInterface::class);
|
|
$storeRepo->method('get')->with('from_host')->willReturn($store);
|
|
|
|
$detector = new TunnelRequestDetector($hostnameMatcher, $storeRepo);
|
|
$request = new Http();
|
|
$request->setServerParams(['HTTP_HOST' => 'dev.example.com']);
|
|
|
|
self::assertSame('from_host', $detector->getTunnelStoreCode($request));
|
|
}
|
|
}
|