Passed
Push — master ( 73bfc2...a878a7 )
by Adrien
13:45 queued 10:48
created

testConvertToDatabaseValueThrowsWithZero()

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
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\SetType;
10
use InvalidArgumentException;
11
use PHPUnit\Framework\TestCase;
12
use ReflectionClass;
13
14
final class SetTypeTest extends TestCase
15
{
16
    private SetType $type;
17
18
    private AbstractPlatform $platform;
19
20
    protected function setUp(): void
21
    {
22
        $this->type = new class() extends SetType {
23
            protected function getPossibleValues(): array
24
            {
25
                return ['value1', 'value2'];
26
            }
27
        };
28
29
        $this->platform = new MySQLPlatform();
30
    }
31
32
    public function testSet(): void
33
    {
34
        self::assertSame("SET('value1', 'value2')", $this->type->getSqlDeclaration(['foo'], $this->platform));
35
36
        // Should always return string
37
        self::assertSame(['value1', 'value2'], $this->type->convertToPHPValue('value1,value2', $this->platform));
38
39
        // Should support null values or empty string
40
        self::assertNull($this->type->convertToPHPValue(null, $this->platform));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->type->convertToPH...(null, $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...
41
        self::assertSame([], $this->type->convertToPHPValue('', $this->platform));
42
        self::assertNull($this->type->convertToDatabaseValue(null, $this->platform));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->type->convertToDa...(null, $this->platform) targeting Ecodev\Felix\DBAL\Types\...onvertToDatabaseValue() 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...
43
        self::assertSame('', $this->type->convertToDatabaseValue([], $this->platform));
44
45
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
46
    }
47
48
    public function testConvertToPHPValueThrowsWithInvalidValue(): void
49
    {
50
        $this->expectException(InvalidArgumentException::class);
51
52
        $this->type->convertToPHPValue('foo', $this->platform);
53
    }
54
55
    public function testConvertToDatabaseValueThrowsWithInvalidValue(): void
56
    {
57
        $this->expectException(InvalidArgumentException::class);
58
59
        $this->type->convertToDatabaseValue(['foo'], $this->platform);
60
    }
61
62
    public function testConvertToDatabaseValueThrowsWithInvalidType(): void
63
    {
64
        $this->expectException(InvalidArgumentException::class);
65
66
        $this->type->convertToDatabaseValue('foo', $this->platform);
67
    }
68
69
    public function testConvertToPHPValueThrowsWithZero(): void
70
    {
71
        $this->expectException(InvalidArgumentException::class);
72
73
        $this->type->convertToPHPValue(0, $this->platform);
74
    }
75
76
    public function testConvertToDatabaseValueThrowsWithZero(): void
77
    {
78
        $this->expectException(InvalidArgumentException::class);
79
80
        $this->type->convertToDatabaseValue(0, $this->platform);
81
    }
82
83
    public function testNameDependsOnValues(): void
84
    {
85
        $class = new ReflectionClass($this->type);
86
        $shortClassName = $class->getShortName();
87
88
        self::assertSame($shortClassName, $this->type->getName());
89
    }
90
}
91