Failed Conditions
Pull Request — master (#7766)
by
unknown
07:43
created

OneToManySelfReferentialAssociationTest::testMatchingOverrulesCollectionOrdering()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 15
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional;
6
7
use Doctrine\Common\Collections\Criteria;
8
use Doctrine\ORM\Mapping\FetchMode;
9
use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
10
use Doctrine\Tests\OrmFunctionalTestCase;
11
use function strstr;
0 ignored issues
show
introduced by
Type strstr is not used in this file.
Loading history...
12
13
/**
14
 * Tests a bidirectional one-to-one association mapping (without inheritance).
15
 */
16
class OneToManySelfReferentialAssociationTest extends OrmFunctionalTestCase
17
{
18
    private $parent;
19
    private $firstChild;
20
    private $secondChild;
21
    private $thirdChild;
22
23
    protected function setUp() : void
24
    {
25
        $this->useModelSet('ecommerce');
26
        parent::setUp();
27
        $this->parent = new ECommerceCategory();
28
        $this->parent->setName('Programming languages books');
29
        $this->firstChild = new ECommerceCategory();
30
        $this->firstChild->setName('Java books');
31
        $this->secondChild = new ECommerceCategory();
32
        $this->secondChild->setName('Php books');
33
        $this->thirdChild = new ECommerceCategory();
34
        $this->thirdChild->setName('BBQ books');
35
    }
36
37
    public function testMatchingWithNoOrderingUsesDatabaseDefault() : void
38
    {
39
        $this->createFixture();
40
41
        $metadata = $this->em->getClassMetadata(ECommerceCategory::class);
42
        $metadata->getProperty('children')->setFetchMode(FetchMode::LAZY);
0 ignored issues
show
Bug introduced by
The method getProperty() does not exist on Doctrine\Common\Persistence\Mapping\ClassMetadata. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $metadata->/** @scrutinizer ignore-call */ 
43
                   getProperty('children')->setFetchMode(FetchMode::LAZY);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
44
        $query    = $this->em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id asc');
45
        $result   = $query->getResult();
46
        $parent   = $result[0];
47
        $children = $parent->getChildren()->matching(Criteria::create());
48
        $this->assertEquals('Java books', $children[0]->getName());
49
        $this->assertEquals('Php books', $children[1]->getName());
50
        $this->assertEquals('BBQ books', $children[2]->getName());
51
    }
52
53
    public function testMatchingRespectsCollectionOrdering() : void
54
    {
55
        $this->createFixture();
56
57
        $metadata = $this->em->getClassMetadata(ECommerceCategory::class);
58
        $metadata->getProperty('children')->setFetchMode(FetchMode::LAZY);
59
        $metadata->getProperty('children')->setOrderBy(['name' => 'DESC']);
60
61
        $query    = $this->em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id asc');
62
        $result   = $query->getResult();
63
        $parent   = $result[0];
64
        $children = $parent->getChildren()->matching(Criteria::create());
65
        $this->assertEquals('Php books', $children[0]->getName());
66
        $this->assertEquals('Java books', $children[1]->getName());
67
        $this->assertEquals('BBQ books', $children[2]->getName());
68
    }
69
70
    public function testMatchingOverrulesCollectionOrdering() : void
71
    {
72
        $this->createFixture();
73
74
        $metadata = $this->em->getClassMetadata(ECommerceCategory::class);
75
        $metadata->getProperty('children')->setFetchMode(FetchMode::LAZY);
76
        $metadata->getProperty('children')->setOrderBy(['name' => 'DESC']);
77
78
        $query    = $this->em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id asc');
79
        $result   = $query->getResult();
80
        $parent   = $result[0];
81
        $children = $parent->getChildren()->matching(Criteria::create()->orderBy(['name' => 'ASC']));
82
        $this->assertEquals('BBQ books', $children[0]->getName());
83
        $this->assertEquals('Java books', $children[1]->getName());
84
        $this->assertEquals('Php books', $children[2]->getName());
85
    }
86
87
    private function createFixture()
88
    {
89
        $this->parent->addChild($this->firstChild);
90
        $this->parent->addChild($this->secondChild);
91
        $this->parent->addChild($this->thirdChild);
92
        $this->em->persist($this->parent);
93
94
        $this->em->flush();
95
        $this->em->clear();
96
    }
97
}
98