1
0
Fork 0
mirror of https://github.com/archtechx/enums.git synced 2025-12-12 16:54:03 +00:00
enums/tests/PHPStan/InvokableCasesTest.php
Márk Magyar df3681965c
General code cleanup (#28)
* Add .idea folder to .gitignore

* Remove useless concatenation

* Add phpunit cache directory to .gitignore

* Use iterable type instead of unnecessary checks

* Remove unnecessary null coalescing

If the return value is `null`, it will return `null` anyway. If the method doesn't exist, it will throw an `Error` because of the undefined method.

* Add typehint

* Merge meta properties in one go instead of in a loop

* Simplify control flow

* Use non-deprecated phpstan configuration value

* Add missing types

* Fix style with php-cs-fixer

* Add tests that satisfy the ArrayIterator branch

* Use is() for comparison in in()

* from(int|string), tryFrom(int|string)

---------

Co-authored-by: Samuel Štancl <samuel@archte.ch>
2025-06-07 01:11:53 +02:00

101 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
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);
});