Failed Conditions
Pull Request — 2.6 (#7544)
by Luís
08:16
created

GH7534Test::collectionShouldBe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Functional\Ticket;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\Common\Collections\Criteria;
10
use Doctrine\Tests\OrmFunctionalTestCase;
11
use function assert;
12
13
final class GH7534Test extends OrmFunctionalTestCase
14
{
15
    /**
16
     * {@inheritDoc}
17
     */
18
    protected function setUp() : void
19
    {
20
        parent::setUp();
21
22
        $this->setUpEntitySchema(
23
            [
24
                GH7534Person::class,
25
                GH7534PhoneNumber::class,
26
            ]
27
        );
28
29
        $this->_em->persist(
30
            new GH7534Person(
31
                [
32
                    new GH7534PhoneNumber('123', 0),
33
                    new GH7534PhoneNumber('456', 1),
34
                    new GH7534PhoneNumber('789', 2),
35
                ]
36
            )
37
        );
38
39
        $this->_em->flush();
40
        $this->_em->clear();
41
    }
42
43
    /**
44
     * @test
45
     * @group 7534
46
     */
47
    public function collectionShouldBe() : void
48
    {
49
        $person = $this->_em->find(GH7534Person::class, 1);
50
        assert($person instanceof GH7534Person);
51
52
        $criteria = Criteria::create()->where(Criteria::expr()->eq('type', 1))
53
                                      ->orWhere(Criteria::expr()->eq('type', 2));
54
55
        $phoneNumbers = $person->phoneNumbers->matching($criteria);
0 ignored issues
show
Bug introduced by
The method matching() does not exist on Doctrine\Common\Collections\Collection. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Common\Collections\AbstractLazyCollection. Are you sure you never get one of those? ( Ignorable by Annotation )

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

55
        /** @scrutinizer ignore-call */ 
56
        $phoneNumbers = $person->phoneNumbers->matching($criteria);
Loading history...
56
57
        self::assertCount(2, $phoneNumbers);
58
        self::assertSame('456', $phoneNumbers->get(0)->number);
0 ignored issues
show
Bug introduced by
The method get() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as Yaf_Config_Simple or Yaf\Session or Yaf_Session or Yaf\Config\Simple or Yaf\Config\Ini or Yaf_Config_Ini or ResourceBundle or Symfony\Component\Console\Helper\HelperSet or phpDocumentor\Reflection\Types\Compound or Doctrine\Common\Collections\Collection or http\QueryString or Yaf_Config_Simple or Yaf\Session or Yaf_Session or Yaf\Config\Simple or Yaf\Config\Ini or Yaf_Config_Ini. ( Ignorable by Annotation )

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

58
        self::assertSame('456', $phoneNumbers->/** @scrutinizer ignore-call */ get(0)->number);
Loading history...
Bug introduced by
The method get() does not exist on Countable. It seems like you code against a sub-type of Countable such as Doctrine\Common\Collections\Collection or Yaf_Config_Simple or Yaf\Session or Yaf_Session or Yaf\Config\Simple or Yaf\Config\Ini or Yaf_Config_Ini. ( Ignorable by Annotation )

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

58
        self::assertSame('456', $phoneNumbers->/** @scrutinizer ignore-call */ get(0)->number);
Loading history...
59
        self::assertSame('789', $phoneNumbers->get(1)->number);
60
    }
61
}
62
63
/**
64
 * @Entity
65
 */
66
class GH7534Person
67
{
68
    /**
69
     * @Id
70
     * @Column(type="integer")
71
     * @GeneratedValue
72
     * @var int
73
     */
74
    public $id;
75
76
    /**
77
     * @ManyToMany(targetEntity=GH7534PhoneNumber::class, cascade={"all"})
78
     * @var Collection
79
     */
80
    public $phoneNumbers;
81
82
    /**
83
     * @param GH7534PhoneNumber[] $phoneNumbers
84
     */
85
    public function __construct(array $phoneNumbers = [])
86
    {
87
        $this->phoneNumbers = new ArrayCollection($phoneNumbers);
88
    }
89
}
90
91
/**
92
 * @Entity
93
 */
94
class GH7534PhoneNumber
95
{
96
    /**
97
     * @Id
98
     * @Column(type="integer")
99
     * @GeneratedValue
100
     * @var int
101
     */
102
    public $id;
103
104
    /**
105
     * @Column(type="string")
106
     * @var string
107
     */
108
    public $number;
109
110
    /**
111
     * @Column(type="integer")
112
     * @var int
113
     */
114
    public $type;
115
116
    public function __construct(string $number, int $type)
117
    {
118
        $this->number = $number;
119
        $this->type   = $type;
120
    }
121
}
122