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

MapBasedTest::testGetDomainsEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
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\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
119
        $this->mapBasedEventsHandler->persist(
120
            $eventArgs->getEntityManager(),
121
            ...$outboxEvents
122
        );
123
    }
124
125
    private function getEventArgs() : EventArgs
126
    {
127
        $entity1 = $this->getEntityClass();
128
        $entity2 = $this->getEntityClass();
129
        $entity3 = $this->getEntityClass();
130
131
        $domainEvent1 = new FirstDomainEvent();
132
        $domainEvent2 = new SecondDomainEvent();
133
        $domainEvent3 = new ThirdDomainEvent();
134
135
        $entity1->trigger($domainEvent1);
136
        $entity2->trigger($domainEvent2);
137
        $entity3->trigger($domainEvent3);
138
139
        $unitOfWork = $this->createMock(UnitOfWork::class);
140
        $unitOfWork->expects(self::once())->method('getScheduledEntityInsertions')->willReturn([$entity1]);
141
        $unitOfWork->expects(self::once())->method('getScheduledEntityUpdates')->willReturn([]);
142
        $unitOfWork->expects(self::once())->method('getScheduledEntityDeletions')->willReturn([$entity2, $entity3]);
143
144
        $entityManager = $this->createMock(EntityManagerInterface::class);
145
        $entityManager->expects(self::any())->method('getUnitOfWork')->willReturn($unitOfWork);
146
147
        $eventArgs = $this->createMock(OnFlushEventArgs::class);
148
        $eventArgs->expects(self::any())->method('getEntityManager')->willReturn($entityManager);
149
150
        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...
151
    }
152
153
    private function getEntityClass() : EventAware
154
    {
155
        return new class() implements EventAware {
156
            use OrderedEventRegistry;
157
158
            public function trigger(DomainEvent $event) : void
159
            {
160
                $this->triggeredA($event);
161
            }
162
        };
163
    }
164
}
165