Browse Source

Code formatting

main
Cédric Belin 4 months ago
parent
commit
03cbeaff99
17 changed files with 336 additions and 329 deletions
  1. +6
    -3
      .editorconfig
  2. +1
    -1
      .github/workflows/build.yaml
  3. +5
    -5
      .vscode/settings.json
  4. +44
    -44
      composer.json
  5. +1
    -1
      doc/index.md
  6. +5
    -5
      doc/installation.md
  7. +6
    -6
      doc/usage.md
  8. +9
    -5
      etc/mkdocs.yaml
  9. +22
    -22
      etc/phpdoc.xml
  10. +2
    -2
      etc/phpstan.neon
  11. +13
    -13
      etc/phpunit.xml
  12. +31
    -31
      example/main.php
  13. +74
    -74
      src/EnumTrait.php
  14. +112
    -112
      test/EnumTraitTest.php
  15. +3
    -3
      tool/clean.ps1
  16. +1
    -1
      tool/doc.ps1
  17. +1
    -1
      tool/upgrade.ps1

+ 6
- 3
.editorconfig View File

@@ -3,11 +3,14 @@ root = true

[*]
charset = utf-8
indent_size = 2
indent_style = space
indent_style = tab
insert_final_newline = true
tab_width = 2
trim_trailing_whitespace = true

[*.md]
indent_size = 4
trim_trailing_whitespace = false

[*.{yaml,yml}]
indent_size = 2
indent_style = space

+ 1
- 1
.github/workflows/build.yaml View File

@@ -3,7 +3,7 @@ on:
pull_request:
push:
schedule:
- cron: '0 0 1 * *'
- cron: "0 0 1 * *"
jobs:
test:
runs-on: ubuntu-latest


+ 5
- 5
.vscode/settings.json View File

@@ -1,7 +1,7 @@
{
"editor.insertSpaces": true,
"editor.tabSize": 2,
"files.encoding": "utf8",
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true
"editor.insertSpaces": false,
"editor.tabSize": 2,
"files.encoding": "utf8",
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true
}

+ 44
- 44
composer.json View File

@@ -1,46 +1,46 @@
{
"description": "A simple implementation of enumerated types.",
"homepage": "https://docs.belin.io/enum.php",
"license": "MIT",
"name": "cedx/enum",
"type": "library",
"version": "8.2.0",
"authors": [
{"email": "cedric@belin.io", "homepage": "https://belin.io", "name": "Cédric Belin"}
],
"autoload": {
"psr-4": {"Enum\\": "src/"}
},
"autoload-dev": {
"psr-4": {"Enum\\": "test/"}
},
"config": {
"optimize-autoloader": true
},
"funding": [
{"type": "patreon", "url": "https://www.patreon.com/cedx"}
],
"keywords": [
"enum",
"enumeration",
"php",
"type"
],
"require": {
"php": ">=7.4.0",
"ext-reflection": "*"
},
"require-dev": {
"cedx/coveralls": "^13.0.0",
"phpstan/phpstan": "^0.12.25",
"phpunit/phpunit": "^9.1.4"
},
"scripts": {
"coverage": "coveralls var/coverage.xml",
"test": "phpunit --configuration=etc/phpunit.xml"
},
"support": {
"docs": "https://api.belin.io/enum.php",
"issues": "https://git.belin.io/cedx/enum.php/issues"
}
"description": "A simple implementation of enumerated types.",
"homepage": "https://docs.belin.io/enum.php",
"license": "MIT",
"name": "cedx/enum",
"type": "library",
"version": "8.2.0",
"authors": [
{"email": "cedric@belin.io", "homepage": "https://belin.io", "name": "Cédric Belin"}
],
"autoload": {
"psr-4": {"Enum\\": "src/"}
},
"autoload-dev": {
"psr-4": {"Enum\\": "test/"}
},
"config": {
"optimize-autoloader": true
},
"funding": [
{"type": "patreon", "url": "https://www.patreon.com/cedx"}
],
"keywords": [
"enum",
"enumeration",
"php",
"type"
],
"require": {
"php": ">=7.4.0",
"ext-reflection": "*"
},
"require-dev": {
"cedx/coveralls": "^13.0.0",
"phpstan/phpstan": "^0.12.25",
"phpunit/phpunit": "^9.1.4"
},
"scripts": {
"coverage": "coveralls var/coverage.xml",
"test": "phpunit --configuration=etc/phpunit.xml"
},
"support": {
"docs": "https://api.belin.io/enum.php",
"issues": "https://git.belin.io/cedx/enum.php/issues"
}
}

+ 1
- 1
doc/index.md View File

@@ -9,7 +9,7 @@ Unlike other [PHP](https://www.php.net) implementations, like the [SplEnum](http
## Quick start
Install the latest version of **Enums for PHP** with [Composer](https://getcomposer.org):

```shell
``` shell
composer require cedx/enum
```



+ 5
- 5
doc/installation.md View File

@@ -6,7 +6,7 @@ and [Composer](https://getcomposer.org), the PHP package manager, up and running

You can verify if you're already good to go with the following commands:

```shell
``` shell
php --version
# PHP 7.4.5 (cli) (built: Apr 14 2020 16:17:19) ( NTS Visual C++ 2017 x64 )

@@ -15,22 +15,22 @@ composer --version
```

!!! info
If you plan to play with the package sources, you will also need the latest versions of
[PowerShell](https://docs.microsoft.com/en-us/powershell) and [Material for MkDocs](https://squidfunk.github.io/mkdocs-material).
If you plan to play with the package sources, you will also need the latest versions of
[PowerShell](https://docs.microsoft.com/en-us/powershell) and [Material for MkDocs](https://squidfunk.github.io/mkdocs-material).

## Installing with Composer package manager

### 1. Install it
From a command prompt, run:

```shell
``` shell
composer require cedx/enum
```

### 2. Import it
Now in your [PHP](https://www.php.net) code, you can use:

```php
``` php
<?php
use Enum\{EnumTrait};
```

+ 6
- 6
doc/usage.md View File

@@ -8,7 +8,7 @@ source: src/EnumTrait.php
## Create the enumeration
Just use the `Enum\EnumTrait` trait on a class:

```php
``` php
<?php
use Enum\{EnumTrait};

@@ -34,7 +34,7 @@ You should only use [scalar constants](https://www.php.net/manual/en/function.is
## Work with the enumeration
Check whether a value is defined among the enumerated type:

```php
``` php
<?php
DayOfWeek::isDefined(DayOfWeek::sunday); // true
DayOfWeek::isDefined(123); // false
@@ -42,7 +42,7 @@ DayOfWeek::isDefined(123); // false

Ensure that a value is defined among the enumerated type:

```php
``` php
<?php
DayOfWeek::assert(DayOfWeek::monday); // DayOfWeek::monday
DayOfWeek::assert(123); // (throws \UnexpectedValueException)
@@ -54,7 +54,7 @@ DayOfWeek::coerce(123, DayOfWeek::tuesday); // DayOfWeek::tuesday

Get the zero-based position of a value in the enumerated type declaration:

```php
``` php
<?php
DayOfWeek::getIndex(DayOfWeek::wednesday); // 3
DayOfWeek::getIndex(123); // -1
@@ -62,7 +62,7 @@ DayOfWeek::getIndex(123); // -1

Get the name associated to an enumerated value:

```php
``` php
<?php
DayOfWeek::getName(DayOfWeek::thursday); // "thursday"
DayOfWeek::getName(123); // "" (empty)
@@ -70,7 +70,7 @@ DayOfWeek::getName(123); // "" (empty)

Get information about the enumerated type:

```php
``` php
<?php
DayOfWeek::getEntries();
// ["sunday" => 0, "monday" => 1, "tuesday" => 2, "wednesday" => 3, "thursday" => 4, "friday" => 5, "saturday" => 6]


+ 9
- 5
etc/mkdocs.yaml View File

@@ -8,19 +8,23 @@ site_dir: ../www

repo_name: git.belin.io
repo_url: https://git.belin.io/cedx/enum.php
edit_uri: ''
edit_uri: ""

copyright: Copyright &copy; 2016 - 2020 Cédric Belin
extra:
social:
- icon: fontawesome/solid/globe
link: 'https://belin.io'
link: "https://belin.io"
name: Belin.io
- icon: fontawesome/brands/github
link: 'https://github.com/cedx'
link: "https://github.com/cedx"
name: GitHub
- icon: fontawesome/brands/twitter
link: 'https://twitter.com/cedxbelin'
link: "https://twitter.com/cedxbelin"
name: Twitter
- icon: fontawesome/brands/linkedin
link: 'https://linkedin.com/in/cedxbelin'
link: "https://linkedin.com/in/cedxbelin"
name: LinkedIn

markdown_extensions:
- admonition


+ 22
- 22
etc/phpdoc.xml View File

@@ -1,25 +1,25 @@
<?xml version="1.0"?>
<phpdocumentor
configVersion="3"
xmlns="http://www.phpdoc.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://docs.phpdoc.org/latest/phpdoc.xsd">
<title>Enums for PHP</title>
<paths>
<cache>../var/phpdoc</cache>
<output>../doc/api</output>
</paths>
<version number="8.2.0">
<api>
<markers>
<marker>TODO</marker>
</markers>
<source dsn="..">
<path>src</path>
</source>
<visibility>protected</visibility>
<visibility>public</visibility>
</api>
</version>
configVersion="3"
xmlns="http://www.phpdoc.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://docs.phpdoc.org/latest/phpdoc.xsd">
<title>Enums for PHP</title>
<paths>
<cache>../var/phpdoc</cache>
<output>../doc/api</output>
</paths>
<version number="8.2.0">
<api>
<markers>
<marker>TODO</marker>
</markers>
<source dsn="..">
<path>src</path>
</source>
<visibility>protected</visibility>
<visibility>public</visibility>
</api>
</version>
</phpdocumentor>

+ 2
- 2
etc/phpstan.neon View File

@@ -1,3 +1,3 @@
parameters:
level: max
paths: [../src, ../test]
level: max
paths: [../src, ../test]

+ 13
- 13
etc/phpunit.xml View File

@@ -1,18 +1,18 @@
<?xml version="1.0"?>
<phpunit bootstrap="../vendor/autoload.php" cacheResult="false" testdox="true">
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../src</directory>
</whitelist>
</filter>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../src</directory>
</whitelist>
</filter>

<logging>
<log type="coverage-clover" target="../var/coverage.xml"/>
</logging>
<logging>
<log type="coverage-clover" target="../var/coverage.xml"/>
</logging>

<testsuites>
<testsuite name="all">
<directory suffix="Test.php">../test</directory>
</testsuite>
</testsuites>
<testsuites>
<testsuite name="all">
<directory suffix="Test.php">../test</directory>
</testsuite>
</testsuites>
</phpunit>

+ 31
- 31
example/main.php View File

@@ -3,46 +3,46 @@ use Enum\{EnumTrait};

/** Specifies the day of the week. */
final class DayOfWeek {
use EnumTrait;
const sunday = 0;
const monday = 1;
const tuesday = 2;
const wednesday = 3;
const thursday = 4;
const friday = 5;
const saturday = 6;
use EnumTrait;
const sunday = 0;
const monday = 1;
const tuesday = 2;
const wednesday = 3;
const thursday = 4;
const friday = 5;
const saturday = 6;
}

/** Works with the enumeration. */
function main(): void {
// Check whether a value is defined among the enumerated type.
DayOfWeek::isDefined(DayOfWeek::sunday); // true
DayOfWeek::isDefined('foo'); // false
// Check whether a value is defined among the enumerated type.
DayOfWeek::isDefined(DayOfWeek::sunday); // true
DayOfWeek::isDefined("foo"); // false

// Ensure that a value is defined among the enumerated type.
DayOfWeek::assert(DayOfWeek::monday); // DayOfWeek::monday
DayOfWeek::assert('foo'); // (throws \UnexpectedValueException)
// Ensure that a value is defined among the enumerated type.
DayOfWeek::assert(DayOfWeek::monday); // DayOfWeek::monday
DayOfWeek::assert("foo"); // (throws \UnexpectedValueException)

DayOfWeek::coerce(DayOfWeek::monday); // DayOfWeek::monday
DayOfWeek::coerce('bar'); // null
DayOfWeek::coerce('baz', DayOfWeek::tuesday); // DayOfWeek::tuesday
DayOfWeek::coerce(DayOfWeek::monday); // DayOfWeek::monday
DayOfWeek::coerce("bar"); // null
DayOfWeek::coerce("baz", DayOfWeek::tuesday); // DayOfWeek::tuesday

// Get the zero-based position of a value in the enumerated type declaration.
DayOfWeek::getIndex(DayOfWeek::wednesday); // 3
DayOfWeek::getIndex('foo'); // -1
// Get the zero-based position of a value in the enumerated type declaration.
DayOfWeek::getIndex(DayOfWeek::wednesday); // 3
DayOfWeek::getIndex("foo"); // -1

// Get the name associated to an enumerated value.
DayOfWeek::getName(DayOfWeek::thursday); // "thursday"
DayOfWeek::getName('foo'); // "" (empty)
// Get the name associated to an enumerated value.
DayOfWeek::getName(DayOfWeek::thursday); // "thursday"
DayOfWeek::getName("foo"); // "" (empty)

// Get information about the enumerated type.
DayOfWeek::getEntries();
// ["sunday" => 0, "monday" => 1, "tuesday" => 2, "wednesday" => 3, "thursday" => 4, "friday" => 5, "saturday" => 6]
// Get information about the enumerated type.
DayOfWeek::getEntries();
// ["sunday" => 0, "monday" => 1, "tuesday" => 2, "wednesday" => 3, "thursday" => 4, "friday" => 5, "saturday" => 6]

DayOfWeek::getNames();
// ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
DayOfWeek::getNames();
// ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]

DayOfWeek::getValues();
// [0, 1, 2, 3, 4, 5, 6]
DayOfWeek::getValues();
// [0, 1, 2, 3, 4, 5, 6]
}

+ 74
- 74
src/EnumTrait.php View File

@@ -4,87 +4,87 @@ namespace Enum;
/** Provides static methods for enumerations. */
trait EnumTrait {

/** Private constructor: prohibits the class instantiation. */
final private function __construct() {}
/** Private constructor: prohibits the class instantiation. */
final private function __construct() {}

/**
* Returns the specified value if it exists in this enumeration, otherwise throws an exception.
* @param mixed $value The value of a constant in this enumeration.
* @return mixed The specified enumerated constant.
* @throws \UnexpectedValueException No such constant was found.
*/
final static function assert($value) {
if (static::isDefined($value)) return $value;
throw new \UnexpectedValueException("Invalid enumerated value: $value");
}
/**
* Returns the specified value if it exists in this enumeration, otherwise throws an exception.
* @param mixed $value The value of a constant in this enumeration.
* @return mixed The specified enumerated constant.
* @throws \UnexpectedValueException No such constant was found.
*/
final static function assert($value) {
if (static::isDefined($value)) return $value;
throw new \UnexpectedValueException("Invalid enumerated value: $value");
}

/**
* Returns the specified value if it exists in this enumeration, otherwise returns the given default value.
* @param mixed $value The value of a constant in this enumeration.
* @param mixed $defaultValue The default value to return if the specified constant does not exist.
* @return mixed The specified enumerated constant, or the default value if no such constant is found.
*/
final static function coerce($value, $defaultValue = null) {
return static::isDefined($value) ? $value : $defaultValue;
}
/**
* Returns the specified value if it exists in this enumeration, otherwise returns the given default value.
* @param mixed $value The value of a constant in this enumeration.
* @param mixed $defaultValue The default value to return if the specified constant does not exist.
* @return mixed The specified enumerated constant, or the default value if no such constant is found.
*/
final static function coerce($value, $defaultValue = null) {
return static::isDefined($value) ? $value : $defaultValue;
}

/**
* Gets an indication whether a constant with a specified value exists in this enumeration.
* @param mixed $value The value of a constant in this enumeration.
* @return bool `true` if a constant in this enumeration has the specified value, otherwise `false`.
*/
final static function isDefined($value): bool {
return in_array($value, static::getValues(), true);
}
/**
* Gets an indication whether a constant with a specified value exists in this enumeration.
* @param mixed $value The value of a constant in this enumeration.
* @return bool `true` if a constant in this enumeration has the specified value, otherwise `false`.
*/
final static function isDefined($value): bool {
return in_array($value, static::getValues(), true);
}

/**
* Gets an associative array of the pairs of names and values of the constants in this enumeration.
* @return array<string, mixed> An associative array that contains the pairs of names and values of the constants in this enumeration.
*/
final static function getEntries(): array {
static $entries;
$entries ??= array_filter(
(new \ReflectionClass(static::class))->getConstants(),
fn($name) => (new \ReflectionClassConstant(static::class, $name))->isPublic(),
ARRAY_FILTER_USE_KEY
);
/**
* Gets an associative array of the pairs of names and values of the constants in this enumeration.
* @return array<string, mixed> An associative array that contains the pairs of names and values of the constants in this enumeration.
*/
final static function getEntries(): array {
static $entries;
$entries ??= array_filter(
(new \ReflectionClass(static::class))->getConstants(),
fn($name) => (new \ReflectionClassConstant(static::class, $name))->isPublic(),
ARRAY_FILTER_USE_KEY
);

return $entries;
}
return $entries;
}

/**
* Gets the zero-based position of the constant in this enumeration that has the specified value.
* @param mixed $value The value of a constant in this enumeration.
* @return int The zero-based position of the constant that has the specified value, or `-1` if no such constant is found.
*/
final static function getIndex($value): int {
$index = array_search($value, static::getValues(), true);
return $index !== false ? $index : -1;
}
/**
* Gets the zero-based position of the constant in this enumeration that has the specified value.
* @param mixed $value The value of a constant in this enumeration.
* @return int The zero-based position of the constant that has the specified value, or `-1` if no such constant is found.
*/
final static function getIndex($value): int {
$index = array_search($value, static::getValues(), true);
return $index !== false ? $index : -1;
}

/**
* Gets the name of the constant in this enumeration that has the specified value.
* @param mixed $value The value of a constant in this enumeration.
* @return string A string containing the name of the constant that has the specified value, or an empty string if no such constant is found.
*/
final static function getName($value): string {
$index = static::getIndex($value);
return $index >= 0 ? static::getNames()[$index] : '';
}
/**
* Gets the name of the constant in this enumeration that has the specified value.
* @param mixed $value The value of a constant in this enumeration.
* @return string A string containing the name of the constant that has the specified value, or an empty string if no such constant is found.
*/
final static function getName($value): string {
$index = static::getIndex($value);
return $index >= 0 ? static::getNames()[$index] : "";
}

/**
* Gets an array of the names of the constants in this enumeration.
* @return string[] An array that contains the names of the constants in this enumeration.
*/
final static function getNames(): array {
return array_keys(static::getEntries());
}
/**
* Gets an array of the names of the constants in this enumeration.
* @return string[] An array that contains the names of the constants in this enumeration.
*/
final static function getNames(): array {
return array_keys(static::getEntries());
}

/**
* Gets an array of the values of the constants in this enumeration.
* @return mixed[] An array that contains the values of the constants in this enumeration.
*/
final static function getValues(): array {
return array_values(static::getEntries());
}
/**
* Gets an array of the values of the constants in this enumeration.
* @return mixed[] An array that contains the values of the constants in this enumeration.
*/
final static function getValues(): array {
return array_values(static::getEntries());
}
}

+ 112
- 112
test/EnumTraitTest.php View File

@@ -6,127 +6,127 @@ use PHPUnit\Framework\{TestCase};

/** A sample enumeration. */
final class SampleEnum {
use EnumTrait;
use EnumTrait;

/** @var bool The first enumerated value. */
const zero = false;
/** @var bool The first enumerated value. */
const zero = false;

/** @var int The second enumerated value. */
const one = 1;
/** @var int The second enumerated value. */
const one = 1;

/** @var string The third enumerated value. */
const two = 'TWO';
/** @var string The third enumerated value. */
const two = "TWO";

/** @var float The fourth enumerated value. */
const three = 3.0;
/** @var float The fourth enumerated value. */
const three = 3.0;

/** @var mixed A protected enumerated value that should be ignored. */
protected const protectedValue = null;
/** @var mixed A protected enumerated value that should be ignored. */
protected const protectedValue = null;

/** @var mixed A private enumerated value that should be ignored. */
private const privateValue = null;
/** @var mixed A private enumerated value that should be ignored. */
private const privateValue = null;
}

/** @testdox Enum\EnumTrait */
class EnumTraitTest extends TestCase {

/** @testdox constructor */
function testConstructor(): void {
// It should create types that are not instantiable.
if ($constructor = (new \ReflectionClass(SampleEnum::class))->getConstructor()) {
assertThat($constructor->isFinal(), isTrue());
assertThat($constructor->isPrivate(), isTrue());
}
}
/** @testdox ::assert() */
function testAssert(): void {
// It should return the specified value if it is a known one.
assertThat(SampleEnum::assert(false), equalTo(SampleEnum::zero));
assertThat(SampleEnum::assert(1), equalTo(SampleEnum::one));
assertThat(SampleEnum::assert('TWO'), equalTo(SampleEnum::two));
assertThat(SampleEnum::assert(3.0), equalTo(SampleEnum::three));
// It should throw an exception if it is an unknown value.
$this->expectException(\UnexpectedValueException::class);
SampleEnum::assert('');
}
/** @testdox ::coerce() */
function testCoerce(): void {
// It should return the specified value if it is a known one.
assertThat(SampleEnum::coerce(false), equalTo(SampleEnum::zero));
assertThat(SampleEnum::coerce(1), equalTo(SampleEnum::one));
assertThat(SampleEnum::coerce('TWO'), equalTo(SampleEnum::two));
assertThat(SampleEnum::coerce(3.0), equalTo(SampleEnum::three));
// It should return the default value if it is an unknown one.
assertThat(SampleEnum::coerce(''), isNull());
assertThat(SampleEnum::coerce(1.0), isNull());
assertThat(SampleEnum::coerce('two', SampleEnum::zero), equalTo(SampleEnum::zero));
assertThat(SampleEnum::coerce(3.1, SampleEnum::two), equalTo(SampleEnum::two));
}
/** @testdox ::isDefined() */
function testIsDefined(): void {
// It should return `false` for unknown values.
assertThat(SampleEnum::isDefined(''), isFalse());
assertThat(SampleEnum::isDefined(1.0), isFalse());
assertThat(SampleEnum::isDefined('two'), isFalse());
assertThat(SampleEnum::isDefined(3.1), isFalse());
// It should return `true` for known values.
assertThat(SampleEnum::isDefined(false), isTrue());
assertThat(SampleEnum::isDefined(1), isTrue());
assertThat(SampleEnum::isDefined('TWO'), isTrue());
assertThat(SampleEnum::isDefined(3.0), isTrue());
}
/** @testdox ::getEntries() */
function testGetEntries(): void {
// It should return the pairs of names and values of the enumerated constants.
assertThat(SampleEnum::getEntries(), equalTo(['zero' => false, 'one' => 1, 'two' => 'TWO', 'three' => 3.0]));
}
/** @testdox ::getIndex() */
function testGetIndex(): void {
// It should return `-1` for unknown values.
assertThat(SampleEnum::getIndex(0), equalTo(-1));
assertThat(SampleEnum::getIndex(1.0), equalTo(-1));
assertThat(SampleEnum::getIndex('two'), equalTo(-1));
assertThat(SampleEnum::getIndex(3.1), equalTo(-1));
// It should return the index of the enumerated constant for known values.
assertThat(SampleEnum::getIndex(false), equalTo(0));
assertThat(SampleEnum::getIndex(1), equalTo(1));
assertThat(SampleEnum::getIndex('TWO'), equalTo(2));
assertThat(SampleEnum::getIndex(3.0), equalTo(3));
}
/** @testdox ::getName() */
function testGetName(): void {
// It should return an empty string for unknown values.
assertThat(SampleEnum::getName(0), isEmpty());
assertThat(SampleEnum::getName(1.0), isEmpty());
assertThat(SampleEnum::getName('two'), isEmpty());
assertThat(SampleEnum::getName(3.1), isEmpty());
// It should return the name for known values.
assertThat(SampleEnum::getName(false), equalTo('zero'));
assertThat(SampleEnum::getName(1), equalTo('one'));
assertThat(SampleEnum::getName('TWO'), equalTo('two'));
assertThat(SampleEnum::getName(3.0), equalTo('three'));
}
/** @testdox ::getNames() */
function testGetNames(): void {
// It should return the names of the enumerated constants.
assertThat(SampleEnum::getNames(), equalTo(['zero', 'one', 'two', 'three']));
}
/** @testdox ::getValues() */
function testGetValues(): void {
// It should return the values of the enumerated constants.
assertThat(SampleEnum::getValues(), equalTo([false, 1, 'TWO', 3.0]));
}
/** @testdox constructor */
function testConstructor(): void {
// It should create types that are not instantiable.
if ($constructor = (new \ReflectionClass(SampleEnum::class))->getConstructor()) {
assertThat($constructor->isFinal(), isTrue());
assertThat($constructor->isPrivate(), isTrue());
}
}
/** @testdox ::assert() */
function testAssert(): void {
// It should return the specified value if it is a known one.
assertThat(SampleEnum::assert(false), equalTo(SampleEnum::zero));
assertThat(SampleEnum::assert(1), equalTo(SampleEnum::one));
assertThat(SampleEnum::assert("TWO"), equalTo(SampleEnum::two));
assertThat(SampleEnum::assert(3.0), equalTo(SampleEnum::three));
// It should throw an exception if it is an unknown value.
$this->expectException(\UnexpectedValueException::class);
SampleEnum::assert("");
}
/** @testdox ::coerce() */
function testCoerce(): void {
// It should return the specified value if it is a known one.
assertThat(SampleEnum::coerce(false), equalTo(SampleEnum::zero));
assertThat(SampleEnum::coerce(1), equalTo(SampleEnum::one));
assertThat(SampleEnum::coerce("TWO"), equalTo(SampleEnum::two));
assertThat(SampleEnum::coerce(3.0), equalTo(SampleEnum::three));
// It should return the default value if it is an unknown one.
assertThat(SampleEnum::coerce(""), isNull());
assertThat(SampleEnum::coerce(1.0), isNull());
assertThat(SampleEnum::coerce("two", SampleEnum::zero), equalTo(SampleEnum::zero));
assertThat(SampleEnum::coerce(3.1, SampleEnum::two), equalTo(SampleEnum::two));
}
/** @testdox ::isDefined() */
function testIsDefined(): void {
// It should return `false` for unknown values.
assertThat(SampleEnum::isDefined(""), isFalse());
assertThat(SampleEnum::isDefined(1.0), isFalse());
assertThat(SampleEnum::isDefined("two"), isFalse());
assertThat(SampleEnum::isDefined(3.1), isFalse());
// It should return `true` for known values.
assertThat(SampleEnum::isDefined(false), isTrue());
assertThat(SampleEnum::isDefined(1), isTrue());
assertThat(SampleEnum::isDefined("TWO"), isTrue());
assertThat(SampleEnum::isDefined(3.0), isTrue());
}
/** @testdox ::getEntries() */
function testGetEntries(): void {
// It should return the pairs of names and values of the enumerated constants.
assertThat(SampleEnum::getEntries(), equalTo(["zero" => false, "one" => 1, "two" => "TWO", "three" => 3.0]));
}
/** @testdox ::getIndex() */
function testGetIndex(): void {
// It should return `-1` for unknown values.
assertThat(SampleEnum::getIndex(0), equalTo(-1));
assertThat(SampleEnum::getIndex(1.0), equalTo(-1));
assertThat(SampleEnum::getIndex("two"), equalTo(-1));
assertThat(SampleEnum::getIndex(3.1), equalTo(-1));
// It should return the index of the enumerated constant for known values.
assertThat(SampleEnum::getIndex(false), equalTo(0));
assertThat(SampleEnum::getIndex(1), equalTo(1));
assertThat(SampleEnum::getIndex("TWO"), equalTo(2));
assertThat(SampleEnum::getIndex(3.0), equalTo(3));
}
/** @testdox ::getName() */
function testGetName(): void {
// It should return an empty string for unknown values.
assertThat(SampleEnum::getName(0), isEmpty());
assertThat(SampleEnum::getName(1.0), isEmpty());
assertThat(SampleEnum::getName("two"), isEmpty());
assertThat(SampleEnum::getName(3.1), isEmpty());
// It should return the name for known values.
assertThat(SampleEnum::getName(false), equalTo("zero"));
assertThat(SampleEnum::getName(1), equalTo("one"));
assertThat(SampleEnum::getName("TWO"), equalTo("two"));
assertThat(SampleEnum::getName(3.0), equalTo("three"));
}
/** @testdox ::getNames() */
function testGetNames(): void {
// It should return the names of the enumerated constants.
assertThat(SampleEnum::getNames(), equalTo(["zero", "one", "two", "three"]));
}
/** @testdox ::getValues() */
function testGetValues(): void {
// It should return the values of the enumerated constants.
assertThat(SampleEnum::getValues(), equalTo([false, 1, "TWO", 3.0]));
}
}

+ 3
- 3
tool/clean.ps1 View File

@@ -2,10 +2,10 @@
Set-StrictMode -Version Latest
Set-Location (Split-Path $PSScriptRoot)

foreach ($item in 'build', 'doc/api', 'www') {
if (Test-Path $item) { Remove-Item $item -Recurse }
foreach ($item in "build", "doc/api", "www") {
if (Test-Path $item) { Remove-Item $item -Recurse }
}

foreach ($item in Get-ChildItem var -Exclude .gitkeep) {
Remove-Item $item -Recurse
Remove-Item $item -Recurse
}

+ 1
- 1
tool/doc.ps1 View File

@@ -2,7 +2,7 @@
Set-StrictMode -Version Latest
Set-Location (Split-Path $PSScriptRoot)

$phpdoc = $IsWindows ? 'php "C:/Program Files/PHP/share/phpDocumentor.phar"' : 'phpdoc';
$phpdoc = $IsWindows ? 'php "C:/Program Files/PHP/share/phpDocumentor.phar"' : "phpdoc";
Invoke-Expression "$phpdoc --config=etc/phpdoc.xml"

if (-not (Test-Path doc/api/images)) { New-Item doc/api/images -ItemType Directory | Out-Null }


+ 1
- 1
tool/upgrade.ps1 View File

@@ -6,5 +6,5 @@ git reset --hard
git fetch --all --prune
git pull --rebase

$composer = $IsWindows ? 'php "C:/Program Files/PHP/share/composer.phar"' : 'composer'
$composer = $IsWindows ? 'php "C:/Program Files/PHP/share/composer.phar"' : "composer"
Invoke-Expression "$composer update --no-interaction"

Loading…
Cancel
Save