FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ClearingEventProcessorTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
20 
25 use Mockery as M;
26 
27 class ClearingEventProcessorTest extends \PHPUnit\Framework\TestCase
28 {
29  private $itemId = 12;
30  private $userId = 5;
31  private $groupId = 2;
32  private $eventType = ClearingEventTypes::USER;
33  private $timestamp;
35  private $addedEvent;
37  private $removedEvent;
38  private $addedName = "<added>";
39  private $addedId = 400;
40  private $removedName = "<removed>";
41  private $removedId = 399;
42 
44  protected $clearingEventProcessor;
45 
46  protected function setUp()
47  {
48  $this->timestamp = time();
49  $this->clearingEventProcessor = new ClearingEventProcessor();
50 
51  $this->addedLicense = M::mock(ClearingLicense::class);
52  $this->addedLicense->shouldReceive("getShortName")->withNoArgs()->andReturn($this->addedName);
53  $this->addedLicense->shouldReceive("getId")->withNoArgs()->andReturn($this->addedId);
54 
55  $this->addedEvent = M::mock(ClearingEvent::class);
56  $this->addedEvent->shouldReceive("getLicenseShortName")->withNoArgs()->andReturn($this->addedName);
57  $this->addedEvent->shouldReceive("getLicenseId")->withNoArgs()->andReturn($this->addedId);
58  $this->addedEvent->shouldReceive("getClearingLicense")->withNoArgs()->andReturn($this->addedLicense);
59  $this->addedEvent->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp);
60  $this->addedEvent->shouldReceive("isRemoved")->withNoArgs()->andReturn(false);
61 
62  $this->removedLicense = M::mock(ClearingLicense::class);
63  $this->removedLicense->shouldReceive("getShortName")->withNoArgs()->andReturn($this->removedName);
64  $this->removedLicense->shouldReceive("getId")->withNoArgs()->andReturn($this->removedId);
65  $this->removedEvent = M::mock(ClearingEvent::class);
66  $this->removedEvent->shouldReceive("getLicenseShortName")->withNoArgs()->andReturn($this->removedName);
67  $this->removedEvent->shouldReceive("getLicenseId")->withNoArgs()->andReturn($this->removedId);
68  $this->removedEvent->shouldReceive("getClearingLicense")->withNoArgs()->andReturn($this->removedLicense);
69  $this->removedEvent->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp);
70  $this->removedEvent->shouldReceive("isRemoved")->withNoArgs()->andReturn(true);
71  }
72 
73  protected function tearDown()
74  {
75  M::close();
76  }
77 
81  protected function createEvents()
82  {
83  $license = M::mock(ClearingLicense::class);
84 
85  $events = array();
86  $events[] = $this->createEvent($this->timestamp, $license);
87 
88  $eventTimestamp = $this->timestamp-3600;
89  $events[] = $this->createEvent($eventTimestamp, $license);
90 
91  return $events;
92  }
93 
99  protected function createEvent($eventTimestamp, ClearingLicense $clearingLicense)
100  {
101  return new ClearingEvent(1, $this->itemId, $eventTimestamp, $this->userId, $this->groupId, $this->eventType, $clearingLicense);
102  }
103 
104  public function testFilterEffectiveEvents()
105  {
106  $events = array();
107  $licAId = 42;
108  $licBId = 23;
109 
110  $license1 = M::mock(ClearingLicense::class);
111  $license1->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licAId);
112  $events[] = $this->createEvent($this->timestamp, $license1, false);
113 
114  $license2 = M::mock(ClearingLicense::class);
115  $license2->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licBId);
116  $events[] = $this->createEvent($this->timestamp+60, $license2, false);
117 
118  $filteredEvents = $this->clearingEventProcessor->filterEffectiveEvents($events);
119 
120  assertThat($filteredEvents, is(arrayWithSize(2)));
121  assertThat($filteredEvents, hasKeyValuePair($licAId,$events[0]));
122  assertThat($filteredEvents, hasKeyValuePair($licBId,$events[1]));
123  }
124 
125  public function testFilterEffectiveEventsIdenticalEventsOverride()
126  {
127  $events = array();
128  $licId = 42;
129  $licenseRef1 = M::mock(ClearingLicense::class);
130  $licenseRef1->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licId);
131  $events[] = $this->createEvent($this->timestamp, $licenseRef1, false);
132 
133  $licenseRef2 = M::mock(ClearingLicense::class);
134  $licenseRef2->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licId);
135  $events[] = $this->createEvent($this->timestamp+60, $licenseRef2, false);
136 
137  $filteredEvents = $this->clearingEventProcessor->filterEffectiveEvents($events);
138 
139  assertThat($filteredEvents, is(arrayWithSize(1)));
140  assertThat($filteredEvents, hasKeyValuePair($licId,$events[1]));
141  }
142 
143  public function testFilterEffectiveEventsOppositeIdenticalEventsOverwrite()
144  {
145  $events = array();
146  $licId = 42;
147 
148  $licenseRef1 = M::mock(ClearingLicense::class);
149  $licenseRef1->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licId);
150  $events[] = $this->createEvent($this->timestamp, $licenseRef1);
151 
152  $licenseRef2 = M::mock(ClearingLicense::class);
153  $licenseRef2->shouldReceive("getLicenseId")->withNoArgs()->andReturn($licId);
154  $events[] = $this->createEvent($this->timestamp+60, $licenseRef2);
155 
156  $filteredEvents = $this->clearingEventProcessor->filterEffectiveEvents($events);
157 
158  assertThat($filteredEvents, is(arrayWithSize(1)));
159  assertThat($filteredEvents, hasKeyValuePair($licId,$events[1]));
160  }
161 
162  public function testFilterEffectiveEventsOppositeIdenticalEventsOverwriteInOtherOrder()
163  {
164  $events = array();
165 
166  $licenseRef1 = M::mock(ClearingLicense::class);
167  $licenseRef1->shouldReceive("getLicenseId")->withNoArgs()->andReturn("fortyTwo");
168  $events[] = $this->createEvent($this->timestamp, $licenseRef1);
169 
170  $licenseRef2 = M::mock(ClearingLicense::class);
171  $licenseRef2->shouldReceive("getLicenseId")->withNoArgs()->andReturn("fortyTwo");
172  $events[] = $this->createEvent($this->timestamp+60, $licenseRef2);
173 
174  $filteredEvents = $this->clearingEventProcessor->filterEffectiveEvents($events);
175 
176  assertThat($filteredEvents, is(arrayWithSize(1)));
177  assertThat($filteredEvents, hasKeyValuePair('fortyTwo',$events[1]));
178  }
179 
180  public function testFilterEffectiveEventsWithEmptyArray()
181  {
182  assertThat($this->clearingEventProcessor->filterEffectiveEvents(array()), is(emptyArray()));
183  }
184 }
createEvent($eventTimestamp, ClearingLicense $clearingLicense)
Contains business rules for FOSSology.