From 5d0e20581c2d64355cf9bef93a45672cc7a22f76 Mon Sep 17 00:00:00 2001 From: Yura Date: Wed, 9 Sep 2020 12:51:31 +0300 Subject: [PATCH] updated method TelephonyCallEvent (#88) --- lib/RetailCrm/Methods/V3/Telephony.php | 18 +++++++--- tests/RetailCrm/Test/TestCase.php | 17 ++++++++++ .../Version4/ApiClientTelephonyTest.php | 34 ++++++++++++++++--- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/lib/RetailCrm/Methods/V3/Telephony.php b/lib/RetailCrm/Methods/V3/Telephony.php index 5e80a96..b6c01bc 100644 --- a/lib/RetailCrm/Methods/V3/Telephony.php +++ b/lib/RetailCrm/Methods/V3/Telephony.php @@ -54,13 +54,15 @@ trait Telephony /** * Call event * - * @param string $phone phone number - * @param string $type call type + * @param string $phone phone number + * @param string $type call type * @param array $codes + * @param array $userIds + * @param string $callExternalId * @param string $hangupStatus * @param string $externalPhone * @param array $webAnalyticsData - * @param string $site (default: null) + * @param string $site (default: null) * * @return \RetailCrm\Response\ApiResponse * @internal param string $code additional phone code @@ -70,8 +72,10 @@ trait Telephony $phone, $type, $codes, + $userIds = [], $hangupStatus = null, $externalPhone = null, + $callExternalId = null, $webAnalyticsData = [], $site = null ) { @@ -90,8 +94,14 @@ trait Telephony $parameters['phone'] = $phone; $parameters['type'] = $type; $parameters['codes'] = $codes; + + if (!empty($userIds)) { + $parameters['userIds'] = $userIds; + } + $parameters['hangupStatus'] = $hangupStatus; - $parameters['callExternalId'] = $externalPhone; + $parameters['callExternalId'] = $callExternalId; + $parameters['externalPhone'] = $externalPhone; $parameters['webAnalyticsData'] = $webAnalyticsData; /* @noinspection PhpUndefinedMethodInspection */ diff --git a/tests/RetailCrm/Test/TestCase.php b/tests/RetailCrm/Test/TestCase.php index 9cc5fea..b8f250c 100644 --- a/tests/RetailCrm/Test/TestCase.php +++ b/tests/RetailCrm/Test/TestCase.php @@ -14,6 +14,7 @@ namespace RetailCrm\Test; +use PHPUnit\Framework\MockObject\MockObject; use RetailCrm\ApiClient; use RetailCrm\Http\Client; use PHPUnit\Framework\TestCase as BaseCase; @@ -58,6 +59,22 @@ class TestCase extends BaseCase ); } + /** + * @param \RetailCrm\Http\Client|MockObject $httpClient + * @return ApiClient + * @throws \ReflectionException + */ + public static function getMockedApiClient($httpClient) + { + $client = self::getApiClient(); + $property = new \ReflectionProperty(get_class($client->request), 'client'); + + $property->setAccessible(true); + $property->setValue($client->request, $httpClient); + + return $client; + } + /** * Return Client object * diff --git a/tests/RetailCrm/Tests/Methods/Version4/ApiClientTelephonyTest.php b/tests/RetailCrm/Tests/Methods/Version4/ApiClientTelephonyTest.php index 08d4a0e..9d9b2d8 100644 --- a/tests/RetailCrm/Tests/Methods/Version4/ApiClientTelephonyTest.php +++ b/tests/RetailCrm/Tests/Methods/Version4/ApiClientTelephonyTest.php @@ -15,6 +15,7 @@ namespace RetailCrm\Tests\Methods\Version4; use RetailCrm\ApiClient; +use RetailCrm\Response\ApiResponse; use RetailCrm\Test\TestCase; /** @@ -90,19 +91,42 @@ class ApiClientTelephonyTest extends TestCase */ public function testTelephonyEvent() { - self::markTestSkipped('Should be fixed.'); - $client = static::getApiClient(null, null, ApiClient::V4); + $stub = $this->getMockBuilder(\RetailCrm\Http\Client::class) + ->disableOriginalConstructor() + ->setMethods(['makeRequest']) + ->getMock(); + + $parameters = [ + 'phone' => '+79999999999', + 'type' => 'in', + 'codes' => ['101'], + 'userIds' => [2], + 'hangupStatus' => 'failed', + 'callExternalId' => '+74950000000', + 'externalPhone' => '123456789', + 'webAnalyticsData' => [], + 'site' => 'retailcrm-ru' + ]; + + $stub->expects(self::once())->method('makeRequest')->with( + '/telephony/call/event', + "POST", + ['event' => json_encode($parameters)] + )->willReturn((new ApiResponse(200, json_encode(['success' => true])))->asJsonResponse()); + + $client = static::getMockedApiClient($stub); $response = $client->request->telephonyCallEvent( '+79999999999', 'in', ['101'], + [2], 'failed', - '+74950000000' + '123456789', + '+74950000000', + [] ); - static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response); - static::assertEquals(200, $response->getStatusCode()); static::assertTrue($response->isSuccessful()); }