This asserts module is prepared to test code generation tools to assert existing of classes, methods, properties.
Use composer to install:
composer require maslosoft/maslosoft/codeception-reflection-assertsAdd to tests/unit.suite.yml:
actor: UnitTester
suite_namespace: Tests\Unit
modules:
enabled:
- \Maslosoft\Codeception\Module\ReflectionAsserts$I->assertMethodExists(Foo::class, 'bar');
$I->assertMethodIsPublic(Foo::class, 'bar');
$I->assertPropertyExists(Foo::class, 'baz');
$I->assertPropertyIsPrivate(Foo::class, 'baz');
$I->assertMethodHasParameter(
Foo::class,
'bar',
'limit',
type: 'int',
allowsNull: false,
optional: true
);<?php
...
class SomeTest extends Unit
{
use Maslosoft\Testing\Reflection\ReflectionAssertionsTrait;
function testMyReflection(): void
{
$this->assertClassExists(SomeService::class);
$this->assertMethodExists(SomeService::class, 'handle');
$this->assertMethodIsPublic(SomeService::class, 'handle');
$this->assertMethodHasParameter(
SomeService::class,
'handle',
'request',
type: Request::class,
allowsNull: false,
optional: false
);
$this->assertPropertyExists(SomeService::class, 'repository');
$this->assertPropertyIsPrivate(SomeService::class, 'repository');
}
}