Skip to content

Commit 8cfa174

Browse files
committed
Add failing tests: Criteria matching on fields with custom column types
1 parent bd4a053 commit 8cfa174

File tree

6 files changed

+149
-1
lines changed

6 files changed

+149
-1
lines changed

tests/Tests/Models/ValueConversionType/InversedManyToManyEntity.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ class InversedManyToManyEntity
2525
*/
2626
public $id1;
2727

28+
/**
29+
* @var string
30+
* @Column(type="rot13", length=255, nullable=true)
31+
*/
32+
public $field = null;
33+
2834
/**
2935
* @phpstan-var Collection<int, OwningManyToManyEntity>
3036
* @ManyToMany(targetEntity="OwningManyToManyEntity", mappedBy="associatedEntities")

tests/Tests/Models/ValueConversionType/InversedOneToManyEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class InversedOneToManyEntity
3333

3434
/**
3535
* @var string
36-
* @Column(type="string", name="some_property", length=255)
36+
* @Column(type="string", name="some_property", length=255, nullable=true)
3737
*/
3838
public $someProperty;
3939

tests/Tests/Models/ValueConversionType/OwningManyToManyEntity.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class OwningManyToManyEntity
2727
*/
2828
public $id2;
2929

30+
/**
31+
* @var string
32+
* @Column(type="rot13", length=255, nullable=true)
33+
*/
34+
public $field = null;
35+
3036
/**
3137
* @var Collection<int, InversedManyToManyEntity>
3238
* @ManyToMany(targetEntity="InversedManyToManyEntity", inversedBy="associatedEntities")

tests/Tests/Models/ValueConversionType/OwningManyToOneEntity.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class OwningManyToOneEntity
2424
*/
2525
public $id2;
2626

27+
/**
28+
* @var string
29+
* @Column(type="rot13", length=255, nullable=true)
30+
*/
31+
public $field = null;
32+
2733
/**
2834
* @var InversedOneToManyEntity
2935
* @ManyToOne(targetEntity="InversedOneToManyEntity", inversedBy="associatedEntities")
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional\ValueConversionType;
6+
7+
use Doctrine\Common\Collections\Criteria;
8+
use Doctrine\Common\Collections\Expr\Comparison;
9+
use Doctrine\Tests\Models\ValueConversionType\InversedManyToManyEntity;
10+
use Doctrine\Tests\Models\ValueConversionType\OwningManyToManyEntity;
11+
use Doctrine\Tests\OrmFunctionalTestCase;
12+
use Generator;
13+
14+
class ManyToManyCriteriaMatchingTest extends OrmFunctionalTestCase
15+
{
16+
protected function setUp(): void
17+
{
18+
$this->useModelSet('vct_manytomany');
19+
20+
parent::setUp();
21+
}
22+
23+
/**
24+
* @dataProvider provideMatchingExpressions
25+
*/
26+
public function testCriteriaMatchingOnFieldInManyToManyTarget(Comparison $comparison): void
27+
{
28+
$associated = new InversedManyToManyEntity();
29+
$associated->id1 = 'associated';
30+
31+
$matching = new OwningManyToManyEntity();
32+
$matching->id2 = 'first';
33+
$matching->field = 'match this';
34+
$matching->associatedEntities->add($associated);
35+
36+
$nonMatching = new OwningManyToManyEntity();
37+
$nonMatching->id2 = 'second';
38+
$nonMatching->field = 'this is no match';
39+
$nonMatching->associatedEntities->add($associated);
40+
41+
$this->_em->persist($associated);
42+
$this->_em->persist($matching);
43+
$this->_em->persist($nonMatching);
44+
$this->_em->flush();
45+
$this->_em->clear();
46+
47+
$this->getQueryLog()->reset()->enable();
48+
49+
$associated = $this->_em->find(InversedManyToManyEntity::class, 'associated');
50+
self::assertFalse($associated->associatedEntities->isInitialized(), 'Pre-condition: lazy collection');
51+
52+
$result = $associated->associatedEntities->matching(Criteria::create()->where($comparison));
53+
54+
$l = $this->getQueryLog();
55+
56+
self::assertCount(1, $result);
57+
self::assertSame('first', $result[0]->id2);
58+
}
59+
60+
public function provideMatchingExpressions(): Generator
61+
{
62+
yield [Criteria::expr()->eq('field', 'match this')];
63+
yield [Criteria::expr()->in('field', ['match this'])];
64+
}
65+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional\ValueConversionType;
6+
7+
use Doctrine\Common\Collections\Criteria;
8+
use Doctrine\Common\Collections\Expr\Comparison;
9+
use Doctrine\Tests\Models\ValueConversionType\InversedOneToManyEntity;
10+
use Doctrine\Tests\Models\ValueConversionType\OwningManyToOneEntity;
11+
use Doctrine\Tests\OrmFunctionalTestCase;
12+
use Generator;
13+
14+
class OneToManyCriteriaMatchingTest extends OrmFunctionalTestCase
15+
{
16+
protected function setUp(): void
17+
{
18+
$this->useModelSet('vct_onetomany');
19+
20+
parent::setUp();
21+
}
22+
23+
/**
24+
* @dataProvider provideMatchingExpressions
25+
*/
26+
public function testCriteriaMatchingOnFieldInOneToManyTarget(Comparison $comparison): void
27+
{
28+
$entityWithCollection = new InversedOneToManyEntity();
29+
$entityWithCollection->id1 = 'associated';
30+
31+
$matching = new OwningManyToOneEntity();
32+
$matching->id2 = 'first';
33+
$matching->field = 'match this';
34+
$matching->associatedEntity = $entityWithCollection;
35+
36+
$notMatching = new OwningManyToOneEntity();
37+
$notMatching->id2 = 'second';
38+
$notMatching->field = 'this is no match';
39+
$notMatching->associatedEntity = $entityWithCollection;
40+
41+
$this->_em->persist($entityWithCollection);
42+
$this->_em->persist($matching);
43+
$this->_em->persist($notMatching);
44+
$this->_em->flush();
45+
$this->_em->clear();
46+
47+
$this->getQueryLog()->reset()->enable();
48+
49+
$entityWithCollection = $this->_em->find(InversedOneToManyEntity::class, 'associated');
50+
self::assertFalse($entityWithCollection->associatedEntities->isInitialized(), 'Pre-condition: lazy collection');
51+
52+
$result = $entityWithCollection->associatedEntities->matching(Criteria::create()->where($comparison));
53+
54+
$l = $this->getQueryLog();
55+
56+
self::assertCount(1, $result);
57+
self::assertSame('first', $result[0]->id2);
58+
}
59+
60+
public function provideMatchingExpressions(): Generator
61+
{
62+
yield [Criteria::expr()->eq('field', 'match this')];
63+
yield [Criteria::expr()->in('field', ['match this'])];
64+
}
65+
}

0 commit comments

Comments
 (0)