mirror of
https://github.com/archtechx/enums.git
synced 2025-12-12 18:44:04 +00:00
* 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>
36 lines
1 KiB
PHP
36 lines
1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use ArchTech\Enums\Exceptions\UndefinedCaseError;
|
|
|
|
it('can be used as a static method with backed enums', function () {
|
|
expect(Status::PENDING())->toBe(0);
|
|
expect(Status::DONE())->toBe(1);
|
|
});
|
|
|
|
it('can be used as a static method with pure enums', function () {
|
|
expect(Role::ADMIN())->toBe('ADMIN');
|
|
expect(Role::GUEST())->toBe('GUEST');
|
|
});
|
|
|
|
it('can be invoked as an instance as a backed enum', function () {
|
|
$status = Status::PENDING;
|
|
|
|
expect($status())->toBe(0);
|
|
expect($status())->toBe($status->value);
|
|
});
|
|
|
|
it('can be invoked as an instance as a pure enum', function () {
|
|
$role = Role::ADMIN;
|
|
|
|
expect($role())->toBe('ADMIN');
|
|
});
|
|
|
|
it('throws an error when a nonexistent case is being used for backed enums', function () {
|
|
Status::INVALID();
|
|
})->expectException(UndefinedCaseError::class);
|
|
|
|
it('throws an error when a nonexistent case is being used for pure enums', function () {
|
|
Role::INVALID();
|
|
})->expectException(UndefinedCaseError::class);
|