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:38
created

MapBasedTest::testPersist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 23
rs 9.7666
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 InvalidArgumentException;
27
use PHPUnit\Framework\TestCase;
28
use stdClass;
29
use function array_pop;
30
use function array_values;
31
32
final class MapBasedTest extends TestCase
33
{
34
    /** @var converter[] */
35
    private $conversionMap;
36
37
    /** @var StubMapBased */
38
    private $mapBasedEventsHandler;
39
40
    /**
41
     * @before
42
     */
43
    public function setUpDependencies() : void
44
    {
45
        $this->conversionMap =
46
            [
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...
47
                'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\FirstDomainEvent'  =>
48
                new FirstOutboxConverter(),
49
50
                'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\SecondDomainEvent' =>
51
                new SecondOutboxConverter(),
52
53
                'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\ThirdDomainEvent'  =>
54
                new ThirdOutboxConverter(),
55
            ];
56
    }
57
58
    public function testDealWithInvalidConversionMap() : void
59
    {
60
        $this->expectException(InvalidArgumentException::class);
61
62
        $this->conversionMap = [
0 ignored issues
show
Documentation Bug introduced by
It seems like array('Dsantang\DomainEv...ent' => new stdClass()) of type array<string,stdClass> 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...
63
            'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\ThirdDomainEvent'  =>
64
                new stdClass(),
65
        ];
66
67
        $this->mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap);
68
    }
69
70
    public function testGetDomainsEvents() : void
71
    {
72
        Counter::reset();
73
74
        $eventArgs = $this->getEventArgs();
75
76
        self::assertEquals(3, Counter::getNext());
77
78
        $this->mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap);
79
80
        $eventsResult = $this->mapBasedEventsHandler->getDomainEvents($eventArgs);
81
82
        self::assertEquals(0, Counter::getNext());
83
84
        $eventsExpected = [new FirstDomainEvent(), new SecondDomainEvent(), new ThirdDomainEvent()];
85
86
        $this->assertEquals(array_values($eventsResult), array_values($eventsExpected));
87
    }
88
89
    public function testConvert() : void
90
    {
91
        $this->mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap);
92
93
        $domainEvents = [new ThirdDomainEvent(), new FirstDomainEvent(), new SecondDomainEvent()];
94
95
        $returnedOutboxEvents = $this->mapBasedEventsHandler->convert(...$domainEvents);
96
97
        $expectedOutboxEvents = [
98
            (new ThirdOutboxConverter())->convert(new ThirdDomainEvent()),
99
            (new FirstOutboxConverter())->convert(new FirstDomainEvent()),
100
            (new SecondOutboxConverter())->convert(new SecondDomainEvent()),
101
        ];
102
103
        self::assertEquals($expectedOutboxEvents, $returnedOutboxEvents);
104
    }
105
106
    public function testConvertWithANonOutboxEntryDomainEvent() : void
107
    {
108
        array_pop($this->conversionMap);
109
        $this->mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap);
110
111
        $domainEvents = [new FirstDomainEvent(), new SecondDomainEvent(), new ThirdDomainEvent()];
112
113
        $returnedOutboxEvents = $this->mapBasedEventsHandler->convert(...$domainEvents);
114
115
        $expectedOutboxEvents = [
116
            (new FirstOutboxConverter())->convert(new FirstDomainEvent()),
117
            (new SecondOutboxConverter())->convert(new SecondDomainEvent()),
118
        ];
119
120
        self::assertEquals($expectedOutboxEvents, $returnedOutboxEvents);
121
    }
122
123
    public function testPersist() : void
124
    {
125
        $eventArgs      = $this->getEventArgs();
126
        $outboxSubClass = new OutboxSubClass();
127
128
        $this->mapBasedEventsHandler = new StubMapBased($outboxSubClass, $this->conversionMap);
129
        $this->mapBasedEventsHandler->getDomainEvents($eventArgs);
130
131
        $outboxEvents = [
132
            new ThirdOutboxEntry(new ThirdDomainEvent()),
133
            new FirstOutboxEntry(new FirstDomainEvent()),
134
            new SecondOutboxEntry(new SecondDomainEvent()),
135
        ];
136
137
        $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

137
        $eventArgs->/** @scrutinizer ignore-call */ 
138
                    getEntityManager()->getUnitOfWork()->expects(self::once())
Loading history...
138
            ->method('computeChangeSets');
139
140
        $eventArgs->getEntityManager()->expects(self::exactly(3))
141
            ->method('persist');
142
143
        $this->mapBasedEventsHandler->persist(
144
            $eventArgs->getEntityManager(),
145
            ...$outboxEvents
146
        );
147
    }
148
149
    private function getEventArgs() : EventArgs
150
    {
151
        $entity1 = $this->getEntityClass();
152
        $entity2 = $this->getEntityClass();
153
        $entity3 = $this->getEntityClass();
154
155
        $domainEvent1 = new FirstDomainEvent();
156
        $domainEvent2 = new SecondDomainEvent();
157
        $domainEvent3 = new ThirdDomainEvent();
158
159
        $entity1->trigger($domainEvent1);
160
        $entity2->trigger($domainEvent2);
161
        $entity3->trigger($domainEvent3);
162
163
        $unitOfWork = $this->createMock(UnitOfWork::class);
164
        $unitOfWork->expects(self::once())->method('getScheduledEntityInsertions')->willReturn([]);
165
        $unitOfWork->expects(self::once())->method('getScheduledEntityUpdates')->willReturn([$entity3]);
166
        $unitOfWork->expects(self::once())->method('getScheduledEntityDeletions')->willReturn([$entity2, $entity1]);
167
168
        $entityManager = $this->createMock(EntityManagerInterface::class);
169
        $entityManager->expects(self::any())->method('getUnitOfWork')->willReturn($unitOfWork);
170
171
        $eventArgs = $this->createMock(OnFlushEventArgs::class);
172
        $eventArgs->expects(self::any())->method('getEntityManager')->willReturn($entityManager);
173
174
        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...
175
    }
176
177
    private function getEntityClass() : EventAware
178
    {
179
        return new class() implements EventAware {
180
            use OrderedEventRegistry;
181
182
            public function trigger(DomainEvent $event) : void
183
            {
184
                $this->triggeredA($event);
185
            }
186
        };
187
    }
188
}
189