UploadTypeTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 67
rs 10
c 2
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testCannotParseNonUploadedFileInstance() 0 7 1
A testCanNeverBeSerialized() 0 7 1
A providerUploadErrorWillThrow() 0 10 1
A testCanParseUploadedFileInstance() 0 6 1
A testUploadErrorWillThrow() 0 9 1
A testCanNeverParseLiteral() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLTests\Upload;
6
7
use Exception;
8
use GraphQL\Error\Error;
9
use GraphQL\Error\InvariantViolation;
10
use GraphQL\Language\AST\StringValueNode;
11
use GraphQL\Upload\UploadError;
12
use GraphQL\Upload\UploadType;
13
use GraphQL\Upload\Utility;
14
use GraphQLTests\Upload\Psr7\PsrUploadedFileStub;
15
use PHPUnit\Framework\TestCase;
16
use Throwable;
17
use UnexpectedValueException;
18
19
final class UploadTypeTest extends TestCase
20
{
21
    public function testCanParseUploadedFileInstance(): void
22
    {
23
        $type = new UploadType();
24
        $file = new PsrUploadedFileStub('image.jpg', 'image/jpeg');
25
        $actual = $type->parseValue($file);
26
        self::assertSame($file, $actual);
27
    }
28
29
    public function testCannotParseNonUploadedFileInstance(): void
30
    {
31
        $type = new UploadType();
32
        $this->expectException(UnexpectedValueException::class);
33
        $this->expectExceptionMessage('Could not get uploaded file, be sure to conform to GraphQL multipart request specification. Instead got: "foo"');
34
35
        $type->parseValue('foo');
36
    }
37
38
    public function testCanNeverBeSerialized(): void
39
    {
40
        $type = new UploadType();
41
        $this->expectException(InvariantViolation::class);
42
        $this->expectExceptionMessage('`Upload` cannot be serialized');
43
44
        $type->serialize('foo');
45
    }
46
47
    public function testCanNeverParseLiteral(): void
48
    {
49
        $type = new UploadType();
50
        $node = new StringValueNode(['value' => 'foo']);
51
52
        $this->expectException(Error::class);
53
        $this->expectExceptionMessage('`Upload` cannot be hardcoded in query, be sure to conform to GraphQL multipart request specification. Instead got: StringValue');
54
        $type->parseLiteral($node);
55
    }
56
57
    /**
58
     * @param class-string<Throwable> $e
59
     *
60
     * @dataProvider providerUploadErrorWillThrow
61
     */
62
    public function testUploadErrorWillThrow(int $errorStatus, string $expectedMessage, string $e = UploadError::class): void
63
    {
64
        $type = new UploadType();
65
        $file = new PsrUploadedFileStub('image.jpg', 'image/jpeg', $errorStatus);
66
67
        $this->expectException($e);
68
        $this->expectExceptionMessage($expectedMessage);
69
70
        $type->parseValue($file);
71
    }
72
73
    /**
74
     * @return iterable<array{0: int, 1: string, 2?: class-string<Throwable>}>
75
     */
76
    public static function providerUploadErrorWillThrow(): iterable
77
    {
78
        yield [UPLOAD_ERR_CANT_WRITE, 'Failed to write file to disk'];
79
        yield [UPLOAD_ERR_EXTENSION, 'A PHP extension stopped the upload'];
80
        yield [UPLOAD_ERR_FORM_SIZE, 'The file exceeds the `MAX_FILE_SIZE` directive that was specified in the HTML form'];
81
        yield [UPLOAD_ERR_INI_SIZE, 'The file exceeds the `upload_max_filesize` of ' . Utility::toMebibyte(Utility::getUploadMaxFilesize())];
82
        yield [UPLOAD_ERR_NO_FILE, 'No file was uploaded'];
83
        yield [UPLOAD_ERR_NO_TMP_DIR, 'Missing a temporary folder'];
84
        yield [UPLOAD_ERR_PARTIAL, 'The file was only partially uploaded'];
85
        yield [5, 'Unsupported UPLOAD_ERR_* constant value: 5', Exception::class];
86
    }
87
}
88