2022-11-11 13:26:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*
|
|
|
|
* https://www.d3data.de
|
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
|
|
|
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
|
|
|
|
* @link https://www.oxidmodule.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace D3\TestingTools\Production;
|
|
|
|
|
2022-12-04 00:19:17 +01:00
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
|
|
|
use Psr\Log\LoggerInterface;
|
2022-11-11 13:26:07 +01:00
|
|
|
|
|
|
|
trait IsMockable
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* mockable wrapper for uncertain parent calls
|
|
|
|
*
|
|
|
|
* @param string $methodName
|
|
|
|
* @param array $arguments
|
|
|
|
*
|
|
|
|
* @return false|mixed
|
|
|
|
*/
|
2022-12-04 00:19:17 +01:00
|
|
|
protected function d3CallMockableFunction(callable $callable, array $arguments = [])
|
2022-11-11 13:26:07 +01:00
|
|
|
{
|
2022-12-04 00:19:17 +01:00
|
|
|
return call_user_func_array($callable, $arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* for mocking use callback:
|
|
|
|
*
|
|
|
|
* $object->method('d3GetMockableOxNewObject')->willReturnCallback(
|
|
|
|
* function () use ($manufacturerMock) {
|
|
|
|
* $args = func_get_args();
|
|
|
|
* switch ($args[0]) {
|
|
|
|
* case Article::class:
|
|
|
|
* return $manufacturerMock;
|
|
|
|
* default:
|
|
|
|
* return call_user_func_array("oxNew", $args);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* );
|
|
|
|
*
|
|
|
|
* @template T
|
|
|
|
* @param class-string<T> $className
|
|
|
|
* @param mixed ...$args constructor arguments
|
|
|
|
*
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
protected function d3GetMockableOxNewObject(string $className)
|
|
|
|
{
|
|
|
|
$arguments = func_get_args();
|
|
|
|
return call_user_func_array("oxNew", $arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* for mocking use callback:
|
|
|
|
*
|
|
|
|
* $object->method('d3GetMockableRegistryObject')->willReturnCallback(
|
|
|
|
* function () use ($utilsServerMock) {
|
|
|
|
* $args = func_get_args();
|
|
|
|
* switch ($args[0]) {
|
|
|
|
* case UtilsServer::class:
|
|
|
|
* return $utilsServerMock;
|
|
|
|
* default:
|
|
|
|
* return Registry::get($args[0]);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @template T
|
|
|
|
* @param class-string<T> $className
|
|
|
|
*
|
|
|
|
* @return T
|
|
|
|
*/
|
|
|
|
protected function d3GetMockableRegistryObject(string $className)
|
|
|
|
{
|
|
|
|
return Registry::get($className);
|
|
|
|
}
|
2022-11-11 13:26:07 +01:00
|
|
|
|
2022-12-04 00:19:17 +01:00
|
|
|
/**
|
|
|
|
* @return LoggerInterface
|
|
|
|
*/
|
|
|
|
public function d3GetMockableLogger(): LoggerInterface
|
|
|
|
{
|
|
|
|
return Registry::getLogger();
|
2022-11-11 13:26:07 +01:00
|
|
|
}
|
|
|
|
}
|