Skip to content

Commit

Permalink
test: pest
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Oct 12, 2023
1 parent a3686fd commit f95081c
Show file tree
Hide file tree
Showing 17 changed files with 1,192 additions and 1,392 deletions.
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
"jiannei/helpers": "^0.2.1"
},
"require-dev": {
"orchestra/testbench": "^6.0",
"phpunit/phpunit": "^9.3.3"
"orchestra/testbench": "^8.0",
"phpunit/phpunit": "^10.0",
"pestphp/pest": "^2.23"
},
"autoload": {
"psr-4": {
Expand All @@ -34,5 +35,10 @@
}
},
"minimum-stability": "dev",
"prefer-stable" : true
"prefer-stable" : true,
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
140 changes: 61 additions & 79 deletions tests/EnumComparisonTest.php
Original file line number Diff line number Diff line change
@@ -1,84 +1,66 @@
<?php

/*
* This file is part of the Jiannei/laravel-enum.
*
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Jiannei\Enum\Laravel\Tests;

uses(\Jiannei\Enum\Laravel\Tests\TestCase::class);
use Jiannei\Enum\Laravel\Tests\Enums\StringValuesEnum;
use Jiannei\Enum\Laravel\Tests\Enums\UserTypeEnum;

class EnumComparisonTest extends TestCase
{
public function testComparisonAgainstPlainValueMatching()
{
// 使用常量实例的 is 方法,比较某个值与当前常量实例的值是否相同
$admin = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

$this->assertTrue($admin->is(UserTypeEnum::ADMINISTRATOR));
}

public function testComparisonAgainstPlainValueNotMatching()
{
// 使用常量实例的 is 方法,比较某个值与当前常量实例的值是否相同
// 对应的,常量实例还有 isNot 方法
$admin = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

$this->assertFalse($admin->is(UserTypeEnum::SUPER_ADMINISTRATOR));
$this->assertFalse($admin->is('some-random-value'));
$this->assertTrue($admin->isNot(UserTypeEnum::SUPER_ADMINISTRATOR));
$this->assertTrue($admin->isNot('some-random-value'));
}

public function testComparisonAgainstItselfMatches()
{
$admin = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

$this->assertTrue($admin->is($admin));
}

public function testComparisonAgainstOtherInstancesMatches()
{
// 比较两个常量实例的值是否相同
$admin = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);
$anotherAdmin = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

$this->assertTrue($admin->is($anotherAdmin));
}

public function testComparisonAgainstOtherInstancesNotMatching()
{
// 比较两个常量实例的值是否相同
$admin = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);
$superAdmin = UserTypeEnum::fromValue(UserTypeEnum::SUPER_ADMINISTRATOR);

$this->assertFalse($admin->is($superAdmin));
}

public function testEnumInstanceInArray()
{
// 常量实例的值是否属于数组
$administrator = new StringValuesEnum(StringValuesEnum::ADMINISTRATOR);

// 数组元素是常量的值
$this->assertTrue($administrator->in([
StringValuesEnum::MODERATOR,
StringValuesEnum::ADMINISTRATOR,
]));

// 数组元素是常量的实例
$this->assertTrue($administrator->in([
new StringValuesEnum(StringValuesEnum::MODERATOR),
new StringValuesEnum(StringValuesEnum::ADMINISTRATOR),
]));

$this->assertTrue($administrator->in([StringValuesEnum::ADMINISTRATOR]));
$this->assertFalse($administrator->in([StringValuesEnum::MODERATOR]));
}
}

test('comparison against plain value matching', function () {
// 使用常量实例的 is 方法,比较某个值与当前常量实例的值是否相同
$admin = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

expect($admin->is(UserTypeEnum::ADMINISTRATOR))->toBeTrue();
});

test('comparison against plain value not matching', function () {
// 使用常量实例的 is 方法,比较某个值与当前常量实例的值是否相同
// 对应的,常量实例还有 isNot 方法
$admin = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

expect($admin->is(UserTypeEnum::SUPER_ADMINISTRATOR))->toBeFalse();
expect($admin->is('some-random-value'))->toBeFalse();
expect($admin->isNot(UserTypeEnum::SUPER_ADMINISTRATOR))->toBeTrue();
expect($admin->isNot('some-random-value'))->toBeTrue();
});

test('comparison against itself matches', function () {
$admin = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

expect($admin->is($admin))->toBeTrue();
});

test('comparison against other instances matches', function () {
// 比较两个常量实例的值是否相同
$admin = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);
$anotherAdmin = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

expect($admin->is($anotherAdmin))->toBeTrue();
});

test('comparison against other instances not matching', function () {
// 比较两个常量实例的值是否相同
$admin = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);
$superAdmin = UserTypeEnum::fromValue(UserTypeEnum::SUPER_ADMINISTRATOR);

expect($admin->is($superAdmin))->toBeFalse();
});

test('enum instance in array', function () {
// 常量实例的值是否属于数组
$administrator = new StringValuesEnum(StringValuesEnum::ADMINISTRATOR);

// 数组元素是常量的值
expect($administrator->in([
StringValuesEnum::MODERATOR,
StringValuesEnum::ADMINISTRATOR,
]))->toBeTrue();

// 数组元素是常量的实例
expect($administrator->in([
new StringValuesEnum(StringValuesEnum::MODERATOR),
new StringValuesEnum(StringValuesEnum::ADMINISTRATOR),
]))->toBeTrue();

expect($administrator->in([StringValuesEnum::ADMINISTRATOR]))->toBeTrue();
expect($administrator->in([StringValuesEnum::MODERATOR]))->toBeFalse();
});
176 changes: 76 additions & 100 deletions tests/EnumInstanceTest.php
Original file line number Diff line number Diff line change
@@ -1,106 +1,82 @@
<?php

/*
* This file is part of the Jiannei/laravel-enum.
*
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Jiannei\Enum\Laravel\Tests;

uses(\Jiannei\Enum\Laravel\Tests\TestCase::class);
use Jiannei\Enum\Laravel\Exceptions\InvalidEnumKeyException;
use Jiannei\Enum\Laravel\Exceptions\InvalidEnumValueException;
use Jiannei\Enum\Laravel\Tests\Enums\UserTypeEnum;

class EnumInstanceTest extends TestCase
{
public function testCanInstantiateEnumClassWithNew()
{
// 使用 new 方式实例化常量
$userType = new UserTypeEnum(UserTypeEnum::ADMINISTRATOR);
$this->assertInstanceOf(UserTypeEnum::class, $userType);
}

public function testCanInstantiateEnumClassFromValue()
{
// 根据常量的值(value)来实例化常量
$userType = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);
$this->assertInstanceOf(UserTypeEnum::class, $userType);
}

public function testCanInstantiateEnumClassFromKey()
{
// 根据常量的名称(key)来实例化常量
$userType = UserTypeEnum::fromKey('ADMINISTRATOR');
$this->assertInstanceOf(UserTypeEnum::class, $userType);
}

public function testAnExceptionIsThrownWhenTryingToInstantiateEnumClassWithAnInvalidEnumValue()
{
// 尝试使用不存在的常量值实例化常量时会抛出异常
$this->expectException(InvalidEnumValueException::class);

UserTypeEnum::fromValue('InvalidValue');
}

public function testAnExceptionIsThrownWhenTryingToInstantiateEnumClassWithAnInvalidEnumKey()
{
// 尝试使用不存在的常量名称实例化常量时会抛出异常
$this->expectException(InvalidEnumKeyException::class);

UserTypeEnum::fromKey('foobar');
}

public function testCanGetTheValueForAnEnumInstance()
{
// 通过常量实例可以获取常量的值(value)
$userType = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

$this->assertEquals($userType->value, UserTypeEnum::ADMINISTRATOR);
}

public function testCanGetTheKeyForAnEnumInstance()
{
// 通过常量实例可以获取常量的名称(key)
$userType = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

$this->assertEquals($userType->key, UserTypeEnum::getKey(UserTypeEnum::ADMINISTRATOR));
}

public function testCanGetTheDescriptionForAnEnumInstance()
{
// 通过常量实例可以获取常量的描述(description)
$userType = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

$this->assertEquals($userType->description, UserTypeEnum::getDescription(UserTypeEnum::ADMINISTRATOR));
}

public function testCanGetEnumInstanceByCallingAnEnumKeyAsAStaticMethod()
{
// 通过静态方法来实例化常量
$this->assertInstanceOf(UserTypeEnum::class, UserTypeEnum::ADMINISTRATOR());
}

public function testMagicInstantiationFromInstanceMethod()
{
// 静态方法、非静态方法
$userType = new UserTypeEnum(UserTypeEnum::ADMINISTRATOR);
$this->assertInstanceOf(UserTypeEnum::class, $userType->magicInstantiationFromInstanceMethod());
}

public function testAnExceptionIsThrownWhenTryingToGetEnumInstanceByCallingAnEnumKeyAsAStaticMethodWhichDoesNotExist()
{
// 通过不合法的静态方法实例化常量时将抛出异常
$this->expectException(InvalidEnumKeyException::class);

UserTypeEnum::KeyWhichDoesNotExist();
}

public function testGettingAnInstanceUsingAnInstanceReturnsAnInstance()
{
$this->assertInstanceOf(UserTypeEnum::class, UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR));
}
}

test('can instantiate enum class with new', function () {
// 使用 new 方式实例化常量
$userType = new UserTypeEnum(UserTypeEnum::ADMINISTRATOR);
expect($userType)->toBeInstanceOf(UserTypeEnum::class);
});

test('can instantiate enum class from value', function () {
// 根据常量的值(value)来实例化常量
$userType = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);
expect($userType)->toBeInstanceOf(UserTypeEnum::class);
});

test('can instantiate enum class from key', function () {
// 根据常量的名称(key)来实例化常量
$userType = UserTypeEnum::fromKey('ADMINISTRATOR');
expect($userType)->toBeInstanceOf(UserTypeEnum::class);
});

test('an exception is thrown when trying to instantiate enum class with an invalid enum value', function () {
// 尝试使用不存在的常量值实例化常量时会抛出异常
$this->expectException(InvalidEnumValueException::class);

UserTypeEnum::fromValue('InvalidValue');
});

test('an exception is thrown when trying to instantiate enum class with an invalid enum key', function () {
// 尝试使用不存在的常量名称实例化常量时会抛出异常
$this->expectException(InvalidEnumKeyException::class);

UserTypeEnum::fromKey('foobar');
});

test('can get the value for an enum instance', function () {
// 通过常量实例可以获取常量的值(value)
$userType = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

expect(UserTypeEnum::ADMINISTRATOR)->toEqual($userType->value);
});

test('can get the key for an enum instance', function () {
// 通过常量实例可以获取常量的名称(key)
$userType = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

expect(UserTypeEnum::getKey(UserTypeEnum::ADMINISTRATOR))->toEqual($userType->key);
});

test('can get the description for an enum instance', function () {
// 通过常量实例可以获取常量的描述(description)
$userType = UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR);

expect(UserTypeEnum::getDescription(UserTypeEnum::ADMINISTRATOR))->toEqual($userType->description);
});

test('can get enum instance by calling an enum key as a static method', function () {
// 通过静态方法来实例化常量
expect(UserTypeEnum::ADMINISTRATOR())->toBeInstanceOf(UserTypeEnum::class);
});

test('magic instantiation from instance method', function () {
// 静态方法、非静态方法
$userType = new UserTypeEnum(UserTypeEnum::ADMINISTRATOR);
expect($userType->magicInstantiationFromInstanceMethod())->toBeInstanceOf(UserTypeEnum::class);
});

test('an exception is thrown when trying to get enum instance by calling an enum key as a static method which does not exist', function () {
// 通过不合法的静态方法实例化常量时将抛出异常
$this->expectException(InvalidEnumKeyException::class);

UserTypeEnum::KeyWhichDoesNotExist();
});

test('getting an instance using an instance returns an instance', function () {
expect(UserTypeEnum::fromValue(UserTypeEnum::ADMINISTRATOR))->toBeInstanceOf(UserTypeEnum::class);
});
Loading

0 comments on commit f95081c

Please sign in to comment.