Passed
Push — master ( ecad72...d36979 )
by Eric
02:10
created

StatusCodesTest::testGetDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Utility - Collection of various PHP utility functions.
7
 *
8
 * @author    Eric Sizemore <[email protected]>
9
 * @version   2.0.0
10
 * @copyright (C) 2017 - 2024 Eric Sizemore
11
 * @license   The MIT License (MIT)
12
 *
13
 * Copyright (C) 2017 - 2024 Eric Sizemore <https://www.secondversion.com>.
14
 *
15
 * Permission is hereby granted, free of charge, to any person obtaining a copy
16
 * of this software and associated documentation files (the "Software"), to
17
 * deal in the Software without restriction, including without limitation the
18
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
19
 * sell copies of the Software, and to permit persons to whom the Software is
20
 * furnished to do so, subject to the following conditions:
21
 *
22
 * The above copyright notice and this permission notice shall be included in
23
 * all copies or substantial portions of the Software.
24
 *
25
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31
 * THE SOFTWARE.
32
 */
33
34
namespace Esi\Utility\Tests\Enums\Http;
35
36
use Esi\Utility\Enums\Http\StatusCodes;
37
use PHPUnit\Framework\TestCase;
38
use PHPUnit\Framework\Attributes\CoversClass;
39
40
/**
41
 * StatusCodes enum tests.
42
 *
43
 * @todo The tests need some work, more detailed testing.
44
 */
45
#[CoversClass(StatusCodes::class)]
46
class StatusCodesTest extends TestCase
47
{
48
    /**
49
     * Holds all cases of the StatusCodes enum.
50
     *
51
     * @var array<int, StatusCodes>
52
     */
53
    protected array $codes;
54
55
    /**
56
     * Build the $codes array.
57
     */
58
    #[\Override]
59
    public function setUp(): void
60
    {
61
        $this->codes = StatusCodes::cases();
62
    }
63
64
    /**
65
     * Simple, perhaps unnecessary, test to make sure no empty values and all returned values are type int.
66
     */
67
    public function testNoneEmptyIsInt(): void
68
    {
69
        self::assertNotEmpty($this->codes);
70
71
        foreach ($this->codes as $code) {
72
            self::assertNotEmpty($code->getValue());
73
            self::assertIsInt($code->getValue());
74
        }
75
    }
76
77
    /**
78
     * Simple, perhaps unnecessary, test to make sure all returned values are within range of 100, 511.
79
     */
80
    public function testWithinRange(): void
81
    {
82
        self::assertNotEmpty($this->codes);
83
84
        foreach ($this->codes as $code) {
85
            self::assertGreaterThanOrEqual(100, $code->getValue());
86
            self::assertLessThanOrEqual(511, $code->getValue());
87
        }
88
    }
89
90
    /**
91
     * Test the getMessage() method.
92
     */
93
    public function testGetMessage(): void
94
    {
95
        self::assertNotEmpty($this->codes);
96
97
        foreach ($this->codes as $code) {
98
            self::assertNotEmpty($code->getMessage());
99
            self::assertIsString($code->getMessage());
100
            self::assertSame(match ($code->getName()) {
101
                'Non_Authoritative_Information' => 'Non-authoritative Information',
102
                'Multi_Status' => 'Multi-Status',
103
                'Im_A_Teapot' => "I'm A Teapot",
104
                'Request_URI_Too_Long' => 'Request-URI Too Long',
105
                default => str_replace('_', ' ', $code->getName()),
106
            }, $code->getMessage());
107
        }
108
    }
109
110
    /**
111
     * Test the getCategory() method.
112
     */
113
    public function testGetCategory(): void
114
    {
115
        self::assertNotEmpty($this->codes);
116
117
        foreach ($this->codes as $code) {
118
            self::assertSame(str_replace(' ', '', $code->getCategory()->getValue()), $code->getCategory()->getName());
119
        }
120
    }
121
122
    /**
123
     * Test the getDescription() method.
124
     */
125
    public function testGetDescription(): void
126
    {
127
        self::assertNotEmpty($this->codes);
128
129
        foreach ($this->codes as $code) {
130
            self::assertSame($code->getDescription()->getName(), $code->getName());
131
        }
132
    }
133
}
134