1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Functional\Ticket; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\AbstractQuery; |
8
|
|
|
use Doctrine\ORM\Annotation as ORM; |
9
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
10
|
|
|
|
11
|
|
|
class GH7605Test extends OrmFunctionalTestCase |
12
|
|
|
{ |
13
|
|
|
protected function setUp() : void |
14
|
|
|
{ |
15
|
|
|
parent::setUp(); |
16
|
|
|
|
17
|
|
|
$this->schemaTool->createSchema( |
18
|
|
|
[ |
19
|
|
|
$this->em->getClassMetadata(GH7605Offer::class), |
20
|
|
|
] |
21
|
|
|
); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The intent of this test is to ensure that the ORM is capable |
26
|
|
|
* of generate queries using both ComparisonExpression and InExpression |
27
|
|
|
* when the left operand is a CASE statement |
28
|
|
|
* |
29
|
|
|
* @group 7605 |
30
|
|
|
*/ |
31
|
|
|
public function testCaseAsLeftOperandOfComparisonAndInExpression() : void |
32
|
|
|
{ |
33
|
|
|
// Rules to define offer status (the order matters): |
34
|
|
|
// 1 - active === false? INACTIVE |
35
|
|
|
// 2 - closed === true? CLOSED |
36
|
|
|
// 3 - balance > 0? ACTIVE |
37
|
|
|
// 4 - balance === 0? FINISHED |
38
|
|
|
$offersInfo = [ |
39
|
|
|
// The fields order is: id, active, closed, balance |
40
|
|
|
[1, true, false, 10], // ACTIVE |
41
|
|
|
[2, true, false, 0], // FINISHED |
42
|
|
|
[3, false, true, 30], // INACTIVE |
43
|
|
|
[4, true, true, 20], // CLOSED |
44
|
|
|
]; |
45
|
|
|
foreach ($offersInfo as $offerInfo) { |
46
|
|
|
$offer = new GH7605Offer($offerInfo[0], $offerInfo[1], $offerInfo[2], $offerInfo[3]); |
47
|
|
|
$this->em->persist($offer); |
48
|
|
|
} |
49
|
|
|
$this->em->flush(); |
50
|
|
|
$this->em->clear(); |
51
|
|
|
|
52
|
|
|
$dqlStatusLogic = "CASE |
53
|
|
|
WHEN offer.active = FALSE THEN 'INACTIVE' |
54
|
|
|
WHEN offer.closed = TRUE THEN 'CLOSED' |
55
|
|
|
WHEN offer.balance > 0 THEN 'ACTIVE' |
56
|
|
|
ELSE 'FINISHED' |
57
|
|
|
END"; |
58
|
|
|
|
59
|
|
|
// ComparisonExpression tests |
60
|
|
|
$query = $this->em->createQueryBuilder() |
61
|
|
|
->select('offer') |
62
|
|
|
->from(GH7605Offer::class, 'offer') |
63
|
|
|
->where($dqlStatusLogic . ' = :status') |
64
|
|
|
->setMaxResults(1) |
65
|
|
|
->getQuery(); |
66
|
|
|
|
67
|
|
|
$query->setParameter('status', 'ACTIVE'); |
68
|
|
|
$offer = $query->getOneOrNullResult(AbstractQuery::HYDRATE_OBJECT); |
69
|
|
|
self::assertEquals(1, $offer->id); |
70
|
|
|
|
71
|
|
|
$query->setParameter('status', 'INACTIVE'); |
72
|
|
|
$offer = $query->getOneOrNullResult(AbstractQuery::HYDRATE_OBJECT); |
73
|
|
|
self::assertEquals(3, $offer->id); |
74
|
|
|
|
75
|
|
|
// InExpression tests |
76
|
|
|
$qb = $this->em->createQueryBuilder() |
77
|
|
|
->select('offer') |
78
|
|
|
->from(GH7605Offer::class, 'offer') |
79
|
|
|
->where($dqlStatusLogic . ' IN (:status)') |
80
|
|
|
->orderBy('offer.id', 'ASC'); |
81
|
|
|
$query = $qb->getQuery(); |
82
|
|
|
$query->setParameter('status', ['INACTIVE', 'CLOSED']); |
83
|
|
|
$offers = $query->getResult(AbstractQuery::HYDRATE_OBJECT); |
84
|
|
|
$expectedIds = [3, 4]; |
85
|
|
|
foreach ($offers as $i => $offer) { |
86
|
|
|
self::assertEquals($expectedIds[$i], $offer->id); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$query = $qb->where($dqlStatusLogic . ' NOT IN (:status)')->getQuery(); |
90
|
|
|
$query->setParameter('status', ['FINISHED', 'INACTIVE']); |
91
|
|
|
$offers = $query->getResult(AbstractQuery::HYDRATE_OBJECT); |
92
|
|
|
$expectedIds = [1, 4]; |
93
|
|
|
foreach ($offers as $i => $offer) { |
94
|
|
|
self::assertEquals($expectedIds[$i], $offer->id); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @ORM\Entity |
101
|
|
|
*/ |
102
|
|
|
class GH7605Offer |
103
|
|
|
{ |
104
|
|
|
/** |
105
|
|
|
* @ORM\Id |
106
|
|
|
* @ORM\Column(type="integer") |
107
|
|
|
* @ORM\GeneratedValue(strategy="NONE") |
108
|
|
|
*/ |
109
|
|
|
public $id; |
110
|
|
|
|
111
|
|
|
/** @ORM\Column(type="boolean", nullable=false) */ |
112
|
|
|
public $active; |
113
|
|
|
|
114
|
|
|
/** @ORM\Column(type="boolean", nullable=false) */ |
115
|
|
|
public $closed; |
116
|
|
|
|
117
|
|
|
/** @ORM\Column(type="integer", nullable=false) */ |
118
|
|
|
public $balance; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $name |
122
|
|
|
*/ |
123
|
|
|
public function __construct($id, $active, $closed, $balance) |
124
|
|
|
{ |
125
|
|
|
$this->id = $id; |
126
|
|
|
$this->active = $active; |
127
|
|
|
$this->closed = $closed; |
128
|
|
|
$this->balance = $balance; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|