Passed
Pull Request — master (#7607)
by
unknown
09:40
created

GH7605Test   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testCaseAsLeftOperandOfComparisonAndInExpression() 0 64 4
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 = array(
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
39
            // The fields order is: id, active, closed, balance
40
            array(1, true, false, 10), // ACTIVE
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
41
            array(2, true, false, 0), // FINISHED
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
42
            array(3, false, true, 30), // INACTIVE
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
43
            array(4, true, true, 20), // CLOSED
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
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')
0 ignored issues
show
Coding Style introduced by
Concat operator must be surrounded by a single space
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
77
            ->select('offer')
78
            ->from(GH7605Offer::class, 'offer')
79
            ->where($dqlStatusLogic.' IN (:status)')
0 ignored issues
show
Coding Style introduced by
Concat operator must be surrounded by a single space
Loading history...
80
            ->orderBy('offer.id', 'ASC');
81
        $query = $qb->getQuery();
82
        $query->setParameter('status', array('INACTIVE', 'CLOSED'));
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
83
        $offers = $query->getResult(AbstractQuery::HYDRATE_OBJECT);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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();
0 ignored issues
show
Coding Style introduced by
Concat operator must be surrounded by a single space
Loading history...
90
        $query->setParameter('status', array('FINISHED', 'INACTIVE'));
0 ignored issues
show
Coding Style introduced by
Short array syntax must be used to define arrays
Loading history...
91
        $offers = $query->getResult(AbstractQuery::HYDRATE_OBJECT);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
126
        $this->active = $active;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
127
        $this->closed = $closed;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
128
        $this->balance = $balance;
129
    }
130
}
131