AbstractStringBasedType::testSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Api\Scalar;
6
7
use GraphQL\Error\Error;
8
use GraphQL\Language\AST\IntValueNode;
9
use GraphQL\Language\AST\StringValueNode;
10
use PHPUnit\Framework\TestCase;
11
12
abstract class AbstractStringBasedType extends TestCase
13
{
14
    abstract public function createType(): \Ecodev\Felix\Api\Scalar\AbstractStringBasedType;
15
16
    abstract public function getTypeName(): string;
17
18
    /**
19
     * @dataProvider providerValues
20
     */
21
    public function testSerialize(?string $input, ?string $expected): void
22
    {
23
        $type = $this->createType();
24
        $actual = $type->serialize($input);
25
        self::assertSame($expected, $actual);
26
    }
27
28
    /**
29
     * @dataProvider providerValues
30
     */
31
    public function testParseValue(?string $input, ?string $expected, bool $isValid): void
32
    {
33
        $type = $this->createType();
34
35
        if (!$isValid) {
36
            $this->expectException(Error::class);
37
            $this->expectExceptionMessage('Query error: Not a valid ' . $this->getTypeName());
38
        }
39
40
        $actual = $type->parseValue($input);
41
42
        self::assertSame($expected, $actual);
43
    }
44
45
    /**
46
     * @dataProvider providerValues
47
     */
48
    public function testParseLiteral(string $input, ?string $expected, bool $isValid): void
49
    {
50
        $type = $this->createType();
51
        $ast = new StringValueNode(['value' => $input]);
52
53
        if (!$isValid) {
54
            $this->expectException(Error::class);
55
            $this->expectExceptionMessage('Query error: Not a valid ' . $this->getTypeName());
56
        }
57
58
        $actual = $type->parseLiteral($ast);
59
60
        self::assertSame($expected, $actual);
61
    }
62
63
    abstract public static function providerValues(): iterable;
64
65
    public function testParseInvalidNodeWillThrow(): void
66
    {
67
        $type = $this->createType();
68
        $ast = new IntValueNode(['value' => '123']);
69
70
        $this->expectException(Error::class);
71
        $this->expectExceptionMessage('Query error: Can only parse strings got: IntValue');
72
        $type->parseLiteral($ast);
73
    }
74
}
75