Completed
Push — master ( fb5ff3...430575 )
by Massimiliano
01:16
created

Tests/Listener/TagSubscriberTest.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Beelab\TagBundle\Tests\Listener;
4
5
use Beelab\TagBundle\Listener\TagSubscriber;
6
use Beelab\TagBundle\Test\NonTaggableStub;
7
use Beelab\TagBundle\Test\TaggableStub;
8
use Beelab\TagBundle\Test\TaggableStub2;
9
use Beelab\TagBundle\Test\TaggableStub3;
10
use Beelab\TagBundle\Test\TagStub;
11
use Doctrine\Common\Persistence\Mapping\MappingException;
12
use Doctrine\ORM\EntityManager;
13
use Doctrine\ORM\Event\OnFlushEventArgs;
14
use Doctrine\ORM\Mapping\ClassMetadata;
15
use PHPUnit\Framework\TestCase;
16
17
/**
18
 * @group unit
19
 */
20
class TagSubscriberTest extends TestCase
21
{
22
    public function testNonexistentClass()
23
    {
24
        $this->expectException(MappingException::class);
25
26
        $subscriber = new TagSubscriber('ClassDoesNotExist');
27
    }
28
29
    public function testInvalidClass()
30
    {
31
        $this->expectException(\InvalidArgumentException::class);
32
33
        $subscriber = new TagSubscriber(NonTaggableStub::class);
34
    }
35
36
    public function testGetSubscribedEvents()
37
    {
38
        $tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock();
39
        $subscriber = new TagSubscriber(get_class($tag));
40
41
        $this->assertContains('onFlush', $subscriber->getSubscribedEvents());
42
    }
43
44
    public function testOnFlush()
45
    {
46
        $tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock();
47
        $args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock();
48
        $manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
49
        $repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')->disableOriginalConstructor()->getMock();
50
        $uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock();
51
        $metadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
52
53
        $args->expects($this->once())->method('getEntityManager')->will($this->returnValue($manager));
54
        $manager->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($uow));
55
        $manager->expects($this->any())->method('getRepository')->will($this->returnValue($repo));
56
        $manager->expects($this->any())->method('getClassMetadata')->will($this->returnValue($metadata));
57
        $uow
58
            ->expects($this->once())
59
            ->method('getScheduledEntityInsertions')
60
            ->will($this->returnValue([new TaggableStub(), new NonTaggableStub()]))
61
        ;
62
        $uow
63
            ->expects($this->once())
64
            ->method('getScheduledEntityUpdates')
65
            ->will($this->returnValue([new TaggableStub2()]))
66
        ;
67
        $uow->expects($this->never())->method('getScheduledEntityDeletions');
68
69
        $subscriber = new TagSubscriber(get_class($tag));
70
        $subscriber->onFlush($args);
0 ignored issues
show
$args is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\Event\OnFlushEventArgs>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
    }
72
73
    public function testOnFlushEntityWithoutTagsUpdate()
74
    {
75
        $tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock();
76
        $args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock();
77
        $manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
78
        $uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock();
79
        $metadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
80
81
        $args->expects($this->once())->method('getEntityManager')->will($this->returnValue($manager));
82
        $manager->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($uow));
83
        $manager->expects($this->any())->method('getClassMetadata')->will($this->returnValue($metadata));
84
        $uow
85
            ->expects($this->once())
86
            ->method('getScheduledEntityInsertions')
87
            ->will($this->returnValue([]))
88
        ;
89
        $uow
90
            ->expects($this->once())
91
            ->method('getScheduledEntityUpdates')
92
            ->will($this->returnValue([new TaggableStub3()]))
93
        ;
94
        $uow->expects($this->never())->method('getScheduledEntityDeletions');
95
96
        $subscriber = new TagSubscriber(get_class($tag));
97
        $subscriber->onFlush($args);
0 ignored issues
show
$args is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\Event\OnFlushEventArgs>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
    }
99
100
    public function testOnFlushEntityWithoutTagsInsert()
101
    {
102
        $tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock();
103
        $args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock();
104
        $manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
105
        $uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock();
106
        $metadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
107
108
        $args->expects($this->once())->method('getEntityManager')->will($this->returnValue($manager));
109
        $manager->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($uow));
110
        $manager->expects($this->any())->method('getClassMetadata')->will($this->returnValue($metadata));
111
        $uow
112
            ->expects($this->once())
113
            ->method('getScheduledEntityInsertions')
114
            ->will($this->returnValue([new TaggableStub3()]))
115
        ;
116
        $uow
117
            ->expects($this->once())
118
            ->method('getScheduledEntityUpdates')
119
            ->will($this->returnValue([]))
120
        ;
121
        $uow->expects($this->never())->method('getScheduledEntityDeletions');
122
123
        $subscriber = new TagSubscriber(get_class($tag));
124
        $subscriber->onFlush($args);
0 ignored issues
show
$args is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\Event\OnFlushEventArgs>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
125
    }
126
127
    public function testOnFlushWithPurge()
128
    {
129
        $tag = new TagStub();
130
        $args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock();
131
        $manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
132
        $uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock();
133
134
        $args->expects($this->once())->method('getEntityManager')->will($this->returnValue($manager));
135
        $manager->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($uow));
136
        $uow->expects($this->once())->method('getScheduledEntityInsertions')->will($this->returnValue([]));
137
        $uow->expects($this->once())->method('getScheduledEntityUpdates')->will($this->returnValue([]));
138
        $uow
139
            ->expects($this->once())
140
            ->method('getScheduledEntityDeletions')
141
            ->will($this->returnValue([new TaggableStub()]))
142
        ;
143
144
        $subscriber = new TagSubscriber(get_class($tag), true);
145
        $subscriber->onFlush($args);
0 ignored issues
show
$args is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\Event\OnFlushEventArgs>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
146
    }
147
148
    public function testSetTags()
149
    {
150
        $tag = $this->getMockBuilder('Beelab\TagBundle\Tag\TagInterface')->getMock();
151
        $args = $this->getMockBuilder(OnFlushEventArgs::class)->disableOriginalConstructor()->getMock();
152
        $manager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
153
        $uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')->disableOriginalConstructor()->getMock();
154
155
        $args->expects($this->once())->method('getEntityManager')->will($this->returnValue($manager));
156
        $manager->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($uow));
157
        // TODO create some stubs of taggable entities and non-taggable entities...
158
        $uow->expects($this->once())->method('getScheduledEntityInsertions')->will($this->returnValue([$tag]));
159
        $uow->expects($this->once())->method('getScheduledEntityUpdates')->will($this->returnValue([]));
160
        $uow->expects($this->once())->method('getScheduledEntityDeletions')->will($this->returnValue([]));
161
162
        $subscriber = new TagSubscriber(get_class($tag), true);
163
        $subscriber->onFlush($args);
0 ignored issues
show
$args is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\Event\OnFlushEventArgs>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
164
    }
165
}
166