You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatusesControllerTest.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  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, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\UserStatus\Tests\Controller;
  24. use OCA\UserStatus\Controller\StatusesController;
  25. use OCA\UserStatus\Db\UserStatus;
  26. use OCA\UserStatus\Service\StatusService;
  27. use OCP\AppFramework\Db\DoesNotExistException;
  28. use OCP\AppFramework\OCS\OCSNotFoundException;
  29. use OCP\IRequest;
  30. use Test\TestCase;
  31. class StatusesControllerTest extends TestCase {
  32. /** @var StatusService|\PHPUnit\Framework\MockObject\MockObject */
  33. private $service;
  34. /** @var StatusesController */
  35. private $controller;
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $request = $this->createMock(IRequest::class);
  39. $this->service = $this->createMock(StatusService::class);
  40. $this->controller = new StatusesController('user_status', $request, $this->service);
  41. }
  42. public function testFindAll(): void {
  43. $userStatus = $this->getUserStatus();
  44. $this->service->expects($this->once())
  45. ->method('findAll')
  46. ->with(20, 40)
  47. ->willReturn([$userStatus]);
  48. $response = $this->controller->findAll(20, 40);
  49. $this->assertEquals([[
  50. 'userId' => 'john.doe',
  51. 'status' => 'offline',
  52. 'icon' => '🏝',
  53. 'message' => 'On vacation',
  54. 'clearAt' => 60000,
  55. ]], $response->getData());
  56. }
  57. public function testFind(): void {
  58. $userStatus = $this->getUserStatus();
  59. $this->service->expects($this->once())
  60. ->method('findByUserId')
  61. ->with('john.doe')
  62. ->willReturn($userStatus);
  63. $response = $this->controller->find('john.doe');
  64. $this->assertEquals([
  65. 'userId' => 'john.doe',
  66. 'status' => 'offline',
  67. 'icon' => '🏝',
  68. 'message' => 'On vacation',
  69. 'clearAt' => 60000,
  70. ], $response->getData());
  71. }
  72. public function testFindDoesNotExist(): void {
  73. $this->service->expects($this->once())
  74. ->method('findByUserId')
  75. ->with('john.doe')
  76. ->willThrowException(new DoesNotExistException(''));
  77. $this->expectException(OCSNotFoundException::class);
  78. $this->expectExceptionMessage('No status for the requested userId');
  79. $this->controller->find('john.doe');
  80. }
  81. private function getUserStatus(): UserStatus {
  82. $userStatus = new UserStatus();
  83. $userStatus->setId(1337);
  84. $userStatus->setUserId('john.doe');
  85. $userStatus->setStatus('invisible');
  86. $userStatus->setStatusTimestamp(5000);
  87. $userStatus->setIsUserDefined(true);
  88. $userStatus->setCustomIcon('🏝');
  89. $userStatus->setCustomMessage('On vacation');
  90. $userStatus->setClearAt(60000);
  91. return $userStatus;
  92. }
  93. }