File: PutCommandTest.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 (95 lines) | stat: -rw-r--r-- 3,115 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
<?php

declare(strict_types=1);

namespace Pheanstalk\Tests\Unit\Command;

use Pheanstalk\Command\PutCommand;
use Pheanstalk\Exception\ExpectedCrlfException;
use Pheanstalk\Exception\JobBuriedException;
use Pheanstalk\Exception\JobTooBigException;
use Pheanstalk\Exception\ServerDrainingException;
use Pheanstalk\Values\RawResponse;
use Pheanstalk\Values\ResponseType;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;

#[CoversClass(PutCommand::class)]
final class PutCommandTest extends CommandTestBase
{
    public function testInterpretBuried(): void
    {
        $id = "123123";
        $this->expectException(JobBuriedException::class);
        try {
            $this->getSubject()->interpret(new RawResponse(ResponseType::Buried, "123123"));
        } catch (JobBuriedException $e) {
            Assert::assertSame($id, $e->jobId->getId());
            throw $e;
        }
    }

    public function testInterpretExpectedCrlf(): void
    {
        $this->expectException(ExpectedCrlfException::class);
        $this->getSubject()->interpret(new RawResponse(ResponseType::ExpectedCrlf));
    }
    public function testInterpretDraining(): void
    {
        $this->expectException(ServerDrainingException::class);
        $this->getSubject()->interpret(new RawResponse(ResponseType::Draining));
    }

    public function testInterpretJobTooBig(): void
    {
        $this->expectException(JobTooBigException::class);
        $this->getSubject()->interpret(new RawResponse(ResponseType::JobTooBig));
    }

    public function testInterpretInserted(): void
    {
        $id = "15997";
        Assert::assertSame($id, $this->getSubject()->interpret(new RawResponse(ResponseType::Inserted, $id))->getId());
    }

    protected static function getSupportedResponses(): array
    {
        return [ResponseType::Buried, ResponseType::ExpectedCrlf, ResponseType::Draining, ResponseType::JobTooBig, ResponseType::Inserted];
    }

    protected function getSubject(): PutCommand
    {
        return new PutCommand("data", 5, 4, 3);
    }


    /**
     * @phpstan-return iterable<array{0: string, 1: int}>
     */
    public static function dataProvider(): iterable
    {
        yield ["ϸϹϻ", 6];
        /**
         * Test some invalid UTF-8 sequences
         * @see https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
         */
        yield [(string) hex2bin("fe"), 1];
        yield [(string) hex2bin("ff"), 1];
        yield [(string) hex2bin("fefeffff"), 4];
        yield [(string) hex2bin("c0af"), 2];
        yield [(string) hex2bin("eda080"), 3];
        yield [(string) hex2bin("eda080edb080"), 6];
        for ($i = 1; $i < 10; $i++) {
            yield [random_bytes(32), 32];
        }
    }

    #[DataProvider('dataProvider')]
    public function testData(string $data, int $expectedLength): void
    {
        $command = new PutCommand($data, 5, 5, 4);
        Assert::assertSame("put 5 5 4 {$expectedLength}", $command->getCommandLine());
        Assert::assertSame($data, $command->getData());
    }
}