testConvertToPHPValueThrowsWithZero()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\EnumType;
10
use InvalidArgumentException;
11
use PHPUnit\Framework\TestCase;
12
13
final class EnumTypeTest extends TestCase
14
{
15
    private EnumType $type;
16
17
    private AbstractPlatform $platform;
18
19
    protected function setUp(): void
20
    {
21
        $this->type = new ExampleEnum();
22
        $this->platform = new MySQLPlatform();
23
    }
24
25
    public function testEnum(): void
26
    {
27
        self::assertSame("ENUM('value1', 'value2')", $this->type->getSqlDeclaration(['foo' => 'bar'], $this->platform));
28
29
        // Should always return string
30
        self::assertSame('value1', $this->type->convertToPHPValue('value1', $this->platform));
31
32
        // Should support null values or empty string
33
        self::assertNull($this->type->convertToPHPValue(null, $this->platform));
34
        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...
35
        self::assertNull($this->type->convertToDatabaseValue(null, $this->platform));
36
        self::assertNull($this->type->convertToDatabaseValue('', $this->platform));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->type->convertToDa...ue('', $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...
37
    }
38
39
    public function testConvertToPHPValueThrowsWithInvalidValue(): void
40
    {
41
        $this->expectException(InvalidArgumentException::class);
42
        $this->expectExceptionMessage("Invalid 'foo' value fetched from database for enum ExampleEnum");
43
44
        $this->type->convertToPHPValue('foo', $this->platform);
45
    }
46
47
    public function testConvertToDatabaseValueThrowsWithInvalidValue(): void
48
    {
49
        $this->expectException(InvalidArgumentException::class);
50
        $this->expectExceptionMessage("Invalid 'foo' value to be stored in database for enum ExampleEnum");
51
52
        $this->type->convertToDatabaseValue('foo', $this->platform);
53
    }
54
55
    public function testConvertToPHPValueThrowsWithZero(): void
56
    {
57
        $this->expectException(InvalidArgumentException::class);
58
        $this->expectExceptionMessage("Invalid '0' value fetched from database for enum ExampleEnum");
59
60
        $this->type->convertToPHPValue(0, $this->platform);
61
    }
62
63
    public function testConvertToDatabaseValueThrowsWithZero(): void
64
    {
65
        $this->expectException(InvalidArgumentException::class);
66
        $this->expectExceptionMessage("Invalid '0' value to be stored in database for enum ExampleEnum");
67
68
        $this->type->convertToDatabaseValue(0, $this->platform);
69
    }
70
}
71