Completed
Pull Request — develop (#3179)
by Sergei
63:28
created

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