add tests
This commit is contained in:
parent
6fef271ec7
commit
8f21a232b3
@ -28,6 +28,9 @@ class IsMockableClass extends IsMockableParent
|
|||||||
*/
|
*/
|
||||||
public function myMethod(string $arg): string
|
public function myMethod(string $arg): string
|
||||||
{
|
{
|
||||||
return 'currentClass::myMethod##.'.$arg;
|
return 'currentClass::myMethod##'.$arg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function fakeMethod()
|
||||||
|
{}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,8 @@ namespace D3\TestingTools\Tests\Unit\Production;
|
|||||||
use D3\TestingTools\Development\CanAccessRestricted;
|
use D3\TestingTools\Development\CanAccessRestricted;
|
||||||
use D3\TestingTools\Production\IsMockable;
|
use D3\TestingTools\Production\IsMockable;
|
||||||
use D3\TestingTools\Tests\Unit\Production\HelperClasses\IsMockableClass;
|
use D3\TestingTools\Tests\Unit\Production\HelperClasses\IsMockableClass;
|
||||||
|
use D3\TestingTools\Tests\Unit\Production\HelperClasses\IsMockableParent;
|
||||||
|
use OxidEsales\Eshop\Application\Model\Article;
|
||||||
use PHPUnit\Framework\MockObject\MockObject;
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
@ -30,44 +32,84 @@ class IsMockableTest extends TestCase
|
|||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
|
* @covers \D3\TestingTools\Production\IsMockable::d3CallMockableFunction
|
||||||
*/
|
*/
|
||||||
public function callMockableNoParent(): void
|
public function callMockableFunctionMissingFunction(): void
|
||||||
{
|
{
|
||||||
$methodName = $this->getRandomString();
|
$methodName = $this->getRandomString();
|
||||||
$argument = $this->getRandomString();
|
$argument = $this->getRandomString();
|
||||||
|
|
||||||
$traitMock = $this->getObjectForTrait(IsMockable::class);
|
$traitMock = $this->getObjectForTrait(IsMockable::class);
|
||||||
|
|
||||||
$this->expectException(RuntimeException::class);
|
// argument #1 is not a valid callable
|
||||||
|
$this->expectError();
|
||||||
|
|
||||||
$this->callMethod(
|
$this->callMethod(
|
||||||
$traitMock,
|
$traitMock,
|
||||||
'd3CallMockableParent',
|
'd3CallMockableFunction',
|
||||||
[$methodName, [$argument]]
|
[[$traitMock, $methodName], [$argument]]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param $fqClassName
|
||||||
|
* @param $expected
|
||||||
* @test
|
* @test
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
|
* @dataProvider callMockableFunctionFromClassDataProvider
|
||||||
|
* @covers \D3\TestingTools\Production\IsMockable::d3CallMockableFunction
|
||||||
*/
|
*/
|
||||||
public function callMockableParent(): void
|
public function callMockableFunctionFromClass($fqClassName, $expected): void
|
||||||
{
|
{
|
||||||
$methodName = 'myMethod';
|
$methodName = 'myMethod';
|
||||||
$argument = $this->getRandomString();
|
$argument = $this->getRandomString();
|
||||||
|
|
||||||
/** @var MockObject $mock */
|
/** @var MockObject $mock */
|
||||||
$mock = $this->getMockBuilder(IsMockableClass::class)
|
$mock = new(IsMockableClass::class);
|
||||||
->getMock();
|
|
||||||
// method from mocked class will never call, run method from parent class only
|
if ($fqClassName === 'mockObject') {
|
||||||
$mock->expects($this->never())->method($methodName);
|
$fqClassName = $mock;
|
||||||
|
}
|
||||||
|
|
||||||
$this->assertSame(
|
$this->assertSame(
|
||||||
'ParentClass::myMethod##'.$argument,
|
$expected.$argument,
|
||||||
$this->callMethod(
|
$this->callMethod(
|
||||||
$mock,
|
$mock,
|
||||||
'd3CallMockableParent',
|
'd3CallMockableFunction',
|
||||||
[$methodName, [$argument]]
|
[[$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,
|
||||||
|
$this->callMethod(
|
||||||
|
$mock,
|
||||||
|
'd3GetMockableOxNewObject',
|
||||||
|
[Article::class]
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user