diff options
author | Joas Schilling <coding@schilljs.com> | 2017-02-13 13:31:17 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2017-02-13 13:37:02 +0300 |
commit | 4f9b051463c2c32e3c79c46c2bab0e7cd05f5554 (patch) | |
tree | c2fa6cecbfa5af209b9b04d37244759d007b79a3 | |
parent | c619d51466bb2489b6ddf1b9d82d01e1c4be1343 (diff) |
Add tests for the controller
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | tests/Controller/APIControllerTest.php | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/tests/Controller/APIControllerTest.php b/tests/Controller/APIControllerTest.php new file mode 100644 index 0000000..2dbf726 --- /dev/null +++ b/tests/Controller/APIControllerTest.php @@ -0,0 +1,168 @@ +<?php +/** + * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\AdminNotifications\Tests\Controller; + +use OCA\AdminNotifications\AppInfo\Application; +use OCA\AdminNotifications\Controller\APIController; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IRequest; +use OCP\IUser; +use OCP\IUserManager; +use OCP\Notification\IManager; +use OCP\Notification\INotification; + +/** + * Class APIControllerTest + * + * @package OCA\AdminNotifications\Tests\Controller + * @group DB + */ +class APIControllerTest extends \Test\TestCase { + /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */ + protected $timeFactory; + /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $userManager; + /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $notificationManager; + /** @var APIController */ + protected $controller; + + protected function setUp() { + parent::setUp(); + + /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject $request */ + $request = $this->createMock(IRequest::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->notificationManager = $this->createMock(IManager::class); + + $this->controller = new APIController( + Application::APP_ID, + $request, + $this->timeFactory, + $this->userManager, + $this->notificationManager + ); + } + + public function dataGenerateNotification() { + return [ + ['user', '', '', false, null, false, null, 123, null, Http::STATUS_NOT_FOUND], + ['user', '', '', false, null, false, 'user', 123, null, Http::STATUS_BAD_REQUEST], + ['user', str_repeat('a', 256), '', false, null, false, 'user', 123, null, Http::STATUS_BAD_REQUEST], + ['user', 'short', '', true, false, false, 'user', 123, '7b', Http::STATUS_OK], + ['user', 'short', str_repeat('a', 4001), false, null, false, 'user', 123, null, Http::STATUS_BAD_REQUEST], + ['user', 'short', str_repeat('a', 4000), true, false, true, 'user', 123, '7b', Http::STATUS_OK], + ['user', 'short', 'long', true, true, true, 'user', 123, '7b', Http::STATUS_INTERNAL_SERVER_ERROR], + ]; + } + + /** + * @dataProvider dataGenerateNotification + * @param string $userId + * @param string $short + * @param string $long + * @param bool $createNotification + * @param bool $notifyThrows + * @param bool $validLong + * @param string|null $user + * @param int $time + * @param string|null $hexTime + * @param int $statusCode + */ + public function testGenerateNotification($userId, $short, $long, $createNotification, $notifyThrows, $validLong, $user, $time, $hexTime, $statusCode) { + if ($user !== null) { + $u = $this->createMock(IUser::class); + $u->expects($createNotification ? $this->once() : $this->never()) + ->method('getUID') + ->willReturn($user); + } else { + $u = null; + } + $this->userManager->expects($this->any()) + ->method('get') + ->with($userId) + ->willReturn($u); + + $this->timeFactory->expects($hexTime === null ? $this->never() : $this->once()) + ->method('getTime') + ->willReturn($time); + + if ($createNotification) { + $n = $this->createMock(INotification::class); + $n->expects($this->once()) + ->method('setApp') + ->with(Application::APP_ID) + ->willReturnSelf(); + $n->expects($this->once()) + ->method('setUser') + ->with($user) + ->willReturnSelf(); + $n->expects($this->once()) + ->method('setDateTime') + ->willReturnSelf(); + $n->expects($this->once()) + ->method('setObject') + ->with(Application::APP_ID, $hexTime) + ->willReturnSelf(); + $n->expects($this->once()) + ->method('setSubject') + ->with('ocs', [$short]) + ->willReturnSelf(); + if ($validLong) { + $n->expects($this->once()) + ->method('setMessage') + ->with('ocs', [$long]) + ->willReturnSelf(); + } else { + $n->expects($this->never()) + ->method('setMessage'); + } + + $this->notificationManager->expects($this->once()) + ->method('createNotification') + ->willReturn($n); + + if ($notifyThrows === null) { + $this->notificationManager->expects($this->never()) + ->method('notify'); + } else if ($notifyThrows === false) { + $this->notificationManager->expects($this->once()) + ->method('notify') + ->with($n); + } else if ($notifyThrows === true) { + $this->notificationManager->expects($this->once()) + ->method('notify') + ->willThrowException(new \InvalidArgumentException()); + } + } + + $response = $this->controller->generateNotification($userId, $short, $long); + + $this->assertInstanceOf(DataResponse::class, $response); + $this->assertSame($statusCode, $response->getStatus()); + } +} |