ContactsMenuControllerTest.php 3.3 KB

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