1
0

ClientServiceTrait.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Traits;
  22. use OCP\Http\Client\IClient;
  23. use OCP\Http\Client\IClientService;
  24. use OCP\Http\Client\IResponse;
  25. trait ClientServiceTrait {
  26. /** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */
  27. private $clientService;
  28. /** @var IClient|\PHPUnit_Framework_MockObject_MockObject */
  29. private $client;
  30. private $expectedGetRequests = [];
  31. private $expectedPostRequests = [];
  32. /**
  33. * Wrapper to be forward compatible to phpunit 5.4+
  34. *
  35. * @param string $originalClassName
  36. * @return \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. abstract protected function createMock($originalClassName);
  39. /**
  40. * Returns a matcher that matches when the method is executed
  41. * zero or more times.
  42. *
  43. * @return \PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount
  44. *
  45. * @since Method available since Release 3.0.0
  46. */
  47. abstract public function any();
  48. protected function setUpClientServiceTrait() {
  49. $this->clientService = $this->createMock(IClientService::class);
  50. $this->client = $this->createMock(IClient::class);
  51. $this->clientService->expects($this->any())
  52. ->method('newClient')
  53. ->willReturn($this->client);
  54. $this->client->expects($this->any())
  55. ->method('get')
  56. ->willReturnCallback(function ($url) {
  57. if (!isset($this->expectedGetRequests[$url])) {
  58. throw new \Exception('unexpected request: ' . $url);
  59. }
  60. $result = $this->expectedGetRequests[$url];
  61. if ($result instanceof \Exception) {
  62. throw $result;
  63. } else {
  64. $response = $this->createMock(IResponse::class);
  65. $response->expects($this->any())
  66. ->method('getBody')
  67. ->willReturn($result);
  68. return $response;
  69. }
  70. });
  71. $this->client->expects($this->any())
  72. ->method('post')
  73. ->willReturnCallback(function ($url) {
  74. if (!isset($this->expectedPostRequests[$url])) {
  75. throw new \Exception('unexpected request: ' . $url);
  76. }
  77. $result = $this->expectedPostRequests[$url];
  78. if ($result instanceof \Exception) {
  79. throw $result;
  80. } else {
  81. $response = $this->createMock(IResponse::class);
  82. $response->expects($this->any())
  83. ->method('getBody')
  84. ->willReturn($result);
  85. return $response;
  86. }
  87. });
  88. }
  89. /**
  90. * @param string $url
  91. * @param string|\Exception $result
  92. */
  93. protected function expectGetRequest($url, $result) {
  94. $this->expectedGetRequests[$url] = $result;
  95. }
  96. /**
  97. * @param string $url
  98. * @param string|\Exception $result
  99. */
  100. protected function expectPostRequest($url, $result) {
  101. $this->expectedPostRequests[$url] = $result;
  102. }
  103. /**
  104. * @return IClientService|\PHPUnit_Framework_MockObject_MockObject
  105. */
  106. protected function getClientService() {
  107. return $this->clientService;
  108. }
  109. }