Failed Conditions
Push — master ( 0ef5da...6c8847 )
by Adrien
11:04 queued 07:56
created

PhpEnumTypeTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 22
c 1
b 0
f 0
dl 0
loc 66
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ testConvertToDatabaseValueThrowsWithInvalidValue() 0 5 1
testConvertToDatabaseValueThrowsWithInvalidEnum() 0 5 ?
testConvertToPHPValueThrowsWithInvalidValue() 0 5 ?
A hp$0 ➔ testConvertToDatabaseValueThrowsWithInvalidEnum() 0 5 1
A hp$0 ➔ getEnumType() 0 3 1
testConvertToDatabaseValueThrowsWithZero() 0 5 ?
A hp$0 ➔ testConvertToPHPValueThrowsWithZero() 0 5 1
testConvertToPHPValueThrowsWithZero() 0 5 ?
setUp() 0 10 ?
A hp$0 ➔ testConvertToDatabaseValueThrowsWithZero() 0 5 1
A hp$0 ➔ setUp() 0 10 1
testEnum() 0 13 ?
testConvertToDatabaseValueThrowsWithInvalidValue() 0 5 ?
A hp$0 ➔ testEnum() 0 13 1
A hp$0 ➔ testConvertToPHPValueThrowsWithInvalidValue() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\DBAL\Types;
6
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\DBAL\Platforms\MySQLPlatform;
9
use Ecodev\Felix\DBAL\Types\PhpEnumType;
10
use EcodevTests\Felix\Service\OtherTestEnum;
11
use EcodevTests\Felix\Service\TestEnum;
12
use InvalidArgumentException;
13
use PHPUnit\Framework\TestCase;
14
use ValueError;
15
16
class PhpEnumTypeTest extends TestCase
17
{
18
    private PhpEnumType $type;
19
20
    private AbstractPlatform $platform;
21
22
    protected function setUp(): void
23
    {
24
        $this->type = new class() extends PhpEnumType {
25
            protected function getEnumType(): string
26
            {
27
                return TestEnum::class;
28
            }
29
        };
30
31
        $this->platform = new MySQLPlatform();
32
    }
33
34
    public function testEnum(): void
35
    {
36
        self::assertSame("ENUM('value1', 'value2')", $this->type->getSqlDeclaration(['foo'], $this->platform));
37
38
        // Should always return string
39
        self::assertSame(TestEnum::key1, $this->type->convertToPHPValue('value1', $this->platform));
40
41
        // Should support null values or empty string
42
        self::assertNull($this->type->convertToPHPValue(null, $this->platform));
43
        self::assertNull($this->type->convertToPHPValue('', $this->platform));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->type->convertToPH...ue('', $this->platform) targeting Ecodev\Felix\DBAL\Types\...pe::convertToPHPValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
44
        self::assertNull($this->type->convertToDatabaseValue(null, $this->platform));
45
46
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
47
    }
48
49
    public function testConvertToPHPValueThrowsWithInvalidValue(): void
50
    {
51
        $this->expectException(ValueError::class);
52
53
        $this->type->convertToPHPValue('foo', $this->platform);
54
    }
55
56
    public function testConvertToDatabaseValueThrowsWithInvalidValue(): void
57
    {
58
        $this->expectException(InvalidArgumentException::class);
59
60
        $this->type->convertToDatabaseValue('foo', $this->platform);
61
    }
62
63
    public function testConvertToDatabaseValueThrowsWithInvalidEnum(): void
64
    {
65
        $this->expectException(InvalidArgumentException::class);
66
67
        $this->type->convertToDatabaseValue(OtherTestEnum::key1, $this->platform);
68
    }
69
70
    public function testConvertToPHPValueThrowsWithZero(): void
71
    {
72
        $this->expectException(InvalidArgumentException::class);
73
74
        $this->type->convertToPHPValue(0, $this->platform);
75
    }
76
77
    public function testConvertToDatabaseValueThrowsWithZero(): void
78
    {
79
        $this->expectException(InvalidArgumentException::class);
80
81
        $this->type->convertToDatabaseValue(0, $this->platform);
82
    }
83
}
84