FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ClearingEventProcessorTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
14 use Mockery as M;
15 
16 class ClearingEventProcessorTest extends \PHPUnit\Framework\TestCase
17 {
18  private $itemId = 12;
19  private $userId = 5;
20  private $groupId = 2;
21  private $eventType = ClearingEventTypes::USER;
22  private $timestamp;
24  private $addedEvent;
26  private $removedEvent;
27  private $addedName = "<added>";
28  private $addedId = 400;
29  private $removedName = "<removed>";
30  private $removedId = 399;
31 
33  protected $clearingEventProcessor;
34 
35  protected function setUp() : void
36  {
37  $this->timestamp = time();
38  $this->clearingEventProcessor = new ClearingEventProcessor();
39 
40  $this->addedLicense = M::mock(ClearingLicense::class);
41  $this->addedLicense->shouldReceive("getShortName")->withNoArgs()->andReturn($this->addedName);
42  $this->addedLicense->shouldReceive("getId")->withNoArgs()->andReturn($this->addedId);
43 
44  $this->addedEvent = M::mock(ClearingEvent::class);
45  $this->addedEvent->shouldReceive("getLicenseShortName")->withNoArgs()->andReturn($this->addedName);
46  $this->addedEvent->shouldReceive("getLicenseId")->withNoArgs()->andReturn($this->addedId);
47  $this->addedEvent->shouldReceive("getClearingLicense")->withNoArgs()->andReturn($this->addedLicense);
48  $this->addedEvent->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp);
49  $this->addedEvent->shouldReceive("isRemoved")->withNoArgs()->andReturn(false);
50 
51  $this->removedLicense = M::mock(ClearingLicense::class);
52  $this->removedLicense->shouldReceive("getShortName")->withNoArgs()->andReturn($this->removedName);
53  $this->removedLicense->shouldReceive("getId")->withNoArgs()->andReturn($this->removedId);
54  $this->removedEvent = M::mock(ClearingEvent::class);
55  $this->removedEvent->shouldReceive("getLicenseShortName")->withNoArgs()->andReturn($this->removedName);
56  $this->removedEvent->shouldReceive("getLicenseId")->withNoArgs()->andReturn($this->removedId);
57  $this->removedEvent->shouldReceive("getClearingLicense")->withNoArgs()->andReturn($this->removedLicense);
58  $this->removedEvent->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp);
59  $this->removedEvent->shouldReceive("isRemoved")->withNoArgs()->andReturn(true);
60  }
61 
62  protected function tearDown() : void
63  {
64  M::close();
65  }
66 
70  protected function createEvents()
71  {
72  $license = M::mock(ClearingLicense::class);
73 
74  $events = array();
75  $events[] = $this->createEvent($this->timestamp, $license);
76 
77  $eventTimestamp = $this->timestamp-3600;
78  $events[] = $this->createEvent($eventTimestamp, $license);
79 
80  return $events;
81  }
82 
88  protected function createEvent($eventTimestamp, ClearingLicense $clearingLicense)
89  {
90  return new ClearingEvent(1, $this->itemId, $eventTimestamp, $this->userId, $this->groupId, $this->eventType, $clearingLicense);
91  }
92 
93  public function testFilterEffectiveEvents()
94  {
95  $events = array();
96  $licAId = 42;
97  $licBId = 23;
98 
99  $license1 = M::mock(ClearingLicense::class);
100  $license1->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licAId);
101  $events[] = $this->createEvent($this->timestamp, $license1, false);
102 
103  $license2 = M::mock(ClearingLicense::class);
104  $license2->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licBId);
105  $events[] = $this->createEvent($this->timestamp+60, $license2, false);
106 
107  $filteredEvents = $this->clearingEventProcessor->filterEffectiveEvents($events);
108 
109  assertThat($filteredEvents, is(arrayWithSize(2)));
110  assertThat($filteredEvents, hasKeyValuePair($licAId,$events[0]));
111  assertThat($filteredEvents, hasKeyValuePair($licBId,$events[1]));
112  }
113 
114  public function testFilterEffectiveEventsIdenticalEventsOverride()
115  {
116  $events = array();
117  $licId = 42;
118  $licenseRef1 = M::mock(ClearingLicense::class);
119  $licenseRef1->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licId);
120  $events[] = $this->createEvent($this->timestamp, $licenseRef1, false);
121 
122  $licenseRef2 = M::mock(ClearingLicense::class);
123  $licenseRef2->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licId);
124  $events[] = $this->createEvent($this->timestamp+60, $licenseRef2, false);
125 
126  $filteredEvents = $this->clearingEventProcessor->filterEffectiveEvents($events);
127 
128  assertThat($filteredEvents, is(arrayWithSize(1)));
129  assertThat($filteredEvents, hasKeyValuePair($licId,$events[1]));
130  }
131 
132  public function testFilterEffectiveEventsOppositeIdenticalEventsOverwrite()
133  {
134  $events = array();
135  $licId = 42;
136 
137  $licenseRef1 = M::mock(ClearingLicense::class);
138  $licenseRef1->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licId);
139  $events[] = $this->createEvent($this->timestamp, $licenseRef1);
140 
141  $licenseRef2 = M::mock(ClearingLicense::class);
142  $licenseRef2->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licId);
143  $events[] = $this->createEvent($this->timestamp+60, $licenseRef2);
144 
145  $filteredEvents = $this->clearingEventProcessor->filterEffectiveEvents($events);
146 
147  assertThat($filteredEvents, is(arrayWithSize(1)));
148  assertThat($filteredEvents, hasKeyValuePair($licId,$events[1]));
149  }
150 
151  public function testFilterEffectiveEventsOppositeIdenticalEventsOverwriteInOtherOrder()
152  {
153  $events = array();
154 
155  $licenseRef1 = M::mock(ClearingLicense::class);
156  $licenseRef1->shouldReceive("getLicenseId")->withNoArgs()->andReturn("fortyTwo");
157  $events[] = $this->createEvent($this->timestamp, $licenseRef1);
158 
159  $licenseRef2 = M::mock(ClearingLicense::class);
160  $licenseRef2->shouldReceive("getLicenseId")->withNoArgs()->andReturn("fortyTwo");
161  $events[] = $this->createEvent($this->timestamp+60, $licenseRef2);
162 
163  $filteredEvents = $this->clearingEventProcessor->filterEffectiveEvents($events);
164 
165  assertThat($filteredEvents, is(arrayWithSize(1)));
166  assertThat($filteredEvents, hasKeyValuePair('fortyTwo',$events[1]));
167  }
168 
169  public function testFilterEffectiveEventsWithEmptyArray()
170  {
171  assertThat($this->clearingEventProcessor->filterEffectiveEvents(array()), is(emptyArray()));
172  }
173 }
createEvent($eventTimestamp, ClearingLicense $clearingLicense)
Contains business rules for FOSSology.