TestingTools/Tests/Unit/Production/IsMockableTest.php
Daniel Seifert be4daabc48
drop PHP 7 support
# Conflicts:
#	Tests/Unit/Production/IsMockableTest.php
2024-02-01 08:40:38 +01:00

132 lines
3.6 KiB
PHP

<?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\Tests\Unit\Production;
use D3\TestingTools\Development\CanAccessRestricted;
use D3\TestingTools\Production\IsMockable;
use D3\TestingTools\Tests\Unit\Production\HelperClasses\IsMockableClass;
use D3\TestingTools\Tests\Unit\Production\HelperClasses\IsMockableParent;
use Generator;
use OxidEsales\Eshop\Application\Model\Article;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ReflectionException;
class IsMockableTest extends TestCase
{
use CanAccessRestricted;
/**
* @test
* @throws ReflectionException
* @covers \D3\TestingTools\Production\IsMockable::d3CallMockableFunction
*/
public function callMockableFunctionMissingFunction(): void
{
$methodName = $this->getRandomString();
$argument = $this->getRandomString();
$traitMock = $this->getObjectForTrait(IsMockable::class);
// argument #1 is not a valid callable
$this->expectError();
$this->callMethod(
$traitMock,
'd3CallMockableFunction',
[[$traitMock, $methodName], [$argument]]
);
}
/**
* @param string $fqClassName
* @param string $expected
*
* @throws ReflectionException
* @test
* @dataProvider callMockableFunctionFromClassDataProvider
* @covers \D3\TestingTools\Production\IsMockable::d3CallMockableFunction
*/
public function callMockableFunctionFromClass(string $fqClassName, string $expected): void
{
$methodName = 'myMethod';
$argument = $this->getRandomString();
/** @var MockObject $mock */
$mock = new(IsMockableClass::class);
if ($fqClassName === 'mockObject') {
$fqClassName = $mock;
}
$this->assertSame(
$expected.$argument,
$this->callMethod(
$mock,
'd3CallMockableFunction',
[[$fqClassName, $methodName], [$argument]]
)
);
}
public function callMockableFunctionFromClassDataProvider(): Generator
{
yield 'parent static method' => [IsMockableParent::class, 'ParentClass::myMethod##'];
yield 'current static method' => [IsMockableClass::class, 'currentClass::myMethod##'];
yield 'current object method' => ['mockObject', 'currentClass::myMethod##'];
}
/**
* @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,
$this->callMethod(
$mock,
'd3GetMockableOxNewObject',
[Article::class]
)
);
}
/**
* @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
);
}
}