ContactsMenuControllerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Test\TestCase;
  31. class ContactsMenuControllerTest extends TestCase {
  32. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  33. private $request;
  34. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  35. private $userSession;
  36. /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */
  37. private $contactsManager;
  38. /** @var ContactsMenuController */
  39. private $controller;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->request = $this->createMock(IRequest::class);
  43. $this->userSession = $this->createMock(IUserSession::class);
  44. $this->contactsManager = $this->createMock(Manager::class);
  45. $this->controller = new ContactsMenuController($this->request, $this->userSession, $this->contactsManager);
  46. }
  47. public function testIndex() {
  48. $user = $this->createMock(IUser::class);
  49. $entries = [
  50. $this->createMock(IEntry::class),
  51. $this->createMock(IEntry::class),
  52. ];
  53. $this->userSession->expects($this->once())
  54. ->method('getUser')
  55. ->willReturn($user);
  56. $this->contactsManager->expects($this->once())
  57. ->method('getEntries')
  58. ->with($this->equalTo($user), $this->equalTo(null))
  59. ->willReturn($entries);
  60. $response = $this->controller->index();
  61. $this->assertEquals($entries, $response);
  62. }
  63. public function testFindOne() {
  64. $user = $this->createMock(IUser::class);
  65. $entry = $this->createMock(IEntry::class);
  66. $this->userSession->expects($this->once())
  67. ->method('getUser')
  68. ->willReturn($user);
  69. $this->contactsManager->expects($this->once())
  70. ->method('findOne')
  71. ->with($this->equalTo($user), $this->equalTo(42), $this->equalTo('test-search-phrase'))
  72. ->willReturn($entry);
  73. $response = $this->controller->findOne(42, 'test-search-phrase');
  74. $this->assertEquals($entry, $response);
  75. }
  76. public function testFindOne404() {
  77. $user = $this->createMock(IUser::class);
  78. $this->userSession->expects($this->once())
  79. ->method('getUser')
  80. ->willReturn($user);
  81. $this->contactsManager->expects($this->once())
  82. ->method('findOne')
  83. ->with($this->equalTo($user), $this->equalTo(42), $this->equalTo('test-search-phrase'))
  84. ->willReturn(null);
  85. $response = $this->controller->findOne(42, 'test-search-phrase');
  86. $this->assertEquals([], $response->getData());
  87. $this->assertEquals(404, $response->getStatus());
  88. }
  89. }