1
0
Fork 0
mirror of https://github.com/archtechx/enums.git synced 2025-12-12 20:04:03 +00:00
enums/tests/PHPStan/InvokableCasesTest.php
Samuel Levy 373a86a16e
Added "Invokable Cases" PHPStan extension (#13)
* Added "Invokable Cases" PHPStan extension

This includes a PHPStan extension to add support for `InvokableCases`
so that static analysis tools can understand the callable methods and
their return types.

The extension has been added in a way that will allow multiple extensions
in the future if required, with a single include file that will import
all extensions, or the option to include only specific extensions.

* Added "Invokable Cases" PHPStan extension testing
2022-08-25 00:27:44 +02:00

99 lines
3.8 KiB
PHP

<?php
use ArchTech\Enums\Tests\PHPStan\InvokableCases\InvokableCasesTestCase;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
uses(InvokableCasesTestCase::class);
it('correctly identifies allowable static method calls for invokable pure enum', function () {
$class = \ArchTech\Enums\Tests\PHPStan\InvokableCases\Role::class;
// Base enum
$this->assertStaticallyCallable($class, 'cases');
$this->assertStaticallyCallable($class, 'from', false);
$this->assertStaticallyCallable($class, 'tryFrom', false);
// Defined methods
$this->assertStaticallyCallable($class, 'administrator');
$this->assertStaticallyCallable($class, 'isManager', true, false);
// Cases
$this->assertStaticallyCallable($class, 'admin');
$this->assertStaticallyCallable($class, 'manager');
$this->assertStaticallyCallable($class, 'staff');
// Missing Case
$this->assertStaticallyCallable($class, 'customer', false);
});
it('correctly identifies allowable static method calls for invokable int backed enum', function () {
$class = \ArchTech\Enums\Tests\PHPStan\InvokableCases\Status::class;
// Base enum
$this->assertStaticallyCallable($class, 'cases');
$this->assertStaticallyCallable($class, 'from');
$this->assertStaticallyCallable($class, 'tryFrom');
// Defined methods
$this->assertStaticallyCallable($class, 'initial');
$this->assertStaticallyCallable($class, 'isStarted', true, false);
// Cases
$this->assertStaticallyCallable($class, 'created');
$this->assertStaticallyCallable($class, 'running');
$this->assertStaticallyCallable($class, 'done');
// Missing Case
$this->assertStaticallyCallable($class, 'failed', false);
});
it('correctly identifies allowable static method calls for invokable string backed enum', function () {
$class = \ArchTech\Enums\Tests\PHPStan\InvokableCases\Suits::class;
// Base enum
$this->assertStaticallyCallable($class, 'cases');
$this->assertStaticallyCallable($class, 'from');
$this->assertStaticallyCallable($class, 'tryFrom');
// Defined methods
$this->assertStaticallyCallable($class, 'valuable');
$this->assertStaticallyCallable($class, 'isRed', true, false);
// Cases
$this->assertStaticallyCallable($class, 'clubs');
$this->assertStaticallyCallable($class, 'diamonds');
$this->assertStaticallyCallable($class, 'hearts');
$this->assertStaticallyCallable($class, 'spades');
// Missing Case
$this->assertStaticallyCallable($class, 'joker', false);
});
it('correctly identifies types for invoked pure enum cases', function () {
$class = \ArchTech\Enums\Tests\PHPStan\InvokableCases\Role::class;
// Cases
$this->assertStaticallyCallableType($class, 'admin', StringType::class);
$this->assertStaticallyCallableType($class, 'manager', StringType::class);
$this->assertStaticallyCallableType($class, 'staff', StringType::class);
});
it('correctly identifies types for invoked int backed enum cases', function () {
$class = \ArchTech\Enums\Tests\PHPStan\InvokableCases\Status::class;
// Cases
$this->assertStaticallyCallableType($class, 'created', IntegerType::class);
$this->assertStaticallyCallableType($class, 'running', IntegerType::class);
$this->assertStaticallyCallableType($class, 'done', IntegerType::class);
});
it('correctly identifies types for invoked string backed enum cases', function () {
$class = \ArchTech\Enums\Tests\PHPStan\InvokableCases\Suits::class;
// Cases
$this->assertStaticallyCallableType($class, 'clubs', StringType::class);
$this->assertStaticallyCallableType($class, 'diamonds', StringType::class);
$this->assertStaticallyCallableType($class, 'hearts', StringType::class);
$this->assertStaticallyCallableType($class, 'spades', StringType::class);
});