Skip to content

Commit

Permalink
Merge pull request #20 from antistatique/feature/raise-coverage
Browse files Browse the repository at this point in the history
improve coverage of args[language] for Pricehubble::makeRequest
  • Loading branch information
WengerK authored Jun 14, 2024
2 parents 6ea16da + cb2d229 commit 1b29bc2
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Pricehubble.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,21 @@ class Pricehubble
*/
public function __construct()
{
if (!function_exists('curl_init') || !function_exists('curl_setopt')) {
if (!$this->isCurlAvailable()) {
throw new \RuntimeException("cURL support is required, but can't be found.");
}

$this->lastResponse = ['headers' => null, 'body' => null];
}

/**
* Check if cURL is available.
*/
public function isCurlAvailable(): bool
{
return function_exists('curl_init') || function_exists('curl_setopt');
}

/**
* Proxies all Pricehubble API Class and Methods.
*/
Expand Down
58 changes: 58 additions & 0 deletions tests/Unit/CurlAvailabilitiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Antistatique\Pricehubble\Tests\Unit;

use Antistatique\Pricehubble\Pricehubble;
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \Antistatique\Pricehubble\Pricehubble
*
* @group pricehubble
* @group pricehubble_unit
*
* @internal
*/
final class CurlAvailabilitiesTest extends TestCase
{
use PHPMock;

/**
* @covers ::isCurlAvailable
*/
public function testIsCurlAvailable(): void
{
$pricehubble = new Pricehubble();
$this->assertTrue($pricehubble->isCurlAvailable());
}

/**
* @covers ::__construct
* @covers ::isCurlAvailable
*/
public function testcurlNotAvailable(): void
{
$pricehubbleMock = $this->createMock(Pricehubble::class);
$pricehubbleMock->method('isCurlAvailable')->willReturn(false);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('cURL support is required, but can\'t be found.');
$pricehubbleMock->__construct();

$pricehubbleMock->method('isCurlAvailable')->willReturn(true);
}

/**
* @covers ::__construct
* @covers ::isCurlAvailable
*
* @doesNotPerformAssertions
*/
public function testCurlAvailable(): void
{
$pricehubbleMock = $this->createMock(Pricehubble::class);
$pricehubbleMock->method('isCurlAvailable')->willReturn(true);
$pricehubbleMock->__construct();
}
}
40 changes: 40 additions & 0 deletions tests/Unit/PricehubbleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,46 @@ public function testMakeRequestByVerbs(string $verb): void
]);
}

/**
* @covers ::makeRequest
*/
public function testMakeRequestArgsLanguage(): void
{
$pricehubble_mock = $this->getMockBuilder(Pricehubble::class)
->onlyMethods(['getApiToken', 'prepareStateForRequest', 'setResponseState', 'formatResponse', 'determineSuccess'])
->getMock();

$pricehubble_mock->expects($this->once())
->method('prepareStateForRequest')
->with('get', 'https://example.org', 10);

$pricehubble_mock->expects($this->exactly(2))
->method('getApiToken')
->willReturn('api-token');

$pricehubble_mock->expects($this->once())
->method('setResponseState')
->with($this->isType('array'), $this->isType('string'), $this->anything())
;

$pricehubble_mock->expects($this->once())
->method('formatResponse')
->with($this->isType('array'))
->willReturn(['foo' => 'bar']);

$pricehubble_mock->expects($this->once())
->method('determineSuccess')
->with($this->isType('array'), $this->isType('array'), $this->isType('integer'))
->willReturn(true);

$curl_exec_mock = $this->getFunctionMock('Antistatique\Pricehubble', 'curl_exec');
$curl_exec_mock->expects($this->once())->willReturn('body');

$this->callPrivateMethod($pricehubble_mock, 'makeRequest', [
'get', 'https://example.org', ['language' => 'fr'],
]);
}

/**
* Provider of :testMakeRequestByVerbs.
*
Expand Down

0 comments on commit 1b29bc2

Please sign in to comment.