Failed Conditions
Push — master ( 2ade86...13f838 )
by Jonathan
18s
created

Tests/ORM/Functional/Ticket/DDC2224Test.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5
use Doctrine\Common\Cache\ArrayCache;
6
use Doctrine\DBAL\Types\Type;
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\ORM\Query;
9
10
/**
11
 * @group DDC-2224
12
 */
13
class DDC2224Test extends \Doctrine\Tests\OrmFunctionalTestCase
14
{
15
    public static function setUpBeforeClass()
16
    {
17
        \Doctrine\DBAL\Types\Type::addType('DDC2224Type', DDC2224Type::class);
18
    }
19
20
    public function testIssue()
21
    {
22
        $dql = 'SELECT e FROM ' . __NAMESPACE__ . '\DDC2224Entity e WHERE e.field = :field';
23
        $query = $this->_em->createQuery($dql);
24
        $query->setQueryCacheDriver(new ArrayCache());
25
26
        $query->setParameter('field', 'test', 'DDC2224Type');
27
        $this->assertStringEndsWith('.field = FUNCTION(?)', $query->getSQL());
0 ignored issues
show
$query->getSQL() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
28
29
        return $query;
30
    }
31
32
    /**
33
     * @depends testIssue
34
     */
35
    public function testCacheMissWhenTypeChanges(Query $query)
36
    {
37
        $query->setParameter('field', 'test', 'string');
38
        $this->assertStringEndsWith('.field = ?', $query->getSQL());
0 ignored issues
show
$query->getSQL() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
    }
40
}
41
42
class DDC2224Type extends Type
43
{
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
48
    {
49
        return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
50
    }
51
52
    public function getName()
53
    {
54
        return 'DDC2224Type';
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function canRequireSQLConversion()
61
    {
62
        return true;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
69
    {
70
        return sprintf('FUNCTION(%s)', $sqlExpr);
71
    }
72
}
73
74
/**
75
 * @Entity
76
 */
77
class DDC2224Entity
78
{
79
    /**
80
     * @Id @GeneratedValue @Column(type="integer")
81
     */
82
    public $id;
83
84
    /**
85
     * @Column(type="DDC2224Type")
86
     */
87
    public $field;
88
}
89