├── .editorconfig
├── .gitattributes
├── .gitignore
├── LICENSE_1_0.txt
├── README.md
├── composer.json
├── ecs.php
├── infection.json
├── phpunit.xml
├── psalm.xml
├── src
├── Method.php
├── ReasonPhrase.php
└── StatusCode.php
└── tests
├── MethodNamedTest.php
├── MethodPositionalTest.php
├── ReasonPhraseNamedTest.php
├── ReasonPhrasePositionalTest.php
├── StatusCodeNamedTest.php
└── StatusCodePositionalTest.php
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | indent_style = space
5 | indent_size = 4
6 | tab_width = 4
7 | end_of_line = lf
8 | charset = utf-8
9 | trim_trailing_whitespace = true
10 | insert_final_newline = true
11 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | * text=auto eol=lf
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /.phpunit.cache/
2 | /composer.lock
3 | /infection.log
4 | /vendor/
5 |
--------------------------------------------------------------------------------
/LICENSE_1_0.txt:
--------------------------------------------------------------------------------
1 | Boost Software License - Version 1.0 - August 17th, 2003
2 |
3 | Permission is hereby granted, free of charge, to any person or organization
4 | obtaining a copy of the software and accompanying documentation covered by
5 | this license (the "Software") to use, reproduce, display, distribute,
6 | execute, and transmit the Software, and to prepare derivative works of the
7 | Software, and to permit third-parties to whom the Software is furnished to
8 | do so, all subject to the following:
9 |
10 | The copyright notices in the Software and this entire statement, including
11 | the above license grant, this restriction and the following disclaimer,
12 | must be included in all copies of the Software, in whole or in part, and
13 | all derivative works of the Software, unless such copies or derivative
14 | works are solely in the form of machine-executable object code generated by
15 | a source language processor.
16 |
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 | DEALINGS IN THE SOFTWARE.
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # HTTP Enums For PHP 8.1 and above
2 |
3 | [](https://packagist.org/packages/alexanderpas/http-enum)
4 | [](https://packagist.org/packages/alexanderpas/http-enum)
5 | [](https://packagist.org/packages/alexanderpas/http-enum)
6 | [](https://packagist.org/packages/alexanderpas/http-enum)
7 |
8 | This package provides HTTP Methods, Status Codes and Reason Phrases as PHP 8.1+ enums
9 |
10 | All [IANA registered HTTP Status codes][STATUS] and corresponding Reason Phrases as of the latest update on 2018-09-21 are supported.
11 |
12 | This includes the HTTP Methods defined in [RFC 5789] and [RFC 7231], as well as all Status Codes and Reason Phrases as defined in [HTTP/1.1] ([RFC 7231], [RFC 7232], [RFC 7233], [RFC 7235]) and [HTTP/2] ([RFC 7540]) as well as other RFC's defining HTTP status codes such as [WebDAV] ([RFC 2518], [RFC 4918], [RFC 5842], [RFC 8144]) and more ([RFC 8297], [RFC 3229], [RFC 7538], [RFC 7694], [RFC 6585], [RFC 7725], [RFC 2295], [RFC 2774])
13 |
14 | ## Requirements
15 |
16 | - PHP 8.1 or above
17 |
18 | ## Installation
19 |
20 | ### Composer:
21 |
22 | composer require alexanderpas/http-enum
23 |
24 | ### Manually (Without Composer):
25 |
26 | include the `src/Method.php` file in order to use the HTTP methods enum.
27 |
28 | include both the `src/ReasonPhrase.php` file as well as the `src/StatusCode.php` file in order to use the HTTP Status Code enum or the HTTP Reason Phrase enum.
29 |
30 | ## Available Enums and Enum methods
31 |
32 | All available Enums live in the `\Alexanderpas\Common\HTTP` namespace.
33 |
34 | - HTTP Methods are represented by the `\Alexanderpas\Common\HTTP\Method` enum.
35 | - HTTP Status Codes are represented by the `\Alexanderpas\Common\HTTP\StatusCode` enum.
36 | - HTTP Reason Phrases are represented by the `\Alexanderpas\Common\HTTP\ReasonPhrase` enum.
37 |
38 | In addition to the Enum methods available by default on Backed Enums, the following Enum methods are available.
39 |
40 | - `Method::fromName(string $name): Method` Gives back a HTTP method enum when provided with a valid HTTP method. (such as `'GET'` or `'POST'` or `'put'` or `'pAtcH'`)
41 | - `StatusCode::fromInteger(int $integer): StatusCode` Gives back a HTTP Status Code enum when provided with a valid HTTP status code as integer. (such as `200` or `404`)
42 | - `StatusCode::fromName(string $name): StatusCode` Gives back a HTTP Status Code enum when provided with a valid HTTP status code as a `HTTP_` prefixed string. (such as `'HTTP_200'` or `'HTTP_404'`)
43 | - `ReasonPhrase::fromInteger(int $integer): ReasonPhrase` Gives back a HTTP Reason Phrase enum when provided with a valid HTTP status code as integer. (such as `200` or `404`)
44 | - `ReasonPhrase::fromName(string $name): ReasonPhrase` Gives back a HTTP Reason Phrase enum when provided with a valid HTTP status code as a `HTTP_` prefixed string. (such as `'HTTP_200'` or `'HTTP_404'`)
45 |
46 | All of the above methods also have a try variant (such as `Method::tryFromName(?string $name): ?Method`), which returns `null` if an invalid value of the correct type has been given instead of throwing an exception.
47 |
48 | Additionally, you can change between Status Code enums and Reason Phrase enums using the following methods:
49 |
50 | - `ReasonPhrase::fromStatusCode(StatusCode $statusCode): ReasonPhrase` changes a Status Code enum into the corresponding Reason Phrase enum.
51 | - `StatusCode::fromReasonPhrase(ReasonPhrase $reasonPhrase): StatusCode` changes a Reason Phrase enum into the corresponding Status Code enum.
52 |
53 | These methods do not have a try variant.
54 |
55 | You can get the respective string or integer representation as usual by reading the `value` attribute on the enum.
56 |
57 | ## License
58 |
59 | Copyright Alexander Pas 2021.
60 | Distributed under the Boost Software License, Version 1.0.
61 | (See accompanying file [LICENSE_1_0.txt][LICENSE] or copy at https://www.boost.org/LICENSE_1_0.txt)
62 |
63 | ## Notes
64 |
65 | - Support for the HTTP status code 306 has intentionally been removed as it has been defined as Unused in [RFC 7231, Section 6.4.6][RFC 7231]
66 | - The Methods, Status Codes and Reason Phrases defined in the [Hyper Text Coffee Pot Control Protocol][HTCPCP] ([RFC 2324]) are not supported as they aren't properly registered and provide Methods unique to that specific protocol.
67 | - The [Request Methods specific to WebDAV][WebDAV] are not supported.
68 |
69 |
70 | [LICENSE]: LICENSE_1_0.txt
71 | [RFC 2295]: https://www.iana.org/go/rfc2295
72 | [RFC 2324]: https://www.iana.org/go/rfc2324
73 | [RFC 2518]: https://www.iana.org/go/rfc2518
74 | [RFC 2774]: https://www.iana.org/go/rfc2774
75 | [RFC 3229]: https://www.iana.org/go/rfc3229
76 | [RFC 4918]: https://www.iana.org/go/rfc4918
77 | [RFC 5789]: https://www.iana.org/go/rfc5789
78 | [RFC 5842]: https://www.iana.org/go/rfc5842
79 | [RFC 6585]: https://www.iana.org/go/rfc6585
80 | [RFC 7231]: https://www.iana.org/go/rfc7231
81 | [RFC 7232]: https://www.iana.org/go/rfc7232
82 | [RFC 7233]: https://www.iana.org/go/rfc7233
83 | [RFC 7235]: https://www.iana.org/go/rfc7235
84 | [RFC 7538]: https://www.iana.org/go/rfc7538
85 | [RFC 7540]: https://www.iana.org/go/rfc7540
86 | [RFC 7725]: https://www.iana.org/go/rfc7725
87 | [RFC 7694]: https://www.iana.org/go/rfc7694
88 | [RFC 8144]: https://www.iana.org/go/rfc8144
89 | [RFC 8297]: https://www.iana.org/go/rfc8297
90 | [RFC 8470]: https://www.iana.org/go/rfc8470
91 | [STATUS]: https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
92 | [HTTP/1.1]: https://en.wikipedia.org/wiki/HTTP/1.1
93 | [HTTP/2]: https://en.wikipedia.org/wiki/HTTP/2
94 | [WebDAV]: https://en.wikipedia.org/wiki/WebDAV
95 | [HTCPCP]: https://en.wikipedia.org/wiki/HTCPCP
96 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "alexanderpas/http-enum",
3 | "type": "library",
4 | "license": "BSL-1.0",
5 | "authors": [
6 | {
7 | "name": "Alexander Pas",
8 | "email": "git@dropdev.org"
9 | }
10 | ],
11 | "require": {
12 | "php": "^8.1"
13 | },
14 | "require-dev": {
15 | "ergebnis/composer-normalize": "^2.15",
16 | "infection/infection": "^0.25.2",
17 | "php-parallel-lint/php-parallel-lint": "^1.3",
18 | "phpunit/phpunit": "^10",
19 | "sebastian/diff": "5.0.x-dev as 4.x-dev",
20 | "symfony/dependency-injection": "^5.3",
21 | "symplify/easy-coding-standard": "^9.4",
22 | "vimeo/psalm": "^4.10"
23 | },
24 | "config": {
25 | "sort-packages": true
26 | },
27 | "extra": {
28 | "branch-alias": {
29 | "dev-master": "1.0.x-dev"
30 | }
31 | },
32 | "autoload": {
33 | "psr-4": {
34 | "Alexanderpas\\Common\\HTTP\\": "src/"
35 | }
36 | },
37 | "autoload-dev": {
38 | "psr-4": {
39 | "Alexanderpas\\Common\\HTTP\\Tests\\": "tests/"
40 | }
41 | },
42 | "minimum-stability": "dev",
43 | "prefer-stable": true,
44 | "scripts": {
45 | "post-update-cmd": [
46 | "@composer normalize"
47 | ],
48 | "test": [
49 | "parallel-lint --exclude vendor src tests ecs.php",
50 | "ecs",
51 | "phpunit",
52 | "psalm --no-cache"
53 | ]
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/ecs.php:
--------------------------------------------------------------------------------
1 | parameters();
22 | $parameters->set(Option::LINE_ENDING, "\n");
23 | $parameters->set(Option::PATHS, [
24 | __DIR__ . '/ecs.php',
25 | __DIR__ . '/src',
26 | __DIR__ . '/tests',
27 | ]);
28 | $parameters->set(Option::SKIP, [
29 | NotOperatorWithSuccessorSpaceFixer::class => null,
30 | SwitchCaseSemicolonToColonFixer::class => null,
31 | ]);
32 |
33 | $services = $containerConfigurator->services();
34 | $services->set(ArraySyntaxFixer::class)
35 | ->call('configure', [[
36 | 'syntax' => 'short',
37 | ]]);
38 |
39 | // run and fix, one by one
40 | $containerConfigurator->import(SetList::SPACES);
41 | $containerConfigurator->import(SetList::ARRAY);
42 | $containerConfigurator->import(SetList::DOCBLOCK);
43 | $containerConfigurator->import(SetList::PSR_12);
44 | };
45 |
--------------------------------------------------------------------------------
/infection.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "./vendor/infection/infection/resources/schema.json",
3 | "source": {
4 | "directories": [
5 | "src/"
6 | ]
7 | },
8 | "logs": {
9 | "text": "infection.log"
10 | },
11 | "mutators": {
12 | "@default": true
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
14 |
15 |
16 | tests
17 |
18 |
19 |
20 |
22 |
23 | src
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/psalm.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/src/Method.php:
--------------------------------------------------------------------------------
1 | name
133 | */
134 | return self::fromName($statusCode->name);
135 | }
136 |
137 | public static function tryFromName(?string $name): ?ReasonPhrase
138 | {
139 | if (defined("self::$name")) {
140 | /**
141 | * @var ReasonPhrase
142 | */
143 | $enumCase = constant("self::$name");
144 | return $enumCase;
145 | }
146 |
147 | return null;
148 | }
149 |
150 | public static function tryFromInteger(?int $integer): ?ReasonPhrase
151 | {
152 | $statusCode = StatusCode::tryFromInteger($integer);
153 | if (is_null($statusCode)) {
154 | return null;
155 | }
156 | return self::FromStatusCode($statusCode);
157 | }
158 | }
159 |
--------------------------------------------------------------------------------
/src/StatusCode.php:
--------------------------------------------------------------------------------
1 | name
133 | */
134 | return self::fromName($reasonPhrase->name);
135 | }
136 |
137 | public static function tryFromName(?string $name): ?StatusCode
138 | {
139 | if (defined("self::$name")) {
140 | /**
141 | * @var StatusCode
142 | */
143 | $enumCase = constant("self::$name");
144 | return $enumCase;
145 | }
146 |
147 | return null;
148 | }
149 |
150 | public static function tryFromInteger(?int $integer): ?StatusCode
151 | {
152 | if (is_null($integer)) {
153 | return null;
154 | }
155 |
156 | /**
157 | * @var ?StatusCode
158 | * @psalm-suppress UndefinedMethod
159 | */
160 | $statusCode = self::tryFrom($integer);
161 | return $statusCode;
162 | }
163 | }
164 |
--------------------------------------------------------------------------------
/tests/MethodNamedTest.php:
--------------------------------------------------------------------------------
1 | cases = Method::cases();
33 | }
34 |
35 | public function testCaconicalNameCapitalization(): void
36 | {
37 | foreach ($this->cases as $case) {
38 | /**
39 | * @var string $case->name
40 | */
41 | $name = $case->name;
42 | $canonicalName = strtoupper($case->name);
43 | Assert::assertThat($name, Assert::identicalTo($canonicalName));
44 | }
45 | }
46 |
47 | public function testFromNamesUppercase(): void
48 | {
49 | foreach ($this->cases as $case) {
50 | /**
51 | * @var string $case->name
52 | */
53 | $name = strtoupper($case->name);
54 | $method = Method::fromName(name: $name);
55 | Assert::assertThat($method, Assert::identicalTo($case));
56 | }
57 | }
58 |
59 | public function testFromNamesLowercase(): void
60 | {
61 | foreach ($this->cases as $case) {
62 | /**
63 | * @var string $case->name
64 | */
65 | $name = strtolower($case->name);
66 | $method = Method::fromName(name: $name);
67 | Assert::assertThat($method, Assert::identicalTo($case));
68 | }
69 | }
70 |
71 | public function testFromNamesTitlecase(): void
72 | {
73 | foreach ($this->cases as $case) {
74 | /**
75 | * @var string $case->name
76 | */
77 | $name = ucfirst(strtolower($case->name));
78 | $method = Method::fromName(name: $name);
79 | Assert::assertThat($method, Assert::identicalTo($case));
80 | }
81 | }
82 |
83 | public function testFromNamesInvertedTitlecase(): void
84 | {
85 | foreach ($this->cases as $case) {
86 | /**
87 | * @var string $case->name
88 | */
89 | $name = lcfirst(strtoupper($case->name));
90 | $method = Method::fromName(name: $name);
91 | Assert::assertThat($method, Assert::identicalTo($case));
92 | }
93 | }
94 |
95 | public function testTryFromNamesUppercase(): void
96 | {
97 | foreach ($this->cases as $case) {
98 | /**
99 | * @var string $case->name
100 | */
101 | $name = strtoupper($case->name);
102 | $method = Method::tryFromName(name: $name);
103 | Assert::assertThat($method, Assert::identicalTo($case));
104 | }
105 | }
106 |
107 | public function testTryFromNamesLowercase(): void
108 | {
109 | foreach ($this->cases as $case) {
110 | /**
111 | * @var string $case->name
112 | */
113 | $name = strtolower($case->name);
114 | $method = Method::tryFromName(name: $name);
115 | Assert::assertThat($method, Assert::identicalTo($case));
116 | }
117 | }
118 |
119 | public function testTryFromNamesTitlecase(): void
120 | {
121 | foreach ($this->cases as $case) {
122 | /**
123 | * @var string $case->name
124 | */
125 | $name = ucfirst(strtolower($case->name));
126 | $method = Method::tryFromName(name: $name);
127 | Assert::assertThat($method, Assert::identicalTo($case));
128 | }
129 | }
130 |
131 | public function testTryFromNamesInvertedTitlecase(): void
132 | {
133 | foreach ($this->cases as $case) {
134 | /**
135 | * @var string $case->name
136 | */
137 | $name = lcfirst(strtoupper($case->name));
138 | $method = Method::tryFromName(name: $name);
139 | Assert::assertThat($method, Assert::identicalTo($case));
140 | }
141 | }
142 |
143 | public function testNullFromName(): void
144 | {
145 | $method = Method::tryFromName(name: null);
146 | Assert::assertThat($method, Assert::isNull());
147 | }
148 |
149 | public function testInvalidFromName(): void
150 | {
151 | $invalidName = 'INVALID';
152 | $method = Method::tryFromName(name: $invalidName);
153 | Assert::assertThat($method, Assert::isNull());
154 | $this->expectException(ValueError::class);
155 | Method::fromName(name: $invalidName);
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/tests/MethodPositionalTest.php:
--------------------------------------------------------------------------------
1 | cases = Method::cases();
33 | }
34 |
35 | public function testCaconicalNameCapitalization(): void
36 | {
37 | foreach ($this->cases as $case) {
38 | /**
39 | * @var string $case->name
40 | */
41 | $name = $case->name;
42 | $canonicalName = strtoupper($case->name);
43 | Assert::assertThat($name, Assert::identicalTo($canonicalName));
44 | }
45 | }
46 |
47 | public function testFromNamesUppercase(): void
48 | {
49 | foreach ($this->cases as $case) {
50 | /**
51 | * @var string $case->name
52 | */
53 | $name = strtoupper($case->name);
54 | $method = Method::fromName($name);
55 | Assert::assertThat($method, Assert::identicalTo($case));
56 | }
57 | }
58 |
59 | public function testFromNamesLowercase(): void
60 | {
61 | foreach ($this->cases as $case) {
62 | /**
63 | * @var string $case->name
64 | */
65 | $name = strtolower($case->name);
66 | $method = Method::fromName($name);
67 | Assert::assertThat($method, Assert::identicalTo($case));
68 | }
69 | }
70 |
71 | public function testFromNamesTitlecase(): void
72 | {
73 | foreach ($this->cases as $case) {
74 | /**
75 | * @var string $case->name
76 | */
77 | $name = ucfirst(strtolower($case->name));
78 | $method = Method::fromName($name);
79 | Assert::assertThat($method, Assert::identicalTo($case));
80 | }
81 | }
82 |
83 | public function testFromNamesInvertedTitlecase(): void
84 | {
85 | foreach ($this->cases as $case) {
86 | /**
87 | * @var string $case->name
88 | */
89 | $name = lcfirst(strtoupper($case->name));
90 | $method = Method::fromName($name);
91 | Assert::assertThat($method, Assert::identicalTo($case));
92 | }
93 | }
94 |
95 | public function testTryFromNamesUppercase(): void
96 | {
97 | foreach ($this->cases as $case) {
98 | /**
99 | * @var string $case->name
100 | */
101 | $name = strtoupper($case->name);
102 | $method = Method::tryFromName($name);
103 | Assert::assertThat($method, Assert::identicalTo($case));
104 | }
105 | }
106 |
107 | public function testTryFromNamesLowercase(): void
108 | {
109 | foreach ($this->cases as $case) {
110 | /**
111 | * @var string $case->name
112 | */
113 | $name = strtolower($case->name);
114 | $method = Method::tryFromName($name);
115 | Assert::assertThat($method, Assert::identicalTo($case));
116 | }
117 | }
118 |
119 | public function testTryFromNamesTitlecase(): void
120 | {
121 | foreach ($this->cases as $case) {
122 | /**
123 | * @var string $case->name
124 | */
125 | $name = ucfirst(strtolower($case->name));
126 | $method = Method::tryFromName($name);
127 | Assert::assertThat($method, Assert::identicalTo($case));
128 | }
129 | }
130 |
131 | public function testTryFromNamesInvertedTitlecase(): void
132 | {
133 | foreach ($this->cases as $case) {
134 | /**
135 | * @var string $case->name
136 | */
137 | $name = lcfirst(strtoupper($case->name));
138 | $method = Method::tryFromName($name);
139 | Assert::assertThat($method, Assert::identicalTo($case));
140 | }
141 | }
142 |
143 | public function testNullFromName(): void
144 | {
145 | $method = Method::tryFromName(null);
146 | Assert::assertThat($method, Assert::isNull());
147 | }
148 |
149 | public function testInvalidFromName(): void
150 | {
151 | $invalidName = 'INVALID';
152 | $method = Method::tryFromName($invalidName);
153 | Assert::assertThat($method, Assert::isNull());
154 | $this->expectException(ValueError::class);
155 | Method::fromName($invalidName);
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/tests/ReasonPhraseNamedTest.php:
--------------------------------------------------------------------------------
1 | cases = ReasonPhrase::cases();
35 | }
36 |
37 | public function testTryFromNames(): void
38 | {
39 | foreach ($this->cases as $case) {
40 | /**
41 | * @var string $case->name
42 | */
43 | $name = $case->name;
44 | $reasonPhrase = ReasonPhrase::tryFromName(name: $name);
45 | Assert::assertThat($reasonPhrase, Assert::identicalTo($case));
46 | }
47 | }
48 |
49 | public function testFromNames(): void
50 | {
51 | foreach ($this->cases as $case) {
52 | /**
53 | * @var string $case->name
54 | */
55 | $name = $case->name;
56 | $reasonPhrase = ReasonPhrase::fromName(name: $name);
57 | Assert::assertThat($reasonPhrase, Assert::identicalTo($case));
58 | }
59 | }
60 |
61 | public function testTryFromInteger(): void
62 | {
63 | foreach ($this->cases as $case) {
64 | $statusCode = StatusCode::fromReasonPhrase($case);
65 | /**
66 | * @var int $statusCode->value
67 | */
68 | $integer = $statusCode->value;
69 | $reasonPhrase = ReasonPhrase::tryFromInteger(integer: $integer);
70 | Assert::assertThat($reasonPhrase, Assert::identicalTo($case));
71 | }
72 | }
73 |
74 | public function testFromInteger(): void
75 | {
76 | foreach ($this->cases as $case) {
77 | $statusCode = StatusCode::fromReasonPhrase($case);
78 | /**
79 | * @var int $statusCode->value
80 | */
81 | $integer = $statusCode->value;
82 | $reasonPhrase = ReasonPhrase::fromInteger(integer: $integer);
83 | Assert::assertThat($reasonPhrase, Assert::identicalTo($case));
84 | }
85 | }
86 |
87 | public function testStatusCodeConversion(): void
88 | {
89 | foreach ($this->cases as $case) {
90 | /**
91 | * @var string $case->name
92 | */
93 | $name = $case->name;
94 | $statusCode = StatusCode::fromName(name: $name);
95 | $reasonPhrase = ReasonPhrase::fromStatusCode(statusCode: $statusCode);
96 | Assert::assertThat($reasonPhrase, Assert::identicalTo($case));
97 | }
98 | }
99 |
100 | public function testNullFromName(): void
101 | {
102 | $reasonPhrase = ReasonPhrase::tryFromName(name: null);
103 | Assert::assertThat($reasonPhrase, Assert::isNull());
104 | }
105 |
106 | public function testNullFromInteger(): void
107 | {
108 | $reasonPhrase = ReasonPhrase::tryFromInteger(integer: null);
109 | Assert::assertThat($reasonPhrase, Assert::isNull());
110 | }
111 |
112 | public function testInvalidFromName(): void
113 | {
114 | $invalidName = 'INVALID';
115 | $reasonPhrase = ReasonPhrase::tryFromName(name: $invalidName);
116 | Assert::assertThat($reasonPhrase, Assert::isNull());
117 | $this->expectException(ValueError::class);
118 | ReasonPhrase::fromName(name: $invalidName);
119 | }
120 |
121 | public function testInvalidFromInteger(): void
122 | {
123 | $invalidValue = -1;
124 | $reasonPhrase = ReasonPhrase::tryFromInteger(integer: $invalidValue);
125 | Assert::assertThat($reasonPhrase, Assert::isNull());
126 | $this->expectException(ValueError::class);
127 | ReasonPhrase::fromInteger(integer: $invalidValue);
128 | }
129 |
130 | public function testCode306Name(): void
131 | {
132 | $name = 'HTTP_306';
133 | $reasonPhrase = ReasonPhrase::tryFromName(name: $name);
134 | Assert::assertThat($reasonPhrase, Assert::isNull());
135 | $this->expectException(ValueError::class);
136 | ReasonPhrase::fromName(name: $name);
137 | }
138 |
139 | public function testCode306Integer(): void
140 | {
141 | $int306 = 306;
142 | $reasonPhrase = ReasonPhrase::tryFromInteger(integer: $int306);
143 | Assert::assertThat($reasonPhrase, Assert::isNull());
144 | $this->expectException(ValueError::class);
145 | ReasonPhrase::fromInteger(integer: $int306);
146 | }
147 | }
148 |
--------------------------------------------------------------------------------
/tests/ReasonPhrasePositionalTest.php:
--------------------------------------------------------------------------------
1 | cases = ReasonPhrase::cases();
35 | }
36 |
37 | public function testTryFromNames(): void
38 | {
39 | foreach ($this->cases as $case) {
40 | /**
41 | * @var string $case->name
42 | */
43 | $name = $case->name;
44 | $reasonPhrase = ReasonPhrase::tryFromName($name);
45 | Assert::assertThat($reasonPhrase, Assert::identicalTo($case));
46 | }
47 | }
48 |
49 | public function testFromNames(): void
50 | {
51 | foreach ($this->cases as $case) {
52 | /**
53 | * @var string $case->name
54 | */
55 | $name = $case->name;
56 | $reasonPhrase = ReasonPhrase::fromName($name);
57 | Assert::assertThat($reasonPhrase, Assert::identicalTo($case));
58 | }
59 | }
60 |
61 | public function testTryFromInteger(): void
62 | {
63 | foreach ($this->cases as $case) {
64 | $statusCode = StatusCode::fromReasonPhrase($case);
65 | /**
66 | * @var int $statusCode->value
67 | */
68 | $integer = $statusCode->value;
69 | $reasonPhrase = ReasonPhrase::tryFromInteger($integer);
70 | Assert::assertThat($reasonPhrase, Assert::identicalTo($case));
71 | }
72 | }
73 |
74 | public function testFromInteger(): void
75 | {
76 | foreach ($this->cases as $case) {
77 | $statusCode = StatusCode::fromReasonPhrase($case);
78 | /**
79 | * @var int $statusCode->value
80 | */
81 | $integer = $statusCode->value;
82 | $reasonPhrase = ReasonPhrase::fromInteger($integer);
83 | Assert::assertThat($reasonPhrase, Assert::identicalTo($case));
84 | }
85 | }
86 |
87 | public function testStatusCodeConversion(): void
88 | {
89 | foreach ($this->cases as $case) {
90 | /**
91 | * @var string $case->name
92 | */
93 | $name = $case->name;
94 | $statusCode = StatusCode::fromName($name);
95 | $reasonPhrase = ReasonPhrase::fromStatusCode($statusCode);
96 | Assert::assertThat($reasonPhrase, Assert::identicalTo($case));
97 | }
98 | }
99 |
100 | public function testNullFromName(): void
101 | {
102 | $reasonPhrase = ReasonPhrase::tryFromName(null);
103 | Assert::assertThat($reasonPhrase, Assert::isNull());
104 | }
105 |
106 | public function testNullFromInteger(): void
107 | {
108 | $reasonPhrase = ReasonPhrase::tryFromInteger(null);
109 | Assert::assertThat($reasonPhrase, Assert::isNull());
110 | }
111 |
112 | public function testInvalidFromName(): void
113 | {
114 | $invalidName = 'INVALID';
115 | $reasonPhrase = ReasonPhrase::tryFromName($invalidName);
116 | Assert::assertThat($reasonPhrase, Assert::isNull());
117 | $this->expectException(ValueError::class);
118 | ReasonPhrase::fromName($invalidName);
119 | }
120 |
121 | public function testInvalidFromInteger(): void
122 | {
123 | $invalidValue = -1;
124 | $reasonPhrase = ReasonPhrase::tryFromInteger($invalidValue);
125 | Assert::assertThat($reasonPhrase, Assert::isNull());
126 | $this->expectException(ValueError::class);
127 | ReasonPhrase::fromInteger($invalidValue);
128 | }
129 |
130 | public function testCode306Name(): void
131 | {
132 | $name = 'HTTP_306';
133 | $reasonPhrase = ReasonPhrase::tryFromName($name);
134 | Assert::assertThat($reasonPhrase, Assert::isNull());
135 | $this->expectException(ValueError::class);
136 | ReasonPhrase::fromName($name);
137 | }
138 |
139 | public function testCode306Integer(): void
140 | {
141 | $int306 = 306;
142 | $reasonPhrase = ReasonPhrase::tryFromInteger($int306);
143 | Assert::assertThat($reasonPhrase, Assert::isNull());
144 | $this->expectException(ValueError::class);
145 | ReasonPhrase::fromInteger($int306);
146 | }
147 | }
148 |
--------------------------------------------------------------------------------
/tests/StatusCodeNamedTest.php:
--------------------------------------------------------------------------------
1 | cases = StatusCode::cases();
35 | }
36 |
37 | public function testTryFromNames(): void
38 | {
39 | foreach ($this->cases as $case) {
40 | /**
41 | * @var string $case->name
42 | */
43 | $name = $case->name;
44 | $statusCode = StatusCode::tryFromName(name: $name);
45 | Assert::assertThat($statusCode, Assert::identicalTo($case));
46 | }
47 | }
48 |
49 | public function testFromNames(): void
50 | {
51 | foreach ($this->cases as $case) {
52 | /**
53 | * @var string $case->name
54 | */
55 | $name = $case->name;
56 | $statusCode = StatusCode::fromName(name: $name);
57 | Assert::assertThat($statusCode, Assert::identicalTo($case));
58 | }
59 | }
60 |
61 | public function testValues(): void
62 | {
63 | foreach ($this->cases as $case) {
64 | /**
65 | * @var int $case->value
66 | */
67 | $value = $case->value;
68 | $name = "HTTP_{$value}";
69 | $statusCode = StatusCode::FromName(name: $name);
70 | Assert::assertThat($statusCode, Assert::identicalTo($case));
71 | }
72 | }
73 |
74 | public function testTryFromInteger(): void
75 | {
76 | foreach ($this->cases as $case) {
77 | /**
78 | * @var int $case->value
79 | */
80 | $value = $case->value;
81 | $statusCode = StatusCode::tryFromInteger(integer: $value);
82 | Assert::assertThat($statusCode, Assert::identicalTo($case));
83 | }
84 | }
85 |
86 | public function testFromInteger(): void
87 | {
88 | foreach ($this->cases as $case) {
89 | /**
90 | * @var int $case->value
91 | */
92 | $value = $case->value;
93 | $statusCode = StatusCode::fromInteger(integer: $value);
94 | Assert::assertThat($statusCode, Assert::identicalTo($case));
95 | }
96 | }
97 |
98 | public function testReasonPhraseConversion(): void
99 | {
100 | foreach ($this->cases as $case) {
101 | /**
102 | * @var string $case->name
103 | */
104 | $name = $case->name;
105 | $reasonPhrase = ReasonPhrase::fromName(name: $name);
106 | $statusCode = StatusCode::fromReasonPhrase(reasonPhrase: $reasonPhrase);
107 | Assert::assertThat($statusCode, Assert::identicalTo($case));
108 | }
109 | }
110 |
111 | public function testNullFromName(): void
112 | {
113 | $null = null;
114 | $statusCode = StatusCode::tryFromName(name: $null);
115 | Assert::assertThat($statusCode, Assert::isNull());
116 | }
117 |
118 | public function testNullFromInteger(): void
119 | {
120 | $null = null;
121 | $statusCode = StatusCode::tryFromInteger(integer: $null);
122 | Assert::assertThat($statusCode, Assert::isNull());
123 | }
124 |
125 | public function testInvalidFromName(): void
126 | {
127 | $invalidName = 'INVALID';
128 | $statusCode = StatusCode::tryFromName(name: $invalidName);
129 | Assert::assertThat($statusCode, Assert::isNull());
130 | $this->expectException(ValueError::class);
131 | StatusCode::fromName($invalidName);
132 | }
133 |
134 | public function testInvalidFromInteger(): void
135 | {
136 | $invalidValue = -1;
137 | $statusCode = StatusCode::tryFromInteger(integer: $invalidValue);
138 | Assert::assertThat($statusCode, Assert::isNull());
139 | $this->expectException(ValueError::class);
140 | StatusCode::fromInteger($invalidValue);
141 | }
142 |
143 | public function testCode306Name(): void
144 | {
145 | $name = 'HTTP_306';
146 | $statusCode = StatusCode::tryFromName(name: $name);
147 | Assert::assertThat($statusCode, Assert::isNull());
148 | $this->expectException(ValueError::class);
149 | StatusCode::fromName($name);
150 | }
151 |
152 | public function testCode306Integer(): void
153 | {
154 | $int306 = 306;
155 | $statusCode = StatusCode::tryFromInteger(integer: $int306);
156 | Assert::assertThat($statusCode, Assert::isNull());
157 | $this->expectException(ValueError::class);
158 | StatusCode::fromInteger($int306);
159 | }
160 | }
161 |
--------------------------------------------------------------------------------
/tests/StatusCodePositionalTest.php:
--------------------------------------------------------------------------------
1 | cases = StatusCode::cases();
35 | }
36 |
37 | public function testTryFromNames(): void
38 | {
39 | foreach ($this->cases as $case) {
40 | /**
41 | * @var string $case->name
42 | */
43 | $name = $case->name;
44 | $statusCode = StatusCode::tryFromName($name);
45 | Assert::assertThat($statusCode, Assert::identicalTo($case));
46 | }
47 | }
48 |
49 | public function testFromNames(): void
50 | {
51 | foreach ($this->cases as $case) {
52 | /**
53 | * @var string $case->name
54 | */
55 | $name = $case->name;
56 | $statusCode = StatusCode::fromName($name);
57 | Assert::assertThat($statusCode, Assert::identicalTo($case));
58 | }
59 | }
60 |
61 | public function testValues(): void
62 | {
63 | foreach ($this->cases as $case) {
64 | /**
65 | * @var int $case->value
66 | */
67 | $value = $case->value;
68 | $name = "HTTP_{$value}";
69 | $statusCode = StatusCode::FromName($name);
70 | Assert::assertThat($statusCode, Assert::identicalTo($case));
71 | }
72 | }
73 |
74 | public function testTryFromInteger(): void
75 | {
76 | foreach ($this->cases as $case) {
77 | /**
78 | * @var int $case->value
79 | */
80 | $value = $case->value;
81 | $statusCode = StatusCode::tryFromInteger($value);
82 | Assert::assertThat($statusCode, Assert::identicalTo($case));
83 | }
84 | }
85 |
86 | public function testFromInteger(): void
87 | {
88 | foreach ($this->cases as $case) {
89 | /**
90 | * @var int $case->value
91 | */
92 | $value = $case->value;
93 | $statusCode = StatusCode::fromInteger($value);
94 | Assert::assertThat($statusCode, Assert::identicalTo($case));
95 | }
96 | }
97 |
98 | public function testReasonPhraseConversion(): void
99 | {
100 | foreach ($this->cases as $case) {
101 | /**
102 | * @var string $case->name
103 | */
104 | $name = $case->name;
105 | $reasonPhrase = ReasonPhrase::fromName($name);
106 | $statusCode = StatusCode::fromReasonPhrase($reasonPhrase);
107 | Assert::assertThat($statusCode, Assert::identicalTo($case));
108 | }
109 | }
110 |
111 | public function testNullFromName(): void
112 | {
113 | $null = null;
114 | $statusCode = StatusCode::tryFromName($null);
115 | Assert::assertThat($statusCode, Assert::isNull());
116 | }
117 |
118 | public function testNullFromInteger(): void
119 | {
120 | $null = null;
121 | $statusCode = StatusCode::tryFromInteger($null);
122 | Assert::assertThat($statusCode, Assert::isNull());
123 | }
124 |
125 | public function testInvalidFromName(): void
126 | {
127 | $invalidName = 'INVALID';
128 | $statusCode = StatusCode::tryFromName($invalidName);
129 | Assert::assertThat($statusCode, Assert::isNull());
130 | $this->expectException(ValueError::class);
131 | StatusCode::fromName($invalidName);
132 | }
133 |
134 | public function testInvalidFromInteger(): void
135 | {
136 | $invalidValue = -1;
137 | $statusCode = StatusCode::tryFromInteger($invalidValue);
138 | Assert::assertThat($statusCode, Assert::isNull());
139 | $this->expectException(ValueError::class);
140 | StatusCode::fromInteger($invalidValue);
141 | }
142 |
143 | public function testCode306Name(): void
144 | {
145 | $name = 'HTTP_306';
146 | $statusCode = StatusCode::tryFromName($name);
147 | Assert::assertThat($statusCode, Assert::isNull());
148 | $this->expectException(ValueError::class);
149 | StatusCode::fromName($name);
150 | }
151 |
152 | public function testCode306Integer(): void
153 | {
154 | $int306 = 306;
155 | $statusCode = StatusCode::tryFromInteger($int306);
156 | Assert::assertThat($statusCode, Assert::isNull());
157 | $this->expectException(ValueError::class);
158 | StatusCode::fromInteger($int306);
159 | }
160 | }
161 |
--------------------------------------------------------------------------------