ContactsMenuControllerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  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
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Tests\Controller;
  24. use OC\Contacts\ContactsMenu\Manager;
  25. use OC\Core\Controller\ContactsMenuController;
  26. use OCP\Contacts\ContactsMenu\IEntry;
  27. use OCP\IRequest;
  28. use OCP\IUser;
  29. use OCP\IUserSession;
  30. use PHPUnit\Framework\MockObject\MockObject;
  31. use Test\TestCase;
  32. class ContactsMenuControllerTest extends TestCase {
  33. /** @var IUserSession|MockObject */
  34. private $userSession;
  35. /** @var Manager|MockObject */
  36. private $contactsManager;
  37. private ContactsMenuController $controller;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $request = $this->createMock(IRequest::class);
  41. $this->userSession = $this->createMock(IUserSession::class);
  42. $this->contactsManager = $this->createMock(Manager::class);
  43. $this->controller = new ContactsMenuController($request, $this->userSession, $this->contactsManager);
  44. }
  45. public function testIndex() {
  46. $user = $this->createMock(IUser::class);
  47. $entries = [
  48. $this->createMock(IEntry::class),
  49. $this->createMock(IEntry::class),
  50. ];
  51. $this->userSession->expects($this->once())
  52. ->method('getUser')
  53. ->willReturn($user);
  54. $this->contactsManager->expects($this->once())
  55. ->method('getEntries')
  56. ->with($this->equalTo($user), $this->equalTo(null))
  57. ->willReturn($entries);
  58. $response = $this->controller->index();
  59. $this->assertEquals($entries, $response);
  60. }
  61. public function testFindOne() {
  62. $user = $this->createMock(IUser::class);
  63. $entry = $this->createMock(IEntry::class);
  64. $this->userSession->expects($this->once())
  65. ->method('getUser')
  66. ->willReturn($user);
  67. $this->contactsManager->expects($this->once())
  68. ->method('findOne')
  69. ->with($this->equalTo($user), $this->equalTo(42), $this->equalTo('test-search-phrase'))
  70. ->willReturn($entry);
  71. $response = $this->controller->findOne(42, 'test-search-phrase');
  72. $this->assertEquals($entry, $response);
  73. }
  74. public function testFindOne404() {
  75. $user = $this->createMock(IUser::class);
  76. $this->userSession->expects($this->once())
  77. ->method('getUser')
  78. ->willReturn($user);
  79. $this->contactsManager->expects($this->once())
  80. ->method('findOne')
  81. ->with($this->equalTo($user), $this->equalTo(42), $this->equalTo('test-search-phrase'))
  82. ->willReturn(null);
  83. $response = $this->controller->findOne(42, 'test-search-phrase');
  84. $this->assertEquals([], $response->getData());
  85. $this->assertEquals(404, $response->getStatus());
  86. }
  87. }