TestingTools/Tests/Unit/Production/IsMockableTest.php

136 lines
3.7 KiB
PHP
Raw Normal View History

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\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;
use OxidEsales\Eshop\Application\Model\Article;
2022-11-11 13:26:07 +01:00
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use RuntimeException;
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
{
$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
$this->expectError();
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
);
}
/**
2022-12-20 23:15:32 +01:00
* @param $fqClassName
* @param $expected
2022-11-11 13:26:07 +01:00
* @test
* @throws ReflectionException
2022-12-20 23:15:32 +01:00
* @dataProvider callMockableFunctionFromClassDataProvider
* @covers \D3\TestingTools\Production\IsMockable::d3CallMockableFunction
2022-11-11 13:26:07 +01:00
*/
2022-12-20 23:15:32 +01:00
public function callMockableFunctionFromClass($fqClassName, $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]]
)
);
}
/**
* @return array[]
*/
public function callMockableFunctionFromClassDataProvider()
{
return [
'parent static method' => [IsMockableParent::class, 'ParentClass::myMethod##'],
'current static method' => [IsMockableClass::class, 'currentClass::myMethod##'],
'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,
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
)
);
}
/**
* @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
);
}
}