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:47 queued 46s
created

StubMapBasedTest::getEventArgs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 34
rs 9.568
c 0
b 0
f 0
cc 2
nc 2
nop 1
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_values;
28
29
final class StubMapBasedTest extends TestCase
30
{
31
    /** @var converter[] */
32
    private $conversionMap;
33
34
    /**
35
     * @before
36
     */
37
    public function setUpDependencies() : void
38
    {
39
        $this->conversionMap =
40
            [
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...
41
                'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\FirstDomainEvent'  =>
42
                new FirstOutboxConverter(),
43
44
                'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\SecondDomainEvent' =>
45
                new SecondOutboxConverter(),
46
47
                'Dsantang\DomainEventsDoctrine\Tests\Unit\Outbox\Stub\DomainEvents\ThirdDomainEvent'  =>
48
                new ThirdOutboxConverter(),
49
            ];
50
    }
51
52
    public function testGetDomainsEvents() : void
53
    {
54
        Counter::reset();
55
56
        $eventArgs = $this->getEventArgs();
57
58
        self::assertEquals(3, Counter::getNext());
59
60
        $mapBasedEventsHandler = new StubMapBased(new OutboxSubClass(), $this->conversionMap);
61
62
        $eventsResult = $mapBasedEventsHandler->getDomainEvents($eventArgs);
63
64
        self::assertEquals(0, Counter::getNext());
65
66
        $eventsExpected = [new FirstDomainEvent(), new SecondDomainEvent(), new ThirdDomainEvent()];
67
68
        $this->assertEquals(array_values($eventsResult), array_values($eventsExpected));
69
    }
70
71
    public function testPersist() : void
72
    {
73
        $eventArgs      = $this->getEventArgs();
74
        $outboxSubClass = new OutboxSubClass();
75
76
        $mapBasedEventsHandler = new StubMapBased($outboxSubClass, $this->conversionMap);
77
        $mapBasedEventsHandler->getDomainEvents($eventArgs);
78
79
        $outboxEvents = [
80
            new ThirdOutboxEntry(new ThirdDomainEvent()),
81
            new FirstOutboxEntry(new FirstDomainEvent()),
82
            new SecondOutboxEntry(new SecondDomainEvent()),
83
        ];
84
85
        $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

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