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
02:12
created

MapBasedTest::getEventArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 26
rs 9.6666
c 0
b 0
f 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\TestCase;
27
use function array_pop;
28
29
final class MapBasedTest extends TestCase
30
{
31
    /** @var converter[] */
32
    private $conversionMap;
33
34
    /** @var StubMapBased */
35
    private $mapBasedEventsHandler;
36
37
    /**
38
     * @before
39
     */
40
    public function setUpDependencies() : void
41
    {
42
        $this->conversionMap =
43
            [
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...
44
                'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\FirstDomainEvent'  =>
45
                new FirstOutboxConverter(),
46
47
                'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\SecondDomainEvent' =>
48
                new SecondOutboxConverter(),
49
50
                'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\ThirdDomainEvent'  =>
51
                new ThirdOutboxConverter(),
52
            ];
53
    }
54
55
    public function testGetDomainsEvents() : void
56
    {
57
        Counter::reset();
58
59
        $eventArgs = $this->getEventArgs();
60
61
        self::assertEquals(3, Counter::getNext());
62
63
        $this->mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap);
64
65
        $this->mapBasedEventsHandler->getDomainsEvents($eventArgs);
66
    }
67
68
    public function testConvert() : void
69
    {
70
        $this->mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap);
71
72
        $domainEvents = [new ThirdDomainEvent(), new FirstDomainEvent(), new SecondDomainEvent()];
73
74
        $returnedOutboxEvents = $this->mapBasedEventsHandler->convert(...$domainEvents);
75
76
        $expectedOutboxEvents = [
77
            (new ThirdOutboxConverter())->convert(new ThirdDomainEvent()),
78
            (new FirstOutboxConverter())->convert(new FirstDomainEvent()),
79
            (new SecondOutboxConverter())->convert(new SecondDomainEvent()),
80
        ];
81
82
        self::assertEquals($expectedOutboxEvents, $returnedOutboxEvents);
83
    }
84
85
    public function testConvertWithANonOutboxEntryDomainEvent() : void
86
    {
87
        array_pop($this->conversionMap);
88
        $this->mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap);
89
90
        $domainEvents = [new FirstDomainEvent(), new SecondDomainEvent(), new ThirdDomainEvent()];
91
92
        $returnedOutboxEvents = $this->mapBasedEventsHandler->convert(...$domainEvents);
93
94
        $expectedOutboxEvents = [
95
            (new FirstOutboxConverter())->convert(new FirstDomainEvent()),
96
            (new SecondOutboxConverter())->convert(new SecondDomainEvent()),
97
        ];
98
99
        self::assertEquals($expectedOutboxEvents, $returnedOutboxEvents);
100
    }
101
102
    public function testPersist() : void
103
    {
104
        $eventArgs      = $this->getEventArgs();
105
        $outboxSubClass = new OutboxSubClass();
106
107
        $this->mapBasedEventsHandler = new StubMapBased($outboxSubClass, $this->conversionMap);
108
        $this->mapBasedEventsHandler->getDomainsEvents($eventArgs);
109
110
        $outboxEvents = [
111
            new ThirdOutboxEntry(new ThirdDomainEvent()),
112
            new FirstOutboxEntry(new FirstDomainEvent()),
113
            new SecondOutboxEntry(new SecondDomainEvent()),
114
        ];
115
116
        $eventArgs->getEntityManager()->expects(self::exactly(3))
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

116
        $eventArgs->/** @scrutinizer ignore-call */ 
117
                    getEntityManager()->expects(self::exactly(3))
Loading history...
117
            ->method('persist')
118
            ->withConsecutive(
119
                [$outboxSubClass->withOutboxEntry($outboxEvents[0])],
120
                [$outboxSubClass->withOutboxEntry($outboxEvents[1])],
121
                [$outboxSubClass->withOutboxEntry($outboxEvents[2])]
122
            );
123
124
        $this->mapBasedEventsHandler->persist(
125
            $eventArgs->getEntityManager(),
126
            ...$outboxEvents
127
        );
128
    }
129
130
    private function getEventArgs() : EventArgs
131
    {
132
        $entity1 = $this->getEntityClass();
133
        $entity2 = $this->getEntityClass();
134
        $entity3 = $this->getEntityClass();
135
136
        $domainEvent1 = new FirstDomainEvent();
137
        $domainEvent2 = new SecondDomainEvent();
138
        $domainEvent3 = new ThirdDomainEvent();
139
140
        $entity1->trigger($domainEvent1);
141
        $entity2->trigger($domainEvent2);
142
        $entity3->trigger($domainEvent3);
143
144
        $unitOfWork = $this->createMock(UnitOfWork::class);
145
        $unitOfWork->expects(self::once())->method('getScheduledEntityInsertions')->willReturn([$entity1]);
146
        $unitOfWork->expects(self::once())->method('getScheduledEntityUpdates')->willReturn([]);
147
        $unitOfWork->expects(self::once())->method('getScheduledEntityDeletions')->willReturn([$entity2, $entity3]);
148
149
        $entityManager = $this->createMock(EntityManagerInterface::class);
150
        $entityManager->expects(self::any())->method('getUnitOfWork')->willReturn($unitOfWork);
151
152
        $eventArgs = $this->createMock(OnFlushEventArgs::class);
153
        $eventArgs->expects(self::any())->method('getEntityManager')->willReturn($entityManager);
154
155
        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...
156
    }
157
158
    private function getEntityClass() : EventAware
159
    {
160
        return new class() implements EventAware {
161
            use OrderedEventRegistry;
162
163
            public function trigger(DomainEvent $event) : void
164
            {
165
                $this->triggeredA($event);
166
            }
167
        };
168
    }
169
}
170