1
0

ClientServiceTrait.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Traits;
  7. use OCP\Http\Client\IClient;
  8. use OCP\Http\Client\IClientService;
  9. use OCP\Http\Client\IResponse;
  10. trait ClientServiceTrait {
  11. /** @var IClientService|\PHPUnit\Framework\MockObject\MockObject */
  12. private $clientService;
  13. /** @var IClient|\PHPUnit\Framework\MockObject\MockObject */
  14. private $client;
  15. private $expectedGetRequests = [];
  16. private $expectedPostRequests = [];
  17. /**
  18. * Wrapper to be forward compatible to phpunit 5.4+
  19. *
  20. * @param string $originalClassName
  21. * @return \PHPUnit\Framework\MockObject\MockObject
  22. */
  23. abstract protected function createMock(string $originalClassName);
  24. /**
  25. * Returns a matcher that matches when the method is executed
  26. * zero or more times.
  27. *
  28. * @return \PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount
  29. *
  30. * @since Method available since Release 3.0.0
  31. */
  32. abstract public static function any();
  33. protected function setUpClientServiceTrait() {
  34. $this->clientService = $this->createMock(IClientService::class);
  35. $this->client = $this->createMock(IClient::class);
  36. $this->clientService->expects($this->any())
  37. ->method('newClient')
  38. ->willReturn($this->client);
  39. $this->client->expects($this->any())
  40. ->method('get')
  41. ->willReturnCallback(function ($url) {
  42. if (!isset($this->expectedGetRequests[$url])) {
  43. throw new \Exception('unexpected request: ' . $url);
  44. }
  45. $result = $this->expectedGetRequests[$url];
  46. if ($result instanceof \Exception) {
  47. throw $result;
  48. } else {
  49. $response = $this->createMock(IResponse::class);
  50. $response->expects($this->any())
  51. ->method('getBody')
  52. ->willReturn($result);
  53. return $response;
  54. }
  55. });
  56. $this->client->expects($this->any())
  57. ->method('post')
  58. ->willReturnCallback(function ($url) {
  59. if (!isset($this->expectedPostRequests[$url])) {
  60. throw new \Exception('unexpected request: ' . $url);
  61. }
  62. $result = $this->expectedPostRequests[$url];
  63. if ($result instanceof \Exception) {
  64. throw $result;
  65. } else {
  66. $response = $this->createMock(IResponse::class);
  67. $response->expects($this->any())
  68. ->method('getBody')
  69. ->willReturn($result);
  70. return $response;
  71. }
  72. });
  73. }
  74. /**
  75. * @param string $url
  76. * @param string|\Exception $result
  77. */
  78. protected function expectGetRequest($url, $result) {
  79. $this->expectedGetRequests[$url] = $result;
  80. }
  81. /**
  82. * @param string $url
  83. * @param string|\Exception $result
  84. */
  85. protected function expectPostRequest($url, $result) {
  86. $this->expectedPostRequests[$url] = $result;
  87. }
  88. /**
  89. * @return IClientService|\PHPUnit\Framework\MockObject\MockObject
  90. */
  91. protected function getClientService() {
  92. return $this->clientService;
  93. }
  94. }