I have created a module in magento 2 for adding prefix to the url of all product and category.
here is the snippet code for the router :
/**
* Match route pages for handling cp / ip
*
* @param \Magento\Framework\App\RequestInterface|\Magento\Framework\App\Request\Http $request
* @return bool
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function match(\Magento\Framework\App\RequestInterface $request)
{
$pathInfo= trim($request->getPathInfo(), '/');
$requestUri = trim($request->getRequestUri(), '/');
$requestUriParts = explode('/', $requestUri);
array_walk($requestUriParts, function (&$value) {
$value = trim($value);
});
$pathInfoParts = explode('/', $pathInfo);
array_walk($pathInfoParts, function (&$value) {
$value = trim($value);
});
if ($requestUriParts[0] == $this->prefixCp || $requestUriParts[0]==$this->prefixIp) {
// remove the cp or ip
$requestUriFinal = '';
unset($requestUriParts[0]);
foreach ($requestUriParts as $r) {
$requestUriFinal .= $r . '/';
}
// remove cp or ip
unset($pathInfoParts[0]);
$pathInfoFinal = '';
foreach ($pathInfoParts as $p) {
$pathInfoFinal .= $p . '/' ;
}
$request->setRequestUri($requestUriFinal);
$request->setPathInfo($pathInfoFinal);
} else {
// No route math here
return;
}
/*
* We have match and now we will forward action
*/
return $this->actionFactory->create(
'Magento\Framework\App\Action\Forward',
['request' => $request]
);
}
How to test the logic in this match
function ?
and also for the plugin :
/**
* this will modify the class Product in vendor/magento/module-catalog/Model/Product.php
*/
class ProductPlugin extends \Magento\Catalog\Model\Product
{
/**
* Returns request path
*
* @return string
*/
public function afterGetRequestPath(\Magento\Catalog\Model\Product $subject, $result)
{
return 'ip/' . $result ;
}
}
I just want to test a simple test case where my custom route handle the request. any idea ?