8
0
TestingTools/Tests/Unit/Production/IsMockableTest.php

132 Zeilen
3.6 KiB
PHP

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 Generator;
2022-12-20 23:15:32 +01:00
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;
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
);
}
/**
* @param string $fqClassName
* @param string $expected
*
2022-11-11 13:26:07 +01:00
* @throws ReflectionException
* @test
2022-12-20 23:15:32 +01:00
* @dataProvider callMockableFunctionFromClassDataProvider
* @covers \D3\TestingTools\Production\IsMockable::d3CallMockableFunction
2022-11-11 13:26:07 +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]]
)
);
}
public function callMockableFunctionFromClassDataProvider(): Generator
2022-12-20 23:15:32 +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
)
);
}
/**
* @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
);
}
}