91 lines
3.5 KiB
PHP
91 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MageSail\Magesail\Test\Unit\Plugin\App\Request;
|
|
|
|
use MageSail\Magesail\Model\AdminPathDetector;
|
|
use MageSail\Magesail\Model\TunnelRequestDetector;
|
|
use MageSail\Magesail\Plugin\App\Request\StoreFromMageSailHeaderPlugin;
|
|
use Magento\Framework\App\Request\Http;
|
|
use Magento\Framework\Exception\NoSuchEntityException;
|
|
use Magento\Store\Api\Data\StoreInterface;
|
|
use Magento\Store\Api\StoreRepositoryInterface;
|
|
use Magento\Store\Model\ScopeInterface;
|
|
use Magento\Store\Model\StoreManager;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class StoreFromMageSailHeaderPluginTest extends TestCase
|
|
{
|
|
public function testAdminPathForcesWebsiteScopeAndEmptyRunCode(): void
|
|
{
|
|
$admin = $this->createMock(AdminPathDetector::class);
|
|
$admin->method('isAdminPath')->willReturn(true);
|
|
$tunnel = $this->createMock(TunnelRequestDetector::class);
|
|
$tunnel->expects(self::never())->method('getTunnelStoreCode');
|
|
$stores = $this->createMock(StoreRepositoryInterface::class);
|
|
|
|
$plugin = new StoreFromMageSailHeaderPlugin($admin, $tunnel, $stores);
|
|
$request = new Http();
|
|
$proceed = static fn ($n, $d = null) => 'proceeded';
|
|
|
|
self::assertSame(
|
|
ScopeInterface::SCOPE_WEBSITE,
|
|
$plugin->aroundGetServerValue($request, $proceed, StoreManager::PARAM_RUN_TYPE)
|
|
);
|
|
self::assertSame(
|
|
'',
|
|
$plugin->aroundGetServerValue($request, $proceed, StoreManager::PARAM_RUN_CODE)
|
|
);
|
|
self::assertSame(
|
|
'proceeded',
|
|
$plugin->aroundGetServerValue($request, $proceed, 'other')
|
|
);
|
|
}
|
|
|
|
public function testTunnelStoreCodeReturnedForMageRunCodeWhenValid(): void
|
|
{
|
|
$admin = $this->createMock(AdminPathDetector::class);
|
|
$admin->method('isAdminPath')->willReturn(false);
|
|
$tunnel = $this->createMock(TunnelRequestDetector::class);
|
|
$tunnel->method('getTunnelStoreCode')->willReturn('mgtun_ok');
|
|
$store = $this->createMock(StoreInterface::class);
|
|
$stores = $this->createMock(StoreRepositoryInterface::class);
|
|
$stores->method('get')->with('mgtun_ok')->willReturn($store);
|
|
|
|
$plugin = new StoreFromMageSailHeaderPlugin($admin, $tunnel, $stores);
|
|
$request = new Http();
|
|
$request->setServerParams([]);
|
|
$proceed = static fn () => null;
|
|
|
|
self::assertSame(
|
|
'mgtun_ok',
|
|
$plugin->aroundGetServerValue($request, $proceed, StoreManager::PARAM_RUN_CODE)
|
|
);
|
|
self::assertSame(
|
|
'store',
|
|
$plugin->aroundGetServerValue($request, $proceed, StoreManager::PARAM_RUN_TYPE)
|
|
);
|
|
}
|
|
|
|
public function testUnknownCandidateStripsRunCodeToEmptyString(): void
|
|
{
|
|
$admin = $this->createMock(AdminPathDetector::class);
|
|
$admin->method('isAdminPath')->willReturn(false);
|
|
$tunnel = $this->createMock(TunnelRequestDetector::class);
|
|
$tunnel->method('getTunnelStoreCode')->willReturn(null);
|
|
$stores = $this->createMock(StoreRepositoryInterface::class);
|
|
$stores->method('get')->willThrowException(new NoSuchEntityException());
|
|
|
|
$plugin = new StoreFromMageSailHeaderPlugin($admin, $tunnel, $stores);
|
|
$request = new Http();
|
|
$request->setServerParams([StoreManager::PARAM_RUN_CODE => 'ghost']);
|
|
$proceed = static fn () => 'skip';
|
|
|
|
self::assertSame(
|
|
'',
|
|
$plugin->aroundGetServerValue($request, $proceed, StoreManager::PARAM_RUN_CODE)
|
|
);
|
|
}
|
|
}
|