Failed Conditions
Push — master ( 9452f0...8be1e3 )
by Marco
10:03
created

ManyToManyUnidirectionalAssociationTest.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional;
6
7
use Doctrine\ORM\Mapping\FetchMode;
8
use Doctrine\Tests\Models\ECommerce\ECommerceCart;
9
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
10
11
/**
12
 * Tests a unidirectional many-to-many association mapping (without inheritance).
13
 * Inverse side is not present.
14
 */
15
class ManyToManyUnidirectionalAssociationTest extends AbstractManyToManyAssociationTestCase
16
{
17
    protected $firstField = 'cart_id';
18
    protected $secondField = 'product_id';
19
    protected $table = 'ecommerce_carts_products';
20
    private $firstProduct;
21
    private $secondProduct;
22
    private $firstCart;
23
    private $secondCart;
24
25
    protected function setUp()
26
    {
27
        $this->useModelSet('ecommerce');
28
29
        parent::setUp();
30
31
        $this->firstProduct = new ECommerceProduct();
32
33
        $this->firstProduct->setName('Doctrine 1.x Manual');
34
35
        $this->secondProduct = new ECommerceProduct();
36
37
        $this->secondProduct->setName('Doctrine 2.x Manual');
38
39
        $this->firstCart = new ECommerceCart();
40
        $this->secondCart = new ECommerceCart();
41
    }
42
43
    public function testSavesAManyToManyAssociationWithCascadeSaveSet()
44
    {
45
        $this->firstCart->addProduct($this->firstProduct);
46
        $this->firstCart->addProduct($this->secondProduct);
47
48
        $this->em->persist($this->firstCart);
49
        $this->em->flush();
50
51
        self::assertForeignKeysContain($this->firstCart->getId(), $this->firstProduct->getId());
52
        self::assertForeignKeysContain($this->firstCart->getId(), $this->secondProduct->getId());
53
    }
54
55
    public function testRemovesAManyToManyAssociation()
56
    {
57
        $this->firstCart->addProduct($this->firstProduct);
58
        $this->firstCart->addProduct($this->secondProduct);
59
        $this->em->persist($this->firstCart);
60
        $this->firstCart->removeProduct($this->firstProduct);
61
62
        $this->em->flush();
63
64
        self::assertForeignKeysNotContain($this->firstCart->getId(), $this->firstProduct->getId());
65
        self::assertForeignKeysContain($this->firstCart->getId(), $this->secondProduct->getId());
66
    }
67
68
    public function testEagerLoad()
69
    {
70
        $this->createFixture();
71
72
        $query = $this->em->createQuery('SELECT c, p FROM Doctrine\Tests\Models\ECommerce\ECommerceCart c LEFT JOIN c.products p ORDER BY c.id, p.id');
73
        $result = $query->getResult();
74
        $firstCart = $result[0];
75
        $products = $firstCart->getProducts();
76
        $secondCart = $result[1];
77
78
        self::assertInstanceOf(ECommerceProduct::class, $products[0]);
79
        self::assertInstanceOf(ECommerceProduct::class, $products[1]);
80
        self::assertCollectionEquals($products, $secondCart->getProducts());
81
        //self::assertEquals("Doctrine 1.x Manual", $products[0]->getName());
82
        //self::assertEquals("Doctrine 2.x Manual", $products[1]->getName());
83
    }
84
85
    public function testLazyLoadsCollection()
86
    {
87
        $this->createFixture();
88
        $metadata = $this->em->getClassMetadata(ECommerceCart::class);
89
        $metadata->getProperty('products')->setFetchMode(FetchMode::LAZY);
0 ignored issues
show
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

89
        $metadata->/** @scrutinizer ignore-call */ 
90
                   getProperty('products')->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...
90
91
        $query = $this->em->createQuery('SELECT c FROM Doctrine\Tests\Models\ECommerce\ECommerceCart c');
92
        $result = $query->getResult();
93
        $firstCart = $result[0];
94
        $products = $firstCart->getProducts();
95
        $secondCart = $result[1];
96
97
        self::assertInstanceOf(ECommerceProduct::class, $products[0]);
98
        self::assertInstanceOf(ECommerceProduct::class, $products[1]);
99
        self::assertCollectionEquals($products, $secondCart->getProducts());
100
    }
101
102
    private function createFixture()
103
    {
104
        $this->firstCart->addProduct($this->firstProduct);
105
        $this->firstCart->addProduct($this->secondProduct);
106
        $this->secondCart->addProduct($this->firstProduct);
107
        $this->secondCart->addProduct($this->secondProduct);
108
        $this->em->persist($this->firstCart);
109
        $this->em->persist($this->secondCart);
110
111
        $this->em->flush();
112
        $this->em->clear();
113
    }
114
}
115