File: CriteriaTest.php

package info (click to toggle)
php-doctrine-collections 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 468 kB
  • sloc: php: 2,800; makefile: 18
file content (185 lines) | stat: -rw-r--r-- 6,362 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\Collections;

use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\Common\Collections\Expr\CompositeExpression;
use Doctrine\Common\Collections\ExpressionBuilder;
use Doctrine\Common\Collections\Order;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\TestCase;

class CriteriaTest extends TestCase
{
    use VerifyDeprecations;

    #[IgnoreDeprecations]
    public function testCreateLegacy(): void
    {
        $this->expectDeprecationWithIdentifier('https://github.com/doctrine/collections/pull/472');

        $criteria = Criteria::create();

        self::assertInstanceOf(Criteria::class, $criteria);
    }

    public function testCreate(): void
    {
        $criteria = Criteria::create(true);

        self::assertInstanceOf(Criteria::class, $criteria);
    }

    public function testConstructor(): void
    {
        $expr     = new Comparison('field', '=', 'value');
        $criteria = new Criteria($expr, ['foo' => Order::Ascending], 10, 20, accessRawFieldValues: true);

        self::assertSame($expr, $criteria->getWhereExpression());
        self::assertSame(['foo' => Order::Ascending], $criteria->orderings());
        self::assertSame(10, $criteria->getFirstResult());
        self::assertSame(20, $criteria->getMaxResults());
    }

    #[IgnoreDeprecations]
    public function testDeprecatedNullOffset(): void
    {
        $expr = new Comparison('field', '=', 'value');

        $this->expectDeprecationWithIdentifier('https://github.com/doctrine/collections/pull/311');
        $criteria = new Criteria($expr, ['foo' => Order::Ascending], null, 20);

        self::assertSame($expr, $criteria->getWhereExpression());
        self::assertSame(['foo' => Order::Ascending], $criteria->orderings());
        self::assertNull($criteria->getFirstResult());
        self::assertSame(20, $criteria->getMaxResults());
    }

    #[IgnoreDeprecations]
    public function testDefaultConstructor(): void
    {
        $this->expectDeprecationWithIdentifier('https://github.com/doctrine/collections/pull/472');
        $this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/collections/pull/311');

        $criteria = new Criteria();

        self::assertNull($criteria->getWhereExpression());
        self::assertSame([], $criteria->orderings());
        self::assertNull($criteria->getFirstResult());
        self::assertNull($criteria->getMaxResults());
    }

    public function testNamedArgs(): void
    {
        $this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/collections/pull/311');
        new Criteria(orderings: ['startDate' => Order::Descending], accessRawFieldValues: true);
    }

    public function testWhere(): void
    {
        $expr     = new Comparison('field', '=', 'value');
        $criteria = Criteria::create(true);

        $criteria->where($expr);

        self::assertSame($expr, $criteria->getWhereExpression());
    }

    public function testAndWhere(): void
    {
        $expr     = new Comparison('field', '=', 'value');
        $criteria = Criteria::create(true);

        $criteria->where($expr);
        $expr = $criteria->getWhereExpression();
        $criteria->andWhere($expr);

        $where = $criteria->getWhereExpression();
        self::assertInstanceOf(CompositeExpression::class, $where);

        self::assertEquals(CompositeExpression::TYPE_AND, $where->getType());
        self::assertSame([$expr, $expr], $where->getExpressionList());
    }

    public function testAndWhereWithoutWhere(): void
    {
        $expr     = new Comparison('field', '=', 'value');
        $criteria = Criteria::create(true);

        $criteria->andWhere($expr);

        self::assertSame($expr, $criteria->getWhereExpression());
    }

    public function testOrWhere(): void
    {
        $expr     = new Comparison('field', '=', 'value');
        $criteria = Criteria::create(true);

        $criteria->where($expr);
        $expr = $criteria->getWhereExpression();
        $criteria->orWhere($expr);

        $where = $criteria->getWhereExpression();
        self::assertInstanceOf(CompositeExpression::class, $where);

        self::assertEquals(CompositeExpression::TYPE_OR, $where->getType());
        self::assertSame([$expr, $expr], $where->getExpressionList());
    }

    public function testOrWhereWithoutWhere(): void
    {
        $expr     = new Comparison('field', '=', 'value');
        $criteria = Criteria::create(true);

        $criteria->orWhere($expr);

        self::assertSame($expr, $criteria->getWhereExpression());
    }

    public function testOrderings(): void
    {
        $criteria = Criteria::create(true)
            ->orderBy(['foo' => Order::Ascending]);

        self::assertEquals(['foo' => Order::Ascending], $criteria->orderings());
    }

    public function testExpr(): void
    {
        self::assertInstanceOf(ExpressionBuilder::class, Criteria::expr());
    }

    #[IgnoreDeprecations]
    public function testPassingNonOrderEnumToOrderByIsDeprecated(): void
    {
        $this->expectDeprecationWithIdentifier('https://github.com/doctrine/collections/pull/389');
        $criteria = Criteria::create(true)->orderBy(['foo' => 'ASC']);
    }

    #[IgnoreDeprecations]
    public function testConstructingCriteriaWithNonOrderEnumIsDeprecated(): void
    {
        $this->expectDeprecationWithIdentifier('https://github.com/doctrine/collections/pull/389');
        $criteria = new Criteria(null, ['foo' => 'ASC'], firstResult: 0, accessRawFieldValues: true);
    }

    public function testUsingOrderEnumIsTheRightWay(): void
    {
        $this->expectNoDeprecationWithIdentifier('https://github.com/doctrine/collections/pull/389');
        Criteria::create(true)->orderBy(['foo' => Order::Ascending]);
        new Criteria(null, ['foo' => Order::Ascending], firstResult: 0, accessRawFieldValues: true);
    }

    #[IgnoreDeprecations]
    public function testCallingGetOrderingsIsDeprecated(): void
    {
        $criteria = Criteria::create(true)->orderBy(['foo' => Order::Ascending]);
        $this->expectDeprecationWithIdentifier('https://github.com/doctrine/collections/pull/389');
        $criteria->getOrderings();
    }
}