ActionProviderStoreTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Contacts\ContactsMenu;
  24. use Exception;
  25. use OC\App\AppManager;
  26. use OC\Contacts\ContactsMenu\ActionProviderStore;
  27. use OC\Contacts\ContactsMenu\Providers\EMailProvider;
  28. use OCP\App\IAppManager;
  29. use OCP\AppFramework\QueryException;
  30. use OCP\Contacts\ContactsMenu\IProvider;
  31. use OCP\ILogger;
  32. use OCP\IServerContainer;
  33. use OCP\IUser;
  34. use PHPUnit_Framework_MockObject_MockObject;
  35. use Test\TestCase;
  36. class ActionProviderStoreTest extends TestCase {
  37. /** @var IServerContainer|PHPUnit_Framework_MockObject_MockObject */
  38. private $serverContainer;
  39. /** @var IAppManager|PHPUnit_Framework_MockObject_MockObject */
  40. private $appManager;
  41. /** @var ILogger|PHPUnit_Framework_MockObject_MockObject */
  42. private $logger;
  43. /** @var ActionProviderStore */
  44. private $actionProviderStore;
  45. protected function setUp() {
  46. parent::setUp();
  47. $this->serverContainer = $this->createMock(IServerContainer::class);
  48. $this->appManager = $this->createMock(AppManager::class);
  49. $this->logger = $this->createMock(ILogger::class);
  50. $this->actionProviderStore = new ActionProviderStore($this->serverContainer, $this->appManager, $this->logger);
  51. }
  52. public function testGetProviders() {
  53. $user = $this->createMock(IUser::class);
  54. $provider1 = $this->createMock(EMailProvider::class);
  55. $provider2 = $this->createMock(IProvider::class);
  56. $this->appManager->expects($this->once())
  57. ->method('getEnabledAppsForUser')
  58. ->with($user)
  59. ->willReturn(['contacts']);
  60. $this->appManager->expects($this->once())
  61. ->method('getAppInfo')
  62. ->with('contacts')
  63. ->willReturn([
  64. 'contactsmenu' => [
  65. 'OCA\Contacts\Provider1',
  66. ],
  67. ]);
  68. $this->serverContainer->expects($this->exactly(2))
  69. ->method('query')
  70. ->will($this->returnValueMap([
  71. [EMailProvider::class, $provider1],
  72. ['OCA\Contacts\Provider1', $provider2]
  73. ]));
  74. $providers = $this->actionProviderStore->getProviders($user);
  75. $this->assertCount(2, $providers);
  76. $this->assertInstanceOf(EMailProvider::class, $providers[0]);
  77. }
  78. public function testGetProvidersOfAppWithIncompleInfo() {
  79. $user = $this->createMock(IUser::class);
  80. $provider1 = $this->createMock(EMailProvider::class);
  81. $this->appManager->expects($this->once())
  82. ->method('getEnabledAppsForUser')
  83. ->with($user)
  84. ->willReturn(['contacts']);
  85. $this->appManager->expects($this->once())
  86. ->method('getAppInfo')
  87. ->with('contacts')
  88. ->willReturn([/* Empty info.xml */]);
  89. $this->serverContainer->expects($this->once())
  90. ->method('query')
  91. ->will($this->returnValueMap([
  92. [EMailProvider::class, $provider1],
  93. ]));
  94. $providers = $this->actionProviderStore->getProviders($user);
  95. $this->assertCount(1, $providers);
  96. $this->assertInstanceOf(EMailProvider::class, $providers[0]);
  97. }
  98. /**
  99. * @expectedException Exception
  100. */
  101. public function testGetProvidersWithQueryException() {
  102. $user = $this->createMock(IUser::class);
  103. $this->appManager->expects($this->once())
  104. ->method('getEnabledAppsForUser')
  105. ->with($user)
  106. ->willReturn([]);
  107. $this->serverContainer->expects($this->once())
  108. ->method('query')
  109. ->willThrowException(new QueryException());
  110. $this->actionProviderStore->getProviders($user);
  111. }
  112. }