Skip to content

Commit a402361

Browse files
committed
refactor(tests): Extract TestHttpClient and TestResponse classes for better test structure
- Moved TestHttpClient and TestResponse classes to separate files for improved organization. - Simplified NavigationServiceTest and PageServiceTest by removing inline class definitions. - Enhanced test readability and maintainability by structuring test-specific clients separately.
1 parent 8751ed1 commit a402361

4 files changed

Lines changed: 100 additions & 122 deletions

File tree

tests/Service/NavigationServiceTest.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,15 @@
44

55
namespace Dotcms\PhpSdk\Tests\Service;
66

7-
use Dotcms\PhpSdk\Config\Config;
87
use Dotcms\PhpSdk\Exception\ResponseException;
9-
use Dotcms\PhpSdk\Http\HttpClient;
108
use Dotcms\PhpSdk\Model\NavigationItem;
119
use Dotcms\PhpSdk\Request\NavigationRequest;
1210
use Dotcms\PhpSdk\Service\NavigationService;
13-
use GuzzleHttp\Client;
1411
use GuzzleHttp\Handler\MockHandler;
15-
use GuzzleHttp\HandlerStack;
1612
use GuzzleHttp\Promise\PromiseInterface;
1713
use GuzzleHttp\Psr7\Response as GuzzleResponse;
1814
use PHPUnit\Framework\TestCase;
1915

20-
/**
21-
* Test-specific HttpClient that allows injecting a mock client
22-
*/
23-
class TestHttpClient extends HttpClient
24-
{
25-
private MockHandler $mockHandler;
26-
27-
public function __construct(MockHandler $mockHandler)
28-
{
29-
$config = new Config(
30-
'https://demo.dotcms.com/api/v1',
31-
'test-api-key',
32-
[
33-
'headers' => ['Content-Type' => 'application/json'],
34-
'verify' => false,
35-
'timeout' => 30,
36-
'connect_timeout' => 5,
37-
'http_errors' => false,
38-
]
39-
);
40-
$this->mockHandler = $mockHandler;
41-
parent::__construct($config);
42-
}
43-
44-
protected function createClient(): Client
45-
{
46-
$handlerStack = HandlerStack::create($this->mockHandler);
47-
48-
return new Client(['handler' => $handlerStack]);
49-
}
50-
}
51-
5216
class NavigationServiceTest extends TestCase
5317
{
5418
private MockHandler $mockHandler;

tests/Service/PageServiceTest.php

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -4,112 +4,28 @@
44

55
namespace Dotcms\PhpSdk\Tests\Service;
66

7-
use Dotcms\PhpSdk\Config\Config;
87
use Dotcms\PhpSdk\Exception\ResponseException;
9-
use Dotcms\PhpSdk\Http\HttpClient;
10-
use Dotcms\PhpSdk\Http\Response as DotcmsResponse;
118
use Dotcms\PhpSdk\Model\PageAsset;
129
use Dotcms\PhpSdk\Request\PageRequest;
1310
use Dotcms\PhpSdk\Service\PageService;
14-
use GuzzleHttp\Client;
1511
use GuzzleHttp\Handler\MockHandler;
16-
use GuzzleHttp\HandlerStack;
1712
use GuzzleHttp\Promise\PromiseInterface;
1813
use GuzzleHttp\Psr7\Response as GuzzleResponse;
1914
use PHPUnit\Framework\TestCase;
20-
use Psr\Http\Message\ResponseInterface;
2115

22-
/**
23-
* Test-specific HttpClient that allows injecting a mock client
24-
*/
25-
class TestHttpClient extends HttpClient
16+
class PageServiceTest extends TestCase
2617
{
2718
private MockHandler $mockHandler;
2819

29-
public function __construct(MockHandler $mockHandler)
30-
{
31-
$config = new Config(
32-
'https://demo.dotcms.com/api/v1',
33-
'test-api-key',
34-
[
35-
'headers' => ['Content-Type' => 'application/json'],
36-
'verify' => false,
37-
'timeout' => 30,
38-
'connect_timeout' => 5,
39-
'http_errors' => false,
40-
]
41-
);
42-
$this->mockHandler = $mockHandler;
43-
parent::__construct($config);
44-
}
45-
46-
protected function createClient(): Client
47-
{
48-
$handlerStack = HandlerStack::create($this->mockHandler);
49-
50-
return new Client(['handler' => $handlerStack]);
51-
}
52-
53-
public function request(string $method, string $uri, array $options = []): DotcmsResponse
54-
{
55-
$client = $this->createClient();
56-
$response = $client->request($method, $uri, $options);
57-
58-
return new TestResponse($response);
59-
}
60-
61-
public function requestAsync(string $method, string $uri, array $options = []): PromiseInterface
62-
{
63-
$client = $this->createClient();
64-
$promise = $client->requestAsync($method, $uri, $options);
65-
66-
return $promise->then(function ($response) {
67-
return new TestResponse($response);
68-
});
69-
}
70-
}
71-
72-
/**
73-
* Custom Response class for testing
74-
*/
75-
class TestResponse extends DotcmsResponse
76-
{
77-
private ResponseInterface $originalResponse;
78-
79-
public function __construct(ResponseInterface $response)
80-
{
81-
$this->originalResponse = $response;
82-
parent::__construct($response);
83-
}
84-
85-
public function toArray(): array
86-
{
87-
$body = (string)$this->originalResponse->getBody();
88-
$data = json_decode($body, true);
89-
90-
if (json_last_error() !== JSON_ERROR_NONE) {
91-
throw new ResponseException('Invalid JSON response: ' . json_last_error_msg());
92-
}
93-
94-
return $data;
95-
}
96-
}
97-
98-
class PageServiceTest extends TestCase
99-
{
10020
private TestHttpClient $httpClient;
10121

102-
private MockHandler $mockHandler;
103-
10422
private PageService $pageService;
10523

10624
protected function setUp(): void
10725
{
10826
$this->mockHandler = new MockHandler();
109-
$handlerStack = HandlerStack::create($this->mockHandler);
110-
$client = new Client(['handler' => $handlerStack]);
11127

112-
// Create our test-specific HttpClient with the mock client
28+
// Create our test-specific HttpClient with the mock handler
11329
$this->httpClient = new TestHttpClient($this->mockHandler);
11430
$this->pageService = new PageService($this->httpClient);
11531
}

tests/Service/TestHttpClient.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Dotcms\PhpSdk\Tests\Service;
6+
7+
use Dotcms\PhpSdk\Config\Config;
8+
use Dotcms\PhpSdk\Http\HttpClient;
9+
use Dotcms\PhpSdk\Http\Response as DotcmsResponse;
10+
use GuzzleHttp\Client;
11+
use GuzzleHttp\Handler\MockHandler;
12+
use GuzzleHttp\HandlerStack;
13+
use GuzzleHttp\Promise\PromiseInterface;
14+
15+
/**
16+
* Test-specific HttpClient that allows injecting a mock client
17+
*/
18+
class TestHttpClient extends HttpClient
19+
{
20+
private MockHandler $mockHandler;
21+
22+
public function __construct(MockHandler $mockHandler)
23+
{
24+
$config = new Config(
25+
'https://demo.dotcms.com/api/v1',
26+
'test-api-key',
27+
[
28+
'headers' => ['Content-Type' => 'application/json'],
29+
'verify' => false,
30+
'timeout' => 30,
31+
'connect_timeout' => 5,
32+
'http_errors' => false,
33+
]
34+
);
35+
$this->mockHandler = $mockHandler;
36+
parent::__construct($config);
37+
}
38+
39+
protected function createClient(): Client
40+
{
41+
$handlerStack = HandlerStack::create($this->mockHandler);
42+
43+
return new Client(['handler' => $handlerStack]);
44+
}
45+
46+
public function request(string $method, string $uri, array $options = []): DotcmsResponse
47+
{
48+
$client = $this->createClient();
49+
$response = $client->request($method, $uri, $options);
50+
51+
return new TestResponse($response);
52+
}
53+
54+
public function requestAsync(string $method, string $uri, array $options = []): PromiseInterface
55+
{
56+
$client = $this->createClient();
57+
$promise = $client->requestAsync($method, $uri, $options);
58+
59+
return $promise->then(function ($response) {
60+
return new TestResponse($response);
61+
});
62+
}
63+
}

tests/Service/TestResponse.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Dotcms\PhpSdk\Tests\Service;
6+
7+
use Dotcms\PhpSdk\Exception\ResponseException;
8+
use Dotcms\PhpSdk\Http\Response as DotcmsResponse;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
/**
12+
* Custom Response class for testing
13+
*/
14+
class TestResponse extends DotcmsResponse
15+
{
16+
private ResponseInterface $originalResponse;
17+
18+
public function __construct(ResponseInterface $response)
19+
{
20+
$this->originalResponse = $response;
21+
parent::__construct($response);
22+
}
23+
24+
public function toArray(): array
25+
{
26+
$body = (string)$this->originalResponse->getBody();
27+
$data = json_decode($body, true);
28+
29+
if (json_last_error() !== JSON_ERROR_NONE) {
30+
throw new ResponseException('Invalid JSON response: ' . json_last_error_msg());
31+
}
32+
33+
return $data;
34+
}
35+
}

0 commit comments

Comments
 (0)