StatusesControllerTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UserStatus\Tests\Controller;
  8. use OCA\UserStatus\Controller\StatusesController;
  9. use OCA\UserStatus\Db\UserStatus;
  10. use OCA\UserStatus\Service\StatusService;
  11. use OCP\AppFramework\Db\DoesNotExistException;
  12. use OCP\AppFramework\OCS\OCSNotFoundException;
  13. use OCP\IRequest;
  14. use Test\TestCase;
  15. class StatusesControllerTest extends TestCase {
  16. /** @var StatusService|\PHPUnit\Framework\MockObject\MockObject */
  17. private $service;
  18. /** @var StatusesController */
  19. private $controller;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $request = $this->createMock(IRequest::class);
  23. $this->service = $this->createMock(StatusService::class);
  24. $this->controller = new StatusesController('user_status', $request, $this->service);
  25. }
  26. public function testFindAll(): void {
  27. $userStatus = $this->getUserStatus();
  28. $this->service->expects($this->once())
  29. ->method('findAll')
  30. ->with(20, 40)
  31. ->willReturn([$userStatus]);
  32. $response = $this->controller->findAll(20, 40);
  33. $this->assertEquals([[
  34. 'userId' => 'john.doe',
  35. 'status' => 'offline',
  36. 'icon' => '🏝',
  37. 'message' => 'On vacation',
  38. 'clearAt' => 60000,
  39. ]], $response->getData());
  40. }
  41. public function testFind(): void {
  42. $userStatus = $this->getUserStatus();
  43. $this->service->expects($this->once())
  44. ->method('findByUserId')
  45. ->with('john.doe')
  46. ->willReturn($userStatus);
  47. $response = $this->controller->find('john.doe');
  48. $this->assertEquals([
  49. 'userId' => 'john.doe',
  50. 'status' => 'offline',
  51. 'icon' => '🏝',
  52. 'message' => 'On vacation',
  53. 'clearAt' => 60000,
  54. ], $response->getData());
  55. }
  56. public function testFindDoesNotExist(): void {
  57. $this->service->expects($this->once())
  58. ->method('findByUserId')
  59. ->with('john.doe')
  60. ->willThrowException(new DoesNotExistException(''));
  61. $this->expectException(OCSNotFoundException::class);
  62. $this->expectExceptionMessage('No status for the requested userId');
  63. $this->controller->find('john.doe');
  64. }
  65. private function getUserStatus(): UserStatus {
  66. $userStatus = new UserStatus();
  67. $userStatus->setId(1337);
  68. $userStatus->setUserId('john.doe');
  69. $userStatus->setStatus('invisible');
  70. $userStatus->setStatusTimestamp(5000);
  71. $userStatus->setIsUserDefined(true);
  72. $userStatus->setCustomIcon('🏝');
  73. $userStatus->setCustomMessage('On vacation');
  74. $userStatus->setClearAt(60000);
  75. return $userStatus;
  76. }
  77. }