2022-11-11 13:26:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2024-06-16 13:53:57 +02:00
|
|
|
* Copyright (c) D3 Data Development (Inh. Thomas Dartsch)
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view
|
|
|
|
* the LICENSE file that was distributed with this source code.
|
2022-11-11 13:26:07 +01:00
|
|
|
*
|
|
|
|
* 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\Tests\Unit\Production;
|
|
|
|
|
|
|
|
use D3\TestingTools\Development\CanAccessRestricted;
|
|
|
|
use D3\TestingTools\Production\IsMockable;
|
|
|
|
use D3\TestingTools\Tests\Unit\Production\HelperClasses\IsMockableClass;
|
2022-12-20 23:15:32 +01:00
|
|
|
use D3\TestingTools\Tests\Unit\Production\HelperClasses\IsMockableParent;
|
2024-06-16 14:10:22 +02:00
|
|
|
use Exception;
|
2024-02-01 08:30:06 +01:00
|
|
|
use Generator;
|
2022-12-20 23:15:32 +01:00
|
|
|
use OxidEsales\Eshop\Application\Model\Article;
|
2024-06-16 22:49:46 +02:00
|
|
|
use OxidEsales\Eshop\Core\Config;
|
2022-11-11 13:26:07 +01:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2024-06-16 22:49:46 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2022-11-11 13:26:07 +01:00
|
|
|
use ReflectionException;
|
|
|
|
|
|
|
|
class IsMockableTest extends TestCase
|
|
|
|
{
|
|
|
|
use CanAccessRestricted;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @throws ReflectionException
|
2022-12-20 23:15:32 +01:00
|
|
|
* @covers \D3\TestingTools\Production\IsMockable::d3CallMockableFunction
|
2022-11-11 13:26:07 +01:00
|
|
|
*/
|
2022-12-20 23:15:32 +01:00
|
|
|
public function callMockableFunctionMissingFunction(): void
|
2022-11-11 13:26:07 +01:00
|
|
|
{
|
2024-06-16 14:10:22 +02:00
|
|
|
set_error_handler(static function (int $errno, string $errstr): void {
|
|
|
|
throw new Exception($errstr, $errno);
|
|
|
|
}, E_USER_WARNING);
|
|
|
|
|
2022-11-11 13:26:07 +01:00
|
|
|
$methodName = $this->getRandomString();
|
|
|
|
$argument = $this->getRandomString();
|
|
|
|
|
|
|
|
$traitMock = $this->getObjectForTrait(IsMockable::class);
|
|
|
|
|
2022-12-20 23:15:32 +01:00
|
|
|
// argument #1 is not a valid callable
|
2024-06-16 14:10:22 +02:00
|
|
|
$this->expectExceptionMessage('must be of type callable, array given');
|
2022-11-11 13:26:07 +01:00
|
|
|
|
|
|
|
$this->callMethod(
|
|
|
|
$traitMock,
|
2022-12-20 23:15:32 +01:00
|
|
|
'd3CallMockableFunction',
|
|
|
|
[[$traitMock, $methodName], [$argument]]
|
2022-11-11 13:26:07 +01:00
|
|
|
);
|
2024-06-16 14:10:22 +02:00
|
|
|
|
|
|
|
restore_error_handler();
|
2022-11-11 13:26:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-01 08:30:06 +01:00
|
|
|
* @param string $fqClassName
|
|
|
|
* @param string $expected
|
|
|
|
*
|
2022-11-11 13:26:07 +01:00
|
|
|
* @throws ReflectionException
|
2024-02-01 08:30:06 +01:00
|
|
|
* @test
|
2022-12-20 23:15:32 +01:00
|
|
|
* @dataProvider callMockableFunctionFromClassDataProvider
|
2024-02-01 08:30:06 +01:00
|
|
|
* @covers \D3\TestingTools\Production\IsMockable::d3CallMockableFunction
|
2022-11-11 13:26:07 +01:00
|
|
|
*/
|
2024-02-01 08:30:06 +01:00
|
|
|
public function callMockableFunctionFromClass(string $fqClassName, string $expected): void
|
2022-11-11 13:26:07 +01:00
|
|
|
{
|
|
|
|
$methodName = 'myMethod';
|
|
|
|
$argument = $this->getRandomString();
|
|
|
|
|
|
|
|
/** @var MockObject $mock */
|
2022-12-20 23:15:32 +01:00
|
|
|
$mock = new(IsMockableClass::class);
|
|
|
|
|
|
|
|
if ($fqClassName === 'mockObject') {
|
|
|
|
$fqClassName = $mock;
|
|
|
|
}
|
2022-11-11 13:26:07 +01:00
|
|
|
|
|
|
|
$this->assertSame(
|
2022-12-20 23:15:32 +01:00
|
|
|
$expected.$argument,
|
|
|
|
$this->callMethod(
|
|
|
|
$mock,
|
|
|
|
'd3CallMockableFunction',
|
|
|
|
[[$fqClassName, $methodName], [$argument]]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-01 08:30:06 +01:00
|
|
|
public function callMockableFunctionFromClassDataProvider(): Generator
|
2022-12-20 23:15:32 +01:00
|
|
|
{
|
2024-02-01 08:30:06 +01:00
|
|
|
yield 'parent static method' => [IsMockableParent::class, 'ParentClass::myMethod##'];
|
|
|
|
yield 'current static method' => [IsMockableClass::class, 'currentClass::myMethod##'];
|
|
|
|
yield 'current object method' => ['mockObject', 'currentClass::myMethod##'];
|
2022-12-20 23:15:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\TestingTools\Production\IsMockable::d3GetMockableOxNewObject
|
|
|
|
*/
|
|
|
|
public function canGetMockableOxNewObject()
|
|
|
|
{
|
|
|
|
/** @var MockObject $mock */
|
|
|
|
$mock = new(IsMockableClass::class);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
Article::class,
|
2022-11-11 13:26:07 +01:00
|
|
|
$this->callMethod(
|
|
|
|
$mock,
|
2022-12-20 23:15:32 +01:00
|
|
|
'd3GetMockableOxNewObject',
|
|
|
|
[Article::class]
|
2022-11-11 13:26:07 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-16 22:49:46 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\TestingTools\Production\IsMockable::d3GetMockableRegistryObject
|
|
|
|
*/
|
|
|
|
public function canGetMockableRegistryObject(): void
|
|
|
|
{
|
|
|
|
/** @var MockObject $mock */
|
|
|
|
$mock = new(IsMockableClass::class);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
Config::class,
|
|
|
|
$this->callMethod(
|
|
|
|
$mock,
|
|
|
|
'd3GetMockableRegistryObject',
|
|
|
|
[Config::class]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @return void
|
|
|
|
* @throws ReflectionException
|
|
|
|
* @covers \D3\TestingTools\Production\IsMockable::d3GetMockableLogger
|
|
|
|
*/
|
|
|
|
public function canGetMockableLogger()
|
|
|
|
{
|
|
|
|
/** @var MockObject $mock */
|
|
|
|
$mock = new(IsMockableClass::class);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
LoggerInterface::class,
|
|
|
|
$this->callMethod(
|
|
|
|
$mock,
|
|
|
|
'd3GetMockableLogger',
|
|
|
|
[Config::class]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-11 13:26:07 +01:00
|
|
|
/**
|
|
|
|
* @param int $length
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getRandomString(int $length = 20): string
|
|
|
|
{
|
|
|
|
return substr(
|
|
|
|
str_shuffle(
|
|
|
|
str_repeat(
|
|
|
|
$x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
|
|
(int) ceil($length/strlen($x))
|
|
|
|
)
|
|
|
|
),
|
|
|
|
1,
|
|
|
|
$length
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|