testCanGetLatestMeetingsAttendanceStatusForMember()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
c 0
b 0
f 0
rs 8.439
cc 5
eloc 21
nc 9
nop 0
1
<?php
2
3
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014-2015, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014-2015, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Storgman\Tests\Meetings;
29
30
use Angelov\Storgman\Meetings\Meeting;
31
use Angelov\Storgman\Meetings\MeetingsService;
32
use Angelov\Storgman\Meetings\Reports\MeetingAttendedReport;
33
use Angelov\Storgman\Meetings\Reports\MeetingsAttendanceDetailsForMemberReport;
34
use Angelov\Storgman\Meetings\Reports\MeetingsAttendedByMemberPerMonthReport;
35
use Angelov\Storgman\Meetings\Reports\MeetingsPerMonthReport;
36
use Angelov\Storgman\Meetings\Repositories\MeetingsRepositoryInterface;
37
use Angelov\Storgman\Members\Member;
38
use Angelov\Storgman\Members\Repositories\MembersRepositoryInterface;
39
use Angelov\Storgman\Membership\MembershipService;
40
use Angelov\Storgman\Tests\TestCase;
41
use Carbon\Carbon;
42
use Mockery;
43
44
class MeetingsServiceTest extends TestCase
45
{
46
    /** @var $meetings Mockery\MockInterface */
47
    protected $meetings;
48
49
    /** @var $members Mockery\MockInterface */
50
    protected $members;
51
52
    /** @var $membershipService Mockery\MockInterface */
53
    protected $membershipService;
54
55
    public function setUp()
56
    {
57
        parent::setUp();
58
59
        $this->meetings = Mockery::mock(MeetingsRepositoryInterface::class);
60
        $this->members = Mockery::mock(MembersRepositoryInterface::class);
61
        $this->membershipService = Mockery::mock(MembershipService::class);
62
    }
63
64
    public function testCanCalculateAttendanceDetailsForMember()
65
    {
66
        $member = Mockery::mock(Member::class);
67
        $member->shouldReceive('getJoiningDate')->withNoArgs()
68
            ->andReturn(new Carbon('1 month ago'));
69
70
        $this->meetings->shouldReceive('countAttendanceForMember')->andReturn(5);
71
        $this->meetings->shouldReceive('countMeetingsInPeriod')->andReturn(10);
72
73
        $membershipService = Mockery::mock(MembershipService::class);
74
75
        $service = new MeetingsService($this->meetings, $this->members, $membershipService);
76
77
        $report = $service->calculateAttendanceDetailsForMember($member);
78
79
        $this->assertInstanceOf(MeetingsAttendanceDetailsForMemberReport::class, $report);
80
81
        $this->assertEquals(5, $report->getAttended());
82
        $this->assertEquals(5, $report->getMissed());
83
        $this->assertEquals(10, $report->getTotal());
84
        $this->assertEquals(50, $report->getRate());
85
    }
86
87
    public function testCanGetLatestMeetingsAttendanceStatusForMember()
88
    {
89
        $member = Mockery::mock(Member::class);
90
        $meetings = [];
91
92
        $numOfMeetings = 10;
93
94
        $membershipService = Mockery::mock(MembershipService::class);
95
96
        for ($i=0; $i<$numOfMeetings; $i++) {
97
            $meeting = Mockery::mock(Meeting::class);
98
            $attendance = ($i < 5) ? true : false;
99
100
            $meeting->shouldReceive('wasAttendedBy')->andReturn($attendance);
101
102
            $meetings[] = $meeting;
103
        }
104
105
        $this->meetings->shouldReceive('latest')->andReturn($meetings);
106
107
        $service = new MeetingsService($this->meetings, $this->members, $membershipService);
108
109
        $reports = $service->latestMeetingsAttendanceStatusForMember($member);
110
111
        $this->assertTrue(is_array($reports));
112
        $this->assertEquals(count($reports), 10);
113
114
        for ($i=0; $i<$numOfMeetings; $i++) {
115
            $attendance = ($i < 5) ? true : false;
116
117
            $this->assertInstanceOf(MeetingAttendedReport::class, $reports[$i]);
118
            $this->assertEquals($member, $reports[$i]->getMember());
119
            $this->assertEquals($meetings[$i], $reports[$i]->getMeeting());
120
121
            $this->assertEquals($reports[$i]->getAttended(), $attendance);
122
        }
123
    }
124
125
    public function testCanCalculateMonthlyAttendanceDetailsForMember()
126
    {
127
        $member = Mockery::mock(Member::class);
128
        $membershipService = Mockery::mock(MembershipService::class);
129
130
        $meetingsPerMonthReport = Mockery::mock(MeetingsPerMonthReport::class);
131
132
        $monthTitles = ["Jan", "Feb", "Mar"];
133
        $monthValues = [10, 5, 10];
134
135
        $meetingsPerMonthReport->shouldReceive("getMonthsTitles")->andReturn($monthTitles);
136
        $meetingsPerMonthReport->shouldReceive("getMonthsValues")->andReturn($monthValues);
137
138
        $this->meetings->shouldReceive("countMeetingsPerMonth")->andReturn($meetingsPerMonthReport);
139
140
        $attendedMeetingsPerMonthReport = Mockery::mock(MeetingsPerMonthReport::class);
141
142
        $attendedMeetingsPerMonthReport->shouldReceive("getMonthsValues")->andReturn($monthValues);
143
144
        $this->meetings->shouldReceive("countAttendedMeetingsByMemberPerMonth")->andReturn($attendedMeetingsPerMonthReport);
145
146
        $service = new MeetingsService($this->meetings, $this->members, $membershipService);
147
148
        $report = $service->calculateMonthlyAttendanceDetailsForMember($member);
149
150
        $this->assertInstanceOf(MeetingsAttendedByMemberPerMonthReport::class, $report);
151
152
        $this->assertEquals($report->getAttended(), $monthValues);
153
        $this->assertEquals($report->getTotal(), $monthValues);
154
        $this->assertEquals($report->getMonths(), $monthTitles);
155
    }
156
157
    public function testCanParseValidAttendantsIds()
158
    {
159
        $service = new MeetingsService($this->meetings, $this->members, $this->membershipService);
160
161
        $attendantIds = "13|14|1|2|3|";
162
163
        $parsed = $service->parseAttendantsIds($attendantIds);
164
165
        $this->assertEquals([13, 14, 1, 2, 3], $parsed);
166
    }
167
168
    public function testShouldIgnoreInvalidIdsWhenParsingAttendantsIds()
169
    {
170
        $service = new MeetingsService($this->meetings, $this->members, $this->membershipService);
171
172
        $attendantIds = "asd";
173
174
        $parsed = $service->parseAttendantsIds($attendantIds);
175
176
        $this->assertEquals([], $parsed);
177
    }
178
179
    public function testCanPrepareAttendantsIds()
180
    {
181
        $service = new MeetingsService($this->meetings, $this->members, $this->membershipService);
182
        $members = [];
183
184
        for ($i=0; $i<3; $i++) {
185
            $member = Mockery::mock(Member::class);
186
            $member->shouldReceive("getId")->andReturn($i);
187
188
            $members[] = $member;
189
        }
190
191
        $str = $service->prepareAttendantsIds($members);
192
193
        $this->assertEquals("|0|1|2|", $str);
194
    }
195
196
    public function testCanPrepareOnlyMemberObjects()
197
    {
198
        $service = new MeetingsService($this->meetings, $this->members, $this->membershipService);
199
        $members = [1, 2];
200
201
        $this->setExpectedException(\InvalidArgumentException::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
202
203
        $service->prepareAttendantsIds($members);
0 ignored issues
show
Documentation introduced by
$members is of type array<integer,integer,{"...nteger","1":"integer"}>, but the function expects a array<integer,object<Ang...orgman\Members\Member>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
204
    }
205
}
206