Failed Conditions
Pull Request — develop (#3179)
by Sergei
130:20 queued 65:17
created

testThrowsConversionExceptionOnInvalidDatabaseValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Types;
4
5
use Doctrine\DBAL\ParameterType;
6
use Doctrine\DBAL\Types\Type;
7
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
8
use function base64_encode;
9
use function chr;
10
use function fopen;
11
12
class BinaryTest extends \Doctrine\Tests\DbalTestCase
13
{
14
    /**
15
     * @var \Doctrine\Tests\DBAL\Mocks\MockPlatform
16
     */
17
    protected $platform;
18
19
    /**
20
     * @var \Doctrine\DBAL\Types\BinaryType
21
     */
22
    protected $type;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function setUp()
28
    {
29
        $this->platform = new MockPlatform();
30
        $this->type     = Type::getType('binary');
31
    }
32
33
    public function testReturnsBindingType()
34
    {
35
        self::assertSame(ParameterType::BINARY, $this->type->getBindingType());
36
    }
37
38
    public function testReturnsName()
39
    {
40
        self::assertSame(Type::BINARY, $this->type->getName());
41
    }
42
43
    public function testReturnsSQLDeclaration()
44
    {
45
        self::assertSame('DUMMYBINARY', $this->type->getSQLDeclaration(array(), $this->platform));
46
    }
47
48
    public function testBinaryNullConvertsToPHPValue()
49
    {
50
        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 Doctrine\DBAL\Types\Bina...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...
51
    }
52
53
    public function testBinaryStringConvertsToPHPValue()
54
    {
55
        $databaseValue = $this->getBinaryString();
56
        $phpValue      = $this->type->convertToPHPValue($databaseValue, $this->platform);
57
58
        self::assertSame($databaseValue, $phpValue);
59
    }
60
61
    public function testBinaryResourceConvertsToPHPValue()
62
    {
63
        $databaseValue = fopen('data://text/plain;base64,' . base64_encode('binary string'), 'r');
64
        $phpValue      = $this->type->convertToPHPValue($databaseValue, $this->platform);
65
66
        self::assertSame('binary string', $phpValue);
67
    }
68
69
    /**
70
     * Creates a binary string containing all possible byte values.
71
     *
72
     * @return string
73
     */
74
    private function getBinaryString()
75
    {
76
        $string = '';
77
78
        for ($i = 0; $i < 256; $i++) {
79
            $string .= chr($i);
80
        }
81
82
        return $string;
83
    }
84
85
    /**
86
     * @dataProvider getInvalidDatabaseValues
87
     * @expectedException \Doctrine\DBAL\Types\ConversionException
88
     */
89
    public function testThrowsConversionExceptionOnInvalidDatabaseValue($value)
90
    {
91
        $this->type->convertToPHPValue($value, $this->platform);
92
    }
93
94
    public function getInvalidDatabaseValues()
95
    {
96
        return array(
97
            array(false),
98
            array(true),
99
            array(0),
100
            array(1),
101
            array(-1),
102
            array(0.0),
103
            array(1.1),
104
            array(-1.1),
105
        );
106
    }
107
}
108