File: TimeoutTest.php

package info (click to toggle)
php-pda-pheanstalk 8.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 836 kB
  • sloc: php: 4,713; xml: 19; makefile: 14
file content (108 lines) | stat: -rw-r--r-- 2,893 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php

declare(strict_types=1);

namespace Pheanstalk\Tests\Unit\Values;

use InvalidArgumentException;
use Pheanstalk\Values\Timeout;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

#[CoversClass(Timeout::class)]
final class TimeoutTest extends TestCase
{
    /**
     * @return list<array{0: int, 1: int}|array{0: int}>
     */
    public static function validConstructorArguments(): array
    {
        return [
            [0, ],
            [0, 0],
            [0, 200],
            [100, 0],
            [100, ],
            [100, 200],
        ];
    }

    /**
     * @return list<array{0: int, 1: int}|array{0: int}>
     */
    public static function invalidConstructorArguments(): array
    {
        return [
            [0, -200],
            [-100, ],
            [-100, 0],
            [-100, -200],
        ];
    }

    #[DataProvider('validConstructorArguments')]
    public function testConstructorWitValidArgumentsCreatesInstance(int $seconds, int $microSeconds = 0): void
    {
        $timeout = new Timeout($seconds, $microSeconds);

        self::assertSame($seconds, $timeout->seconds);
        self::assertSame($microSeconds, $timeout->microSeconds);
    }

    #[DataProvider('invalidConstructorArguments')]
    public function testConstructorWitInvalidArgumentsThrowsException(int $seconds, int $microSeconds = 0): void
    {
        $this->expectException(InvalidArgumentException::class);
        new Timeout($seconds, $microSeconds);
    }

    public function testToFloat(): void
    {
        self::assertSame(1.5, (new Timeout(1, 500000))->toFloat());
    }

    public function testToArray(): void
    {
        self::assertSame([
            'sec' => 1,
            'usec' => 500000
        ], (new Timeout(1, 500000))->toArray());
    }

    public function testAddNullReturnsSameValue(): void
    {
        $timeout = (new Timeout(1, 500000))->add();
        self::assertSame([
            'sec' => 1,
            'usec' => 500000
        ], $timeout->toArray());
    }

    public function testAddWithNoMsReturnsExpectedValue(): void
    {
        $timeout = (new Timeout(1, 500000))->add(new Timeout(2));
        self::assertSame([
            'sec' => 3,
            'usec' => 500000
        ], $timeout->toArray());
    }

    public function testAddWithNotOverflowingMsReturnsExpectedValue(): void
    {
        $timeout = (new Timeout(1, 500000))->add(new Timeout(2, 200000));
        self::assertSame([
            'sec' => 3,
            'usec' => 700000
        ], $timeout->toArray());
    }

    public function testAddWithOverflowingMsReturnsExpectedValue(): void
    {
        $timeout = (new Timeout(1, 500000))->add(new Timeout(2, 800000));
        self::assertSame([
            'sec' => 4,
            'usec' => 300000
        ], $timeout->toArray());
    }
}