Skip to content

Commit 478c16d

Browse files
committed
Add tests for new Listeners
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
1 parent 3f1f7e0 commit 478c16d

File tree

5 files changed

+360
-0
lines changed

5 files changed

+360
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* @copyright 2021 Thomas Citharel <nextcloud@tcit.fr>
4+
*
5+
* @author Thomas Citharel <nextcloud@tcit.fr>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program 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 Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
namespace OCA\DAV\Tests\unit\CalDAV\Listeners;
24+
25+
use OCA\DAV\CalDAV\Activity\Backend;
26+
use OCA\DAV\Events\CalendarPublishedEvent;
27+
use OCA\DAV\Listener\CalendarPublicationListener;
28+
use OCP\EventDispatcher\Event;
29+
use PHPUnit\Framework\MockObject\MockObject;
30+
use Psr\Log\LoggerInterface;
31+
use Test\TestCase;
32+
33+
class CalendarPublicationListenerTest extends TestCase {
34+
35+
/** @var Backend|MockObject */
36+
private $activityBackend;
37+
38+
/** @var LoggerInterface|MockObject */
39+
private $logger;
40+
41+
/** @var CalendarPublicationListener */
42+
private $calendarPublicationListener;
43+
44+
/** @var CalendarPublishedEvent|MockObject */
45+
private $event;
46+
47+
protected function setUp(): void {
48+
parent::setUp();
49+
50+
$this->activityBackend = $this->createMock(Backend::class);
51+
$this->logger = $this->createMock(LoggerInterface::class);
52+
$this->event = $this->createMock(CalendarPublishedEvent::class);
53+
$this->calendarPublicationListener = new CalendarPublicationListener($this->activityBackend, $this->logger);
54+
}
55+
56+
public function testInvalidEvent(): void {
57+
$this->activityBackend->expects($this->never())->method('onCalendarPublication');
58+
$this->logger->expects($this->never())->method('debug');
59+
$this->calendarPublicationListener->handle(new Event());
60+
}
61+
62+
public function testEvent(): void {
63+
$this->event->expects($this->once())->method('getCalendarData')->with()->willReturn([]);
64+
$this->activityBackend->expects($this->once())->method('onCalendarPublication')->with([], true);
65+
$this->logger->expects($this->once())->method('debug');
66+
$this->calendarPublicationListener->handle($this->event);
67+
}
68+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* @copyright 2021 Thomas Citharel <nextcloud@tcit.fr>
4+
*
5+
* @author Thomas Citharel <nextcloud@tcit.fr>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program 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 Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
namespace OCA\DAV\Tests\unit\CalDAV\Listeners;
24+
25+
use OCA\DAV\CalDAV\Activity\Backend;
26+
use OCA\DAV\Events\CalendarShareUpdatedEvent;
27+
use OCA\DAV\Listener\CalendarShareUpdateListener;
28+
use OCP\EventDispatcher\Event;
29+
use PHPUnit\Framework\MockObject\MockObject;
30+
use Psr\Log\LoggerInterface;
31+
use Test\TestCase;
32+
33+
class CalendarShareUpdateListenerTest extends TestCase {
34+
35+
/** @var Backend|MockObject */
36+
private $activityBackend;
37+
38+
/** @var LoggerInterface|MockObject */
39+
private $logger;
40+
41+
/** @var CalendarShareUpdateListener */
42+
private $calendarPublicationListener;
43+
44+
/** @var CalendarShareUpdatedEvent|MockObject */
45+
private $event;
46+
47+
protected function setUp(): void {
48+
parent::setUp();
49+
50+
$this->activityBackend = $this->createMock(Backend::class);
51+
$this->logger = $this->createMock(LoggerInterface::class);
52+
$this->event = $this->createMock(CalendarShareUpdatedEvent::class);
53+
$this->calendarPublicationListener = new CalendarShareUpdateListener($this->activityBackend, $this->logger);
54+
}
55+
56+
public function testInvalidEvent(): void {
57+
$this->activityBackend->expects($this->never())->method('onCalendarUpdateShares');
58+
$this->logger->expects($this->never())->method('debug');
59+
$this->calendarPublicationListener->handle(new Event());
60+
}
61+
62+
public function testEvent(): void {
63+
$this->event->expects($this->once())->method('getCalendarData')->with()->willReturn([]);
64+
$this->event->expects($this->once())->method('getOldShares')->with()->willReturn([]);
65+
$this->event->expects($this->once())->method('getAdded')->with()->willReturn([]);
66+
$this->event->expects($this->once())->method('getRemoved')->with()->willReturn([]);
67+
$this->activityBackend->expects($this->once())->method('onCalendarUpdateShares')->with([], [], [], []);
68+
$this->logger->expects($this->once())->method('debug');
69+
$this->calendarPublicationListener->handle($this->event);
70+
}
71+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* @copyright 2021 Thomas Citharel <nextcloud@tcit.fr>
4+
*
5+
* @author Thomas Citharel <nextcloud@tcit.fr>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program 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 Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
namespace OCA\DAV\Tests\unit\CalDAV\Listeners;
24+
25+
use OCA\DAV\CalDAV\Activity\Backend;
26+
use OCA\DAV\Events\CalendarUnpublishedEvent;
27+
use OCA\DAV\Listener\CalendarUnpublicationListener;
28+
use OCP\EventDispatcher\Event;
29+
use PHPUnit\Framework\MockObject\MockObject;
30+
use Psr\Log\LoggerInterface;
31+
use Test\TestCase;
32+
33+
class CalendarUnpublicationListenerTest extends TestCase {
34+
35+
/** @var Backend|MockObject */
36+
private $activityBackend;
37+
38+
/** @var LoggerInterface|MockObject */
39+
private $logger;
40+
41+
/** @var CalendarUnpublicationListener */
42+
private $calendarPublicationListener;
43+
44+
/** @var CalendarUnpublishedEvent|MockObject */
45+
private $event;
46+
47+
protected function setUp(): void {
48+
parent::setUp();
49+
50+
$this->activityBackend = $this->createMock(Backend::class);
51+
$this->logger = $this->createMock(LoggerInterface::class);
52+
$this->event = $this->createMock(CalendarUnpublishedEvent::class);
53+
$this->calendarPublicationListener = new CalendarUnpublicationListener($this->activityBackend, $this->logger);
54+
}
55+
56+
public function testInvalidEvent(): void {
57+
$this->activityBackend->expects($this->never())->method('onCalendarPublication');
58+
$this->logger->expects($this->never())->method('debug');
59+
$this->calendarPublicationListener->handle(new Event());
60+
}
61+
62+
public function testEvent(): void {
63+
$this->event->expects($this->once())->method('getCalendarData')->with()->willReturn([]);
64+
$this->activityBackend->expects($this->once())->method('onCalendarPublication')->with([], false);
65+
$this->logger->expects($this->once())->method('debug');
66+
$this->calendarPublicationListener->handle($this->event);
67+
}
68+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* @copyright 2021 Thomas Citharel <nextcloud@tcit.fr>
4+
*
5+
* @author Thomas Citharel <nextcloud@tcit.fr>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program 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 Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
namespace OCA\DAV\Tests\unit\CalDAV\Listeners;
24+
25+
use OCA\DAV\BackgroundJob\RefreshWebcalJob;
26+
use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
27+
use OCA\DAV\Events\SubscriptionCreatedEvent;
28+
use OCA\DAV\Listener\SubscriptionCreationListener;
29+
use OCP\BackgroundJob\IJobList;
30+
use OCP\EventDispatcher\Event;
31+
use PHPUnit\Framework\MockObject\MockObject;
32+
use Psr\Log\LoggerInterface;
33+
use Test\TestCase;
34+
35+
class SubscriptionCreationListenerTest extends TestCase {
36+
37+
/** @var RefreshWebcalService|MockObject */
38+
private $refreshWebcalService;
39+
40+
/** @var IJobList|MockObject */
41+
private $jobList;
42+
43+
/** @var LoggerInterface|MockObject */
44+
private $logger;
45+
46+
/** @var SubscriptionCreationListener */
47+
private $calendarPublicationListener;
48+
49+
/** @var SubscriptionCreatedEvent|MockObject */
50+
private $event;
51+
52+
protected function setUp(): void {
53+
parent::setUp();
54+
55+
$this->refreshWebcalService = $this->createMock(RefreshWebcalService::class);
56+
$this->jobList = $this->createMock(IJobList::class);
57+
$this->logger = $this->createMock(LoggerInterface::class);
58+
$this->event = $this->createMock(SubscriptionCreatedEvent::class);
59+
$this->calendarPublicationListener = new SubscriptionCreationListener($this->jobList, $this->refreshWebcalService, $this->logger);
60+
}
61+
62+
public function testInvalidEvent(): void {
63+
$this->refreshWebcalService->expects($this->never())->method('refreshSubscription');
64+
$this->jobList->expects($this->never())->method('add');
65+
$this->logger->expects($this->never())->method('debug');
66+
$this->calendarPublicationListener->handle(new Event());
67+
}
68+
69+
public function testEvent(): void {
70+
$this->event->expects($this->once())->method('getSubscriptionId')->with()->willReturn(5);
71+
$this->event->expects($this->once())->method('getSubscriptionData')->with()->willReturn(['principaluri' => 'principaluri', 'uri' => 'uri']);
72+
$this->refreshWebcalService->expects($this->once())->method('refreshSubscription')->with('principaluri', 'uri');
73+
$this->jobList->expects($this->once())->method('add')->with(RefreshWebcalJob::class, ['principaluri' => 'principaluri', 'uri' => 'uri']);
74+
$this->logger->expects($this->exactly(2))->method('debug');
75+
$this->calendarPublicationListener->handle($this->event);
76+
}
77+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* @copyright 2021 Thomas Citharel <nextcloud@tcit.fr>
4+
*
5+
* @author Thomas Citharel <nextcloud@tcit.fr>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program 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 Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
namespace OCA\DAV\Tests\unit\CalDAV\Listeners;
24+
25+
use OCA\DAV\BackgroundJob\RefreshWebcalJob;
26+
use OCA\DAV\CalDAV\Reminder\Backend;
27+
use OCA\DAV\Events\SubscriptionDeletedEvent;
28+
use OCA\DAV\Listener\SubscriptionDeletionListener;
29+
use OCP\BackgroundJob\IJobList;
30+
use OCP\EventDispatcher\Event;
31+
use PHPUnit\Framework\MockObject\MockObject;
32+
use Psr\Log\LoggerInterface;
33+
use Test\TestCase;
34+
35+
class SubscriptionDeletionListenerTest extends TestCase {
36+
37+
/** @var Backend|MockObject */
38+
private $reminderBackend;
39+
40+
/** @var IJobList|MockObject */
41+
private $jobList;
42+
43+
/** @var LoggerInterface|MockObject */
44+
private $logger;
45+
46+
/** @var SubscriptionDeletionListener */
47+
private $calendarPublicationListener;
48+
49+
/** @var SubscriptionDeletedEvent|MockObject */
50+
private $event;
51+
52+
protected function setUp(): void {
53+
parent::setUp();
54+
55+
$this->reminderBackend = $this->createMock(Backend::class);
56+
$this->jobList = $this->createMock(IJobList::class);
57+
$this->logger = $this->createMock(LoggerInterface::class);
58+
$this->event = $this->createMock(SubscriptionDeletedEvent::class);
59+
$this->calendarPublicationListener = new SubscriptionDeletionListener($this->jobList, $this->reminderBackend, $this->logger);
60+
}
61+
62+
public function testInvalidEvent(): void {
63+
$this->jobList->expects($this->never())->method('remove');
64+
$this->logger->expects($this->never())->method('debug');
65+
$this->calendarPublicationListener->handle(new Event());
66+
}
67+
68+
public function testEvent(): void {
69+
$this->event->expects($this->once())->method('getSubscriptionId')->with()->willReturn(5);
70+
$this->event->expects($this->once())->method('getSubscriptionData')->with()->willReturn(['principaluri' => 'principaluri', 'uri' => 'uri']);
71+
$this->jobList->expects($this->once())->method('remove')->with(RefreshWebcalJob::class, ['principaluri' => 'principaluri', 'uri' => 'uri']);
72+
$this->reminderBackend->expects($this->once())->method('cleanRemindersForCalendar')->with(5);
73+
$this->logger->expects($this->exactly(2))->method('debug');
74+
$this->calendarPublicationListener->handle($this->event);
75+
}
76+
}

0 commit comments

Comments
 (0)