StatusesControllerTest.php 3.2 KB

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 GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\UserStatus\Tests\Controller;
  25. use OCA\UserStatus\Controller\StatusesController;
  26. use OCA\UserStatus\Db\UserStatus;
  27. use OCA\UserStatus\Service\StatusService;
  28. use OCP\AppFramework\Db\DoesNotExistException;
  29. use OCP\AppFramework\OCS\OCSNotFoundException;
  30. use OCP\IRequest;
  31. use Test\TestCase;
  32. class StatusesControllerTest extends TestCase {
  33. /** @var StatusService|\PHPUnit\Framework\MockObject\MockObject */
  34. private $service;
  35. /** @var StatusesController */
  36. private $controller;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $request = $this->createMock(IRequest::class);
  40. $this->service = $this->createMock(StatusService::class);
  41. $this->controller = new StatusesController('user_status', $request, $this->service);
  42. }
  43. public function testFindAll(): void {
  44. $userStatus = $this->getUserStatus();
  45. $this->service->expects($this->once())
  46. ->method('findAll')
  47. ->with(20, 40)
  48. ->willReturn([$userStatus]);
  49. $response = $this->controller->findAll(20, 40);
  50. $this->assertEquals([[
  51. 'userId' => 'john.doe',
  52. 'status' => 'offline',
  53. 'icon' => '🏝',
  54. 'message' => 'On vacation',
  55. 'clearAt' => 60000,
  56. ]], $response->getData());
  57. }
  58. public function testFind(): void {
  59. $userStatus = $this->getUserStatus();
  60. $this->service->expects($this->once())
  61. ->method('findByUserId')
  62. ->with('john.doe')
  63. ->willReturn($userStatus);
  64. $response = $this->controller->find('john.doe');
  65. $this->assertEquals([
  66. 'userId' => 'john.doe',
  67. 'status' => 'offline',
  68. 'icon' => '🏝',
  69. 'message' => 'On vacation',
  70. 'clearAt' => 60000,
  71. ], $response->getData());
  72. }
  73. public function testFindDoesNotExist(): void {
  74. $this->service->expects($this->once())
  75. ->method('findByUserId')
  76. ->with('john.doe')
  77. ->willThrowException(new DoesNotExistException(''));
  78. $this->expectException(OCSNotFoundException::class);
  79. $this->expectExceptionMessage('No status for the requested userId');
  80. $this->controller->find('john.doe');
  81. }
  82. private function getUserStatus(): UserStatus {
  83. $userStatus = new UserStatus();
  84. $userStatus->setId(1337);
  85. $userStatus->setUserId('john.doe');
  86. $userStatus->setStatus('invisible');
  87. $userStatus->setStatusTimestamp(5000);
  88. $userStatus->setIsUserDefined(true);
  89. $userStatus->setCustomIcon('🏝');
  90. $userStatus->setCustomMessage('On vacation');
  91. $userStatus->setClearAt(60000);
  92. return $userStatus;
  93. }
  94. }