Skip to content

Commit 3984a1b

Browse files
author
n.gnato
committed
WIP: new composition prepend / append
1 parent 1181eac commit 3984a1b

File tree

9 files changed

+38
-17
lines changed

9 files changed

+38
-17
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public function test()
1919
$responseFactory,
2020
new \FreeElephants\StaticHttpClient\PathResolver\PathBuilderBasedResolver(
2121
new \FreeElephants\StaticHttpClient\PathBuilder\Composite(
22-
new \FreeElephants\StaticHttpClient\PathBuilder\BasePath(__DIR__),
23-
new \FreeElephants\StaticHttpClient\PathBuilder\HostnameAsDirectory(),
22+
new \FreeElephants\StaticHttpClient\PathBuilder\PrependBasePath(__DIR__),
23+
new \FreeElephants\StaticHttpClient\PathBuilder\PrependHostnameAsDirectory(),
2424
new \FreeElephants\StaticHttpClient\PathBuilder\AppendRequestPath(),
25-
new \FreeElephants\StaticHttpClient\PathBuilder\DefaultFileExtension('.json'),
25+
new \FreeElephants\StaticHttpClient\PathBuilder\AppendDefaultFileExtension('.json'),
2626
)
2727
)
2828
);

src/FreeElephants/StaticHttpClient/PathBuilder/DefaultFileExtension.php renamed to src/FreeElephants/StaticHttpClient/PathBuilder/AppendDefaultFileExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Psr\Http\Message\RequestInterface;
66

7-
class DefaultFileExtension implements PathBuilderInterface
7+
class AppendDefaultFileExtension implements PathBuilderInterface
88
{
99

1010
private string $fileExtension;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace FreeElephants\StaticHttpClient\PathBuilder;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
class AppendHostnameAsDirectory implements PathBuilderInterface
8+
{
9+
public function build(RequestInterface $request, string $path = ''): string
10+
{
11+
return $path . DIRECTORY_SEPARATOR . $request->getUri()->getHost();
12+
}
13+
14+
}

src/FreeElephants/StaticHttpClient/PathBuilder/BasePath.php renamed to src/FreeElephants/StaticHttpClient/PathBuilder/PrependBasePath.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
use Psr\Http\Message\RequestInterface;
66

7-
class BasePath implements PathBuilderInterface
7+
class PrependBasePath implements PathBuilderInterface
88
{
99
private string $basePath;
1010

1111
public function __construct(string $basePath)
1212
{
13-
1413
$this->basePath = $basePath;
1514
}
1615

src/FreeElephants/StaticHttpClient/PathBuilder/HostnameAsDirectory.php renamed to src/FreeElephants/StaticHttpClient/PathBuilder/PrependHostnameAsDirectory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Psr\Http\Message\RequestInterface;
66

7-
class HostnameAsDirectory implements PathBuilderInterface
7+
class PrependHostnameAsDirectory implements PathBuilderInterface
88
{
99
public function build(RequestInterface $request, string $path = ''): string
1010
{

src/FreeElephants/StaticHttpClient/PathResolver/PathBuilderBasedResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function resolve(RequestInterface $request): string
1919
{
2020
$filename = $this->pathBuilder->build($request);
2121

22+
var_dump($filename);
2223
if (file_exists($filename)) {
2324
return $filename;
2425
}

tests/FreeElephants/StaticHttpClient/PathBuilder/CompositeTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ class CompositeTest extends AbstractTestCase
1111
public function testBuild(): void
1212
{
1313
$composite = new Composite(
14+
new PrependBasePath('/root'),
15+
new AppendHostnameAsDirectory(),
1416
new AppendRequestPath(),
15-
new DefaultFileExtension(),
16-
new HostnameAsDirectory(),
17+
new AppendDefaultFileExtension(),
1718
);
1819

1920
$path = $composite->build(new Request('GET', 'http://example.com/foo'));
2021

21-
$this->assertSame('example.com/foo.json', $path, 'Composite should build path from all builders');
22+
$this->assertSame('/root/example.com/foo.json', $path, 'Composite should build path from all builders');
2223
}
2324
}

tests/FreeElephants/StaticHttpClient/PathResolver/PathBuilderBasedResolverTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use FreeElephants\StaticHttpClient\AbstractTestCase;
66
use FreeElephants\StaticHttpClient\PathBuilder\AppendRequestPath;
7-
use FreeElephants\StaticHttpClient\PathBuilder\BasePath;
7+
use FreeElephants\StaticHttpClient\PathBuilder\PrependBasePath;
88
use FreeElephants\StaticHttpClient\PathBuilder\Composite;
9-
use FreeElephants\StaticHttpClient\PathBuilder\DefaultFileExtension;
9+
use FreeElephants\StaticHttpClient\PathBuilder\AppendDefaultFileExtension;
1010
use FreeElephants\StaticHttpClient\PathBuilder\PathBuilderInterface;
11+
use FreeElephants\StaticHttpClient\PathBuilder\PrependHostnameAsDirectory;
1112
use FreeElephants\StaticHttpClient\PathResolver\Exception\UnresolvablePathException;
1213
use Nyholm\Psr7\Request;
1314
use PHPUnit\Framework\TestCase;
@@ -18,21 +19,22 @@ public function testResolve(): void
1819
{
1920
$pathBuilderBasedResolver = new PathBuilderBasedResolver(
2021
new Composite(
21-
new BasePath(self::FIXTURE_PATH),
22+
new PrependBasePath(self::FIXTURE_PATH),
23+
new PrependHostnameAsDirectory(),
2224
new AppendRequestPath(),
23-
new DefaultFileExtension()
25+
new AppendDefaultFileExtension('.html')
2426
)
2527
);
2628

27-
$actual = $pathBuilderBasedResolver->resolve(new Request('GET', 'http://example.com/foo'));
28-
$this->assertSame(self::FIXTURE_PATH . DIRECTORY_SEPARATOR . 'foo.json', $actual);
29+
$actual = $pathBuilderBasedResolver->resolve(new Request('GET', 'http://example.com/bar'));
30+
$this->assertSame(self::FIXTURE_PATH . '/example.com/bar.html', $actual);
2931
}
3032

3133
public function testUnresolvedException(): void
3234
{
3335
$pathBuilderBasedResolver = new PathBuilderBasedResolver($this->createMock(PathBuilderInterface::class));;
3436

3537
$this->expectException(UnresolvablePathException::class);
36-
$pathBuilderBasedResolver->resolve(new Request('GET', 'http://example.com/foo'));
38+
$pathBuilderBasedResolver->resolve(new Request('GET', 'http://example.com/bar'));
3739
}
3840
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<html>
2+
<head><title>bar</title></head>
3+
<body></body>
4+
</html>

0 commit comments

Comments
 (0)