ContactsMenuControllerTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Tests\Controller;
  7. use OC\Contacts\ContactsMenu\Manager;
  8. use OC\Core\Controller\ContactsMenuController;
  9. use OCP\Contacts\ContactsMenu\IEntry;
  10. use OCP\IRequest;
  11. use OCP\IUser;
  12. use OCP\IUserSession;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. use Test\TestCase;
  15. class ContactsMenuControllerTest extends TestCase {
  16. /** @var IUserSession|MockObject */
  17. private $userSession;
  18. /** @var Manager|MockObject */
  19. private $contactsManager;
  20. private ContactsMenuController $controller;
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $request = $this->createMock(IRequest::class);
  24. $this->userSession = $this->createMock(IUserSession::class);
  25. $this->contactsManager = $this->createMock(Manager::class);
  26. $this->controller = new ContactsMenuController($request, $this->userSession, $this->contactsManager);
  27. }
  28. public function testIndex(): void {
  29. $user = $this->createMock(IUser::class);
  30. $entries = [
  31. $this->createMock(IEntry::class),
  32. $this->createMock(IEntry::class),
  33. ];
  34. $this->userSession->expects($this->once())
  35. ->method('getUser')
  36. ->willReturn($user);
  37. $this->contactsManager->expects($this->once())
  38. ->method('getEntries')
  39. ->with($this->equalTo($user), $this->equalTo(null))
  40. ->willReturn($entries);
  41. $response = $this->controller->index();
  42. $this->assertEquals($entries, $response);
  43. }
  44. public function testFindOne(): void {
  45. $user = $this->createMock(IUser::class);
  46. $entry = $this->createMock(IEntry::class);
  47. $this->userSession->expects($this->once())
  48. ->method('getUser')
  49. ->willReturn($user);
  50. $this->contactsManager->expects($this->once())
  51. ->method('findOne')
  52. ->with($this->equalTo($user), $this->equalTo(42), $this->equalTo('test-search-phrase'))
  53. ->willReturn($entry);
  54. $response = $this->controller->findOne(42, 'test-search-phrase');
  55. $this->assertEquals($entry, $response);
  56. }
  57. public function testFindOne404(): void {
  58. $user = $this->createMock(IUser::class);
  59. $this->userSession->expects($this->once())
  60. ->method('getUser')
  61. ->willReturn($user);
  62. $this->contactsManager->expects($this->once())
  63. ->method('findOne')
  64. ->with($this->equalTo($user), $this->equalTo(42), $this->equalTo('test-search-phrase'))
  65. ->willReturn(null);
  66. $response = $this->controller->findOne(42, 'test-search-phrase');
  67. $this->assertEquals([], $response->getData());
  68. $this->assertEquals(404, $response->getStatus());
  69. }
  70. }