Skip to content

Commit

Permalink
Merge 84cf962 into 72e70ee
Browse files Browse the repository at this point in the history
  • Loading branch information
keksa committed Mar 17, 2020
2 parents 72e70ee + 84cf962 commit fd8b51a
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 39 deletions.
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
]
},
"require-dev": {
"phpunit/phpunit": "^8.4",
"phpunit/phpunit": "^8.5",
"eloquent/phony-phpunit": "^5.0",
"phpstan/phpstan": "^0.11.19",
"eloquent/phpstan-phony": "^0.5.0",
"phpstan/phpstan-strict-rules": "^0.11",
"symplify/easy-coding-standard": "^6.1",
"php-coveralls/php-coveralls": "^2.1"
"phpstan/phpstan": "^0.12.15",
"eloquent/phpstan-phony": "^0.7.0",
"phpstan/phpstan-strict-rules": "^0.12",
"symplify/easy-coding-standard": "^7.2",
"php-coveralls/php-coveralls": "^2.2",
"ocramius/package-versions": "<1.6"
},
"suggest": {
"ext-zip": "Needed for pack-plugin script."
Expand Down
5 changes: 4 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ parameters:
- '#Only booleans are allowed in#'

# false positive
- '#UcrmOptionsManager::loadOptions\(\) should return .+ but returns .+ UcrmOptions|null#'
- '#UcrmOptionsManager::loadOptions\(\) should return .+ but returns .+UcrmOptions\|null#'

# too strict
- '#Short ternary operator is not allowed#'
38 changes: 29 additions & 9 deletions src/UcrmPluginSdk/Service/UcrmApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ public function get(string $endpoint, array $query = [])
]
);

if (stripos($response->getHeaderLine('content-type'), 'application/json') !== false) {
return Json::decode((string) $response->getBody());
}

return (string) $response->getBody();
return $this->handleResponse($response);
}

/**
Expand All @@ -140,17 +136,22 @@ public function get(string $endpoint, array $query = [])
*
* @param mixed[] $data
*
* @return mixed[]|string
*
* @throws GuzzleException
* @throws JsonException
*/
public function post(string $endpoint, array $data = []): void
public function post(string $endpoint, array $data = [])
{
$this->request(
$response = $this->request(
'POST',
$endpoint,
[
'json' => $data,
]
);

return $this->handleResponse($response);
}

/**
Expand All @@ -167,17 +168,22 @@ public function post(string $endpoint, array $data = []): void
*
* @param mixed[] $data
*
* @return mixed[]|string
*
* @throws GuzzleException
* @throws JsonException
*/
public function patch(string $endpoint, array $data = []): void
public function patch(string $endpoint, array $data = [])
{
$this->request(
$response = $this->request(
'PATCH',
$endpoint,
[
'json' => $data,
]
);

return $this->handleResponse($response);
}

/**
Expand Down Expand Up @@ -215,4 +221,18 @@ private function request(string $method, string $endpoint, array $options = []):
)
);
}

/**
* @return mixed[]|string
*
* @throws JsonException
*/
private function handleResponse(ResponseInterface $response)
{
if (stripos($response->getHeaderLine('content-type'), 'application/json') !== false) {
return Json::decode((string) $response->getBody());
}

return (string) $response->getBody();
}
}
38 changes: 29 additions & 9 deletions src/UcrmPluginSdk/Service/UnmsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,47 +107,53 @@ public function get(string $endpoint, array $query = [])
]
);

if (stripos($response->getHeaderLine('content-type'), 'application/json') !== false) {
return Json::decode((string) $response->getBody());
}

return (string) $response->getBody();
return $this->handleResponse($response);
}

/**
* Sends a POST request to UNMS API.
*
* @param mixed[] $data
*
* @return mixed[]|string
*
* @throws GuzzleException
* @throws JsonException
*/
public function post(string $endpoint, array $data = []): void
public function post(string $endpoint, array $data = [])
{
$this->request(
$response = $this->request(
'POST',
$endpoint,
[
'json' => $data,
]
);

return $this->handleResponse($response);
}

/**
* Sends a PATCH request to UNMS API.
*
* @param mixed[] $data
*
* @return mixed[]|string
*
* @throws GuzzleException
* @throws JsonException
*/
public function patch(string $endpoint, array $data = []): void
public function patch(string $endpoint, array $data = [])
{
$this->request(
$response = $this->request(
'PATCH',
$endpoint,
[
'json' => $data,
]
);

return $this->handleResponse($response);
}

/**
Expand Down Expand Up @@ -181,4 +187,18 @@ private function request(string $method, string $endpoint, array $options = []):
)
);
}

/**
* @return mixed[]|string
*
* @throws JsonException
*/
private function handleResponse(ResponseInterface $response)
{
if (stripos($response->getHeaderLine('content-type'), 'application/json') !== false) {
return Json::decode((string) $response->getBody());
}

return (string) $response->getBody();
}
}
30 changes: 23 additions & 7 deletions tests/UcrmPluginSdk/Service/UcrmApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,17 @@ public function testDisabledPlugin(): void
self::assertInstanceOf(ConfigurationException::class, $exception);
}

public function testPost(): void
/**
* @param mixed[]|string $expectedResult
*
* @dataProvider responseProvider
*/
public function testPost(string $contentType, string $returnedBody, $expectedResult): void
{
$responseHandle = Phony::mock(Response::class);
$responseHandle->getStatusCode->returns(201);
$responseHandle->getBody->returns($returnedBody);
$responseHandle->getHeaderLine->with('content-type')->returns($contentType);
$responseMock = $responseHandle->get();

$clientHandle = Phony::mock(Client::class);
Expand All @@ -74,7 +81,8 @@ public function testPost(): void
'firstName' => 'John',
'lastName' => 'Doe',
];
$ucrmApi->post($endpoint, $data);
$result = $ucrmApi->post($endpoint, $data);
self::assertSame($expectedResult, $result);

$clientHandle->request->calledWith(
'POST',
Expand All @@ -88,10 +96,17 @@ public function testPost(): void
);
}

public function testPatch(): void
/**
* @param mixed[]|string $expectedResult
*
* @dataProvider responseProvider
*/
public function testPatch(string $contentType, string $returnedBody, $expectedResult): void
{
$responseHandle = Phony::mock(Response::class);
$responseHandle->getStatusCode->returns(200);
$responseHandle->getBody->returns($returnedBody);
$responseHandle->getHeaderLine->with('content-type')->returns($contentType);
$responseMock = $responseHandle->get();

$clientHandle = Phony::mock(Client::class);
Expand All @@ -104,7 +119,8 @@ public function testPatch(): void
'firstName' => 'John',
'lastName' => 'Doe',
];
$ucrmApi->patch($endpoint, $data);
$result = $ucrmApi->patch($endpoint, $data);
self::assertSame($expectedResult, $result);

$clientHandle->request->calledWith(
'PATCH',
Expand Down Expand Up @@ -146,12 +162,12 @@ public function testDelete(): void
/**
* @param mixed[]|string $expectedResult
*
* @dataProvider getProvider
* @dataProvider responseProvider
*/
public function testGet(string $contentType, string $returnedBody, $expectedResult): void
{
$responseHandle = Phony::mock(Response::class);
$responseHandle->getStatusCode->returns(201);
$responseHandle->getStatusCode->returns(200);
$responseHandle->getBody->returns($returnedBody);
$responseHandle->getHeaderLine->with('content-type')->returns($contentType);
$responseMock = $responseHandle->get();
Expand Down Expand Up @@ -184,7 +200,7 @@ public function testGet(string $contentType, string $returnedBody, $expectedResu
/**
* @return mixed[]
*/
public function getProvider(): array
public function responseProvider(): array
{
return [
[
Expand Down
30 changes: 23 additions & 7 deletions tests/UcrmPluginSdk/Service/UnmsApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,17 @@ public function testDisabledPlugin(): void
self::assertInstanceOf(ConfigurationException::class, $exception);
}

public function testPost(): void
/**
* @param mixed[]|string $expectedResult
*
* @dataProvider responseProvider
*/
public function testPost(string $contentType, string $returnedBody, $expectedResult): void
{
$responseHandle = Phony::mock(Response::class);
$responseHandle->getStatusCode->returns(201);
$responseHandle->getBody->returns($returnedBody);
$responseHandle->getHeaderLine->with('content-type')->returns($contentType);
$responseMock = $responseHandle->get();

$clientHandle = Phony::mock(Client::class);
Expand All @@ -74,7 +81,8 @@ public function testPost(): void
'firstName' => 'John',
'lastName' => 'Doe',
];
$ucrmApi->post($endpoint, $data);
$result = $ucrmApi->post($endpoint, $data);
self::assertSame($expectedResult, $result);

$clientHandle->request->calledWith(
'POST',
Expand All @@ -88,10 +96,17 @@ public function testPost(): void
);
}

public function testPatch(): void
/**
* @param mixed[]|string $expectedResult
*
* @dataProvider responseProvider
*/
public function testPatch(string $contentType, string $returnedBody, $expectedResult): void
{
$responseHandle = Phony::mock(Response::class);
$responseHandle->getStatusCode->returns(200);
$responseHandle->getBody->returns($returnedBody);
$responseHandle->getHeaderLine->with('content-type')->returns($contentType);
$responseMock = $responseHandle->get();

$clientHandle = Phony::mock(Client::class);
Expand All @@ -104,7 +119,8 @@ public function testPatch(): void
'firstName' => 'John',
'lastName' => 'Doe',
];
$ucrmApi->patch($endpoint, $data);
$result = $ucrmApi->patch($endpoint, $data);
self::assertSame($expectedResult, $result);

$clientHandle->request->calledWith(
'PATCH',
Expand Down Expand Up @@ -146,12 +162,12 @@ public function testDelete(): void
/**
* @param mixed[]|string $expectedResult
*
* @dataProvider getProvider
* @dataProvider responseProvider
*/
public function testGet(string $contentType, string $returnedBody, $expectedResult): void
{
$responseHandle = Phony::mock(Response::class);
$responseHandle->getStatusCode->returns(201);
$responseHandle->getStatusCode->returns(200);
$responseHandle->getBody->returns($returnedBody);
$responseHandle->getHeaderLine->with('content-type')->returns($contentType);
$responseMock = $responseHandle->get();
Expand Down Expand Up @@ -184,7 +200,7 @@ public function testGet(string $contentType, string $returnedBody, $expectedResu
/**
* @return mixed[]
*/
public function getProvider(): array
public function responseProvider(): array
{
return [
[
Expand Down

0 comments on commit fd8b51a

Please sign in to comment.