105 lines
No EOL
3.3 KiB
PHP
105 lines
No EOL
3.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Tests\TestCase;
|
|
|
|
class ControllerTest extends TestCase
|
|
{
|
|
public function test_controller_is_abstract(): void
|
|
{
|
|
$reflection = new \ReflectionClass(Controller::class);
|
|
|
|
$this->assertTrue($reflection->isAbstract());
|
|
}
|
|
|
|
public function test_controller_can_be_extended(): void
|
|
{
|
|
$testController = new class extends Controller {
|
|
public function testMethod(): string
|
|
{
|
|
return 'test';
|
|
}
|
|
};
|
|
|
|
$this->assertInstanceOf(Controller::class, $testController);
|
|
$this->assertEquals('test', $testController->testMethod());
|
|
}
|
|
|
|
public function test_controller_can_be_used_as_base_class(): void
|
|
{
|
|
$reflection = new \ReflectionClass(Controller::class);
|
|
|
|
// Check that it's an abstract class that can be extended
|
|
$this->assertTrue($reflection->isAbstract());
|
|
$this->assertNotNull($reflection->getName());
|
|
}
|
|
|
|
public function test_controller_namespace_is_correct(): void
|
|
{
|
|
$reflection = new \ReflectionClass(Controller::class);
|
|
|
|
$this->assertEquals('App\Http\Controllers', $reflection->getNamespaceName());
|
|
}
|
|
|
|
public function test_controller_has_no_methods(): void
|
|
{
|
|
$reflection = new \ReflectionClass(Controller::class);
|
|
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
|
|
|
|
// Filter out methods inherited from parent classes
|
|
$ownMethods = array_filter($methods, function ($method) {
|
|
return $method->getDeclaringClass()->getName() === Controller::class;
|
|
});
|
|
|
|
$this->assertEmpty($ownMethods, 'Controller should not define any methods of its own');
|
|
}
|
|
|
|
public function test_controller_has_no_properties(): void
|
|
{
|
|
$reflection = new \ReflectionClass(Controller::class);
|
|
$properties = $reflection->getProperties();
|
|
|
|
// Filter out properties inherited from parent classes
|
|
$ownProperties = array_filter($properties, function ($property) {
|
|
return $property->getDeclaringClass()->getName() === Controller::class;
|
|
});
|
|
|
|
$this->assertEmpty($ownProperties, 'Controller should not define any properties of its own');
|
|
}
|
|
|
|
public function test_multiple_inheritance_works(): void
|
|
{
|
|
$firstController = new class extends Controller {
|
|
public function method1(): string
|
|
{
|
|
return 'first';
|
|
}
|
|
};
|
|
|
|
$secondController = new class extends Controller {
|
|
public function method2(): string
|
|
{
|
|
return 'second';
|
|
}
|
|
};
|
|
|
|
$this->assertInstanceOf(Controller::class, $firstController);
|
|
$this->assertInstanceOf(Controller::class, $secondController);
|
|
$this->assertEquals('first', $firstController->method1());
|
|
$this->assertEquals('second', $secondController->method2());
|
|
}
|
|
|
|
public function test_controller_can_be_used_as_type_hint(): void
|
|
{
|
|
$testFunction = function (Controller $controller): bool {
|
|
return $controller instanceof Controller;
|
|
};
|
|
|
|
$testController = new class extends Controller {
|
|
};
|
|
|
|
$this->assertTrue($testFunction($testController));
|
|
}
|
|
} |