Passed
Pull Request — master (#100)
by
unknown
23:41 queued 21:10
created

InvalidPhoneNumberTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
c 2
b 0
f 0
dl 0
loc 84
rs 10
wmc 8
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of the Numverify API Client for PHP.
7
 *
8
 * (c) 2024 Eric Sizemore <[email protected]>
9
 * (c) 2018-2021 Mark Rogoyski <[email protected]>
10
 *
11
 * This source file is subject to the MIT license. For the full copyright,
12
 * license information, and credits/acknowledgements, please view the LICENSE
13
 * and README files that were distributed with this source code.
14
 */
15
16
namespace Numverify\Tests\PhoneNumber;
17
18
use Iterator;
19
use Numverify\Exception\NumverifyApiResponseException;
20
use Numverify\PhoneNumber\InvalidPhoneNumber;
21
use PHPUnit\Framework\Attributes\CoversClass;
22
use PHPUnit\Framework\Attributes\DataProvider;
23
use PHPUnit\Framework\Attributes\TestDox;
24
use PHPUnit\Framework\TestCase;
25
use stdClass;
26
27
use function json_decode;
28
use function json_encode;
29
use function print_r;
30
31
/**
32
 * @internal
33
 */
34
#[CoversClass(InvalidPhoneNumber::class)]
35
class InvalidPhoneNumberTest extends TestCase
36
{
37
    private const NUMBER = '14158586273';
38
    private const VALID  = false;
39
40
    private stdClass $validatedPhoneNumberData;
41
42
    protected function setUp(): void
43
    {
44
        $this->validatedPhoneNumberData         = new stdClass();
45
        $this->validatedPhoneNumberData->valid  = self::VALID;
46
        $this->validatedPhoneNumberData->number = self::NUMBER;
47
    }
48
49
    #[TestDox('InvalidPhoneNumber sets __debugInfo for var_dump to return number data as array.')]
50
    public function testDebugInfo(): void
51
    {
52
        $invalidPhoneNumber = new InvalidPhoneNumber($this->validatedPhoneNumberData);
53
54
        $debugInfo = print_r($invalidPhoneNumber, true);
55
        self::assertStringContainsString('valid', $debugInfo);
56
        self::assertStringContainsString('number', $debugInfo);
57
    }
58
59
    #[TestDox('InvalidPhoneNumber getNumber returns number.')]
60
    public function testGetters(): void
61
    {
62
        $invalidPhoneNumber = new InvalidPhoneNumber($this->validatedPhoneNumberData);
63
64
        $number = $invalidPhoneNumber->getNumber();
65
        self::assertSame(self::NUMBER, $number);
66
    }
67
68
    #[TestDox('InvalidPhoneNumber returns false on isValid.')]
69
    public function testIsValid(): void
70
    {
71
        $invalidPhoneNumber = new InvalidPhoneNumber($this->validatedPhoneNumberData);
72
73
        $isValid = $invalidPhoneNumber->isValid();
74
        self::assertFalse($isValid);
75
    }
76
77
    #[TestDox('InvalidPhoneNumber uses JsonSerializable interface to return number data as array.')]
78
    public function testJsonSerialize(): void
79
    {
80
        // Given
81
        $invalidPhoneNumber = new InvalidPhoneNumber($this->validatedPhoneNumberData);
82
83
        /** @var non-empty-string $json */
84
        $json = json_encode($invalidPhoneNumber);
85
86
        /** @var stdClass $object */
87
        $object = json_decode($json);
88
        self::assertSame(self::VALID, $object->valid);
89
        self::assertSame(self::NUMBER, $object->number);
90
    }
91
92
    #[DataProvider('dataProviderForFields')]
93
    #[TestDox('InvalidPhoneNumber throws a NumverifyApiResponseException exception if missing data. Using field: $missingField')]
94
    public function testPhoneNumberDataValidation(string $missingField): void
95
    {
96
        unset($this->validatedPhoneNumberData->$missingField);
97
98
        $this->expectException(NumverifyApiResponseException::class);
99
        new InvalidPhoneNumber($this->validatedPhoneNumberData);
100
    }
101
102
    #[TestDox('InvalidPhoneNumber uses Stringable interface to return proper string representation.')]
103
    public function testToString(): void
104
    {
105
        $invalidPhoneNumber = new InvalidPhoneNumber($this->validatedPhoneNumberData);
106
107
        $stringRepresentation = (string) $invalidPhoneNumber;
108
        self::assertSame(self::NUMBER, $stringRepresentation);
109
    }
110
111
    /**
112
     * @psalm-suppress PossiblyUnusedMethod
113
     */
114
    public static function dataProviderForFields(): Iterator
115
    {
116
        yield ['valid'];
117
        yield ['number'];
118
    }
119
}
120