GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#7)
by Fábio Tadeu da
03:01 queued 55s
created

StubMapBasedTest::testOnFlushWithNoDomainEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox;
6
7
use Doctrine\Common\EventArgs;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Doctrine\ORM\Event\OnFlushEventArgs;
10
use Doctrine\ORM\UnitOfWork;
11
use Dsantang\DomainEvents\Counter;
12
use Dsantang\DomainEvents\DomainEvent;
13
use Dsantang\DomainEvents\EventAware;
14
use Dsantang\DomainEvents\Registry\OrderedEventRegistry;
15
use Dsantang\DomainEventsDoctrine\Tests\OutboxSubClass;
16
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\Converters\FirstOutboxConverter;
17
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\Converters\SecondOutboxConverter;
18
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\Converters\ThirdOutboxConverter;
19
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\FirstDomainEvent;
20
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\SecondDomainEvent;
21
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\ThirdDomainEvent;
22
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\OutboxEntries\FirstOutboxEntry;
23
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\OutboxEntries\SecondOutboxEntry;
24
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\OutboxEntries\ThirdOutboxEntry;
25
use Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\StubMapBased;
26
use PHPUnit\Framework\MockObject\MockObject;
27
use PHPUnit\Framework\TestCase;
28
use function array_values;
29
30
final class StubMapBasedTest extends TestCase
31
{
32
    /** @var converter[] */
33
    private $conversionMap;
34
35
    /**
36
     * @before
37
     */
38
    public function setUpDependencies() : void
39
    {
40
        $this->conversionMap =
41
            [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('Dsantang\DomainEv...ThirdOutboxConverter()) of type array<string,Dsantang\Do...s\ThirdOutboxConverter> is incompatible with the declared type Dsantang\DomainEventsDoc...Unit\Outbox\converter[] of property $conversionMap.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
                'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\FirstDomainEvent'  =>
43
                new FirstOutboxConverter(),
44
45
                'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\SecondDomainEvent' =>
46
                new SecondOutboxConverter(),
47
48
                'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\ThirdDomainEvent'  =>
49
                new ThirdOutboxConverter(),
50
            ];
51
    }
52
53
    public function testGetDomainsEvents() : void
54
    {
55
        Counter::reset();
56
57
        $eventArgs = $this->getEventArgs();
58
59
        self::assertEquals(3, Counter::getNext());
60
61
        $mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap);
62
63
        $eventsResult = $mapBasedEventsHandler->getDomainEvents($eventArgs);
64
65
        self::assertEquals(0, Counter::getNext());
66
67
        $eventsExpected = [new FirstDomainEvent(), new SecondDomainEvent(), new ThirdDomainEvent()];
68
69
        $this->assertEquals(array_values($eventsResult), array_values($eventsExpected));
70
    }
71
72
    public function testPersist() : void
73
    {
74
        $eventArgs      = $this->getEventArgs();
75
        $outboxSubClass = new OutboxSubClass();
76
77
        $mapBasedEventsHandler = new StubMapBased($outboxSubClass, $this->conversionMap);
78
        $mapBasedEventsHandler->getDomainEvents($eventArgs);
79
80
        $outboxEvents = [
81
            new ThirdOutboxEntry(new ThirdDomainEvent()),
82
            new FirstOutboxEntry(new FirstDomainEvent()),
83
            new SecondOutboxEntry(new SecondDomainEvent()),
84
        ];
85
86
        $eventArgs->getEntityManager()->getUnitOfWork()->expects(self::once())
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not exist on Doctrine\Common\EventArgs. It seems like you code against a sub-type of Doctrine\Common\EventArgs such as Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs or Doctrine\ORM\Event\OnFlushEventArgs or Doctrine\ORM\Event\OnClearEventArgs or Doctrine\ORM\Event\PostFlushEventArgs or Doctrine\ORM\Event\PreFlushEventArgs or Doctrine\ORM\Event\LoadClassMetadataEventArgs or Doctrine\ORM\Event\LifecycleEventArgs. ( Ignorable by Annotation )

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

86
        $eventArgs->/** @scrutinizer ignore-call */ 
87
                    getEntityManager()->getUnitOfWork()->expects(self::once())
Loading history...
87
            ->method('computeChangeSets');
88
89
        $eventArgs->getEntityManager()->expects(self::exactly(3))
90
            ->method('persist');
91
92
        $mapBasedEventsHandler->persist(
93
            $eventArgs->getEntityManager(),
94
            ...$outboxEvents
95
        );
96
    }
97
98
    public function testOnFlushWithDomainEvents() : void
99
    {
100
        $eventArgs = $this->getEventArgs();
101
102
        $mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap);
103
104
        $eventArgs->getEntityManager()->expects(self::exactly(3))
105
            ->method('persist');
106
107
        $mapBasedEventsHandler->onFlush($eventArgs);
108
    }
109
110
    public function testOnFlushWithNoDomainEvents() : void
111
    {
112
        $eventArgs = $this->getEventArgs(false);
113
114
        $mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap);
115
        $mapBasedEventsHandler->onFlush($eventArgs);
116
117
        $this->addToAssertionCount(1);
118
    }
119
120
    /**
121
     * @return EventArgs|MockObject
122
     */
123
    private function getEventArgs(bool $withEvents = true) : EventArgs
124
    {
125
        $insertions = $updates = $deletions = [];
126
127
        if ($withEvents) {
128
            $entity1 = $this->getEntityClass();
129
            $entity2 = $this->getEntityClass();
130
            $entity3 = $this->getEntityClass();
131
132
            $domainEvent1 = new FirstDomainEvent();
133
            $domainEvent2 = new SecondDomainEvent();
134
            $domainEvent3 = new ThirdDomainEvent();
135
136
            $entity1->trigger($domainEvent1);
137
            $entity2->trigger($domainEvent2);
138
            $entity3->trigger($domainEvent3);
139
140
            $updates   = [$entity3];
141
            $deletions = [$entity2, $entity1];
142
        }
143
144
        $unitOfWork = $this->createMock(UnitOfWork::class);
145
146
        $unitOfWork->expects(self::once())->method('getScheduledEntityInsertions')->willReturn($insertions);
147
        $unitOfWork->expects(self::once())->method('getScheduledEntityUpdates')->willReturn($updates);
148
        $unitOfWork->expects(self::once())->method('getScheduledEntityDeletions')->willReturn($deletions);
149
150
        $entityManager = $this->createMock(EntityManagerInterface::class);
151
        $entityManager->expects(self::any())->method('getUnitOfWork')->willReturn($unitOfWork);
152
153
        $eventArgs = $this->createMock(OnFlushEventArgs::class);
154
        $eventArgs->expects(self::any())->method('getEntityManager')->willReturn($entityManager);
155
156
        return $eventArgs;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $eventArgs returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Doctrine\Common\EventArgs.
Loading history...
157
    }
158
159
    private function getEntityClass() : EventAware
160
    {
161
        return new class() implements EventAware {
162
            use OrderedEventRegistry;
163
164
            public function trigger(DomainEvent $event) : void
165
            {
166
                $this->triggeredA($event);
167
            }
168
        };
169
    }
170
}
171