Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.0.3] - 2025-04-26

### Added
- Prepend / Append composition for handy path parts

## [0.0.2] - 2025-04-22

### Added
Expand All @@ -16,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- StaticHttpClient, HostnameAwareResolver classes

[Unreleased]: https://github.com/FreeElephants/static-http-client/compare/0.0.2...HEAD
[Unreleased]: https://github.com/FreeElephants/static-http-client/compare/0.0.3...HEAD
[0.0.3]: https://github.com/FreeElephants/static-http-client/releases/tag/0.0.3
[0.0.2]: https://github.com/FreeElephants/static-http-client/releases/tag/0.0.2
[0.0.1]: https://github.com/FreeElephants/static-http-client/releases/tag/0.0.1
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public function test()
$responseFactory,
new \FreeElephants\StaticHttpClient\PathResolver\PathBuilderBasedResolver(
new \FreeElephants\StaticHttpClient\PathBuilder\Composite(
new \FreeElephants\StaticHttpClient\PathBuilder\BasePath(__DIR__),
new \FreeElephants\StaticHttpClient\PathBuilder\HostnameAsDirectory(),
new \FreeElephants\StaticHttpClient\PathBuilder\PrependBasePath(__DIR__),
new \FreeElephants\StaticHttpClient\PathBuilder\PrependHostnameAsDirectory(),
new \FreeElephants\StaticHttpClient\PathBuilder\AppendRequestPath(),
new \FreeElephants\StaticHttpClient\PathBuilder\DefaultFileExtension('.json'),
new \FreeElephants\StaticHttpClient\PathBuilder\AppendDefaultFileExtension('.json'),
)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Psr\Http\Message\RequestInterface;

class DefaultFileExtension implements PathBuilderInterface
class AppendDefaultFileExtension implements PathBuilderInterface
{

private string $fileExtension;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace FreeElephants\StaticHttpClient\PathBuilder;

use Psr\Http\Message\RequestInterface;

class AppendHostnameAsDirectory implements PathBuilderInterface
{
public function build(RequestInterface $request, string $path = ''): string
{
return $path . DIRECTORY_SEPARATOR . $request->getUri()->getHost();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

use Psr\Http\Message\RequestInterface;

class BasePath implements PathBuilderInterface
class PrependBasePath implements PathBuilderInterface
{
private string $basePath;

public function __construct(string $basePath)
{

$this->basePath = $basePath;
$this->basePath = rtrim($basePath, DIRECTORY_SEPARATOR);
}

public function build(RequestInterface $request, string $path = ''): string
{
return $this->basePath . $path;
return $this->basePath . DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Psr\Http\Message\RequestInterface;

class HostnameAsDirectory implements PathBuilderInterface
class PrependHostnameAsDirectory implements PathBuilderInterface
{
public function build(RequestInterface $request, string $path = ''): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ public function testBuild(): void
{
$composite = new Composite(
new AppendRequestPath(),
new DefaultFileExtension(),
new HostnameAsDirectory(),
new PrependHostnameAsDirectory(),
new PrependBasePath('/root'),
new AppendDefaultFileExtension(),
);

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

$this->assertSame('example.com/foo.json', $path, 'Composite should build path from all builders');
$this->assertSame('/root/example.com/foo.json', $path, 'Composite should build path from all builders');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use FreeElephants\StaticHttpClient\AbstractTestCase;
use FreeElephants\StaticHttpClient\PathBuilder\AppendRequestPath;
use FreeElephants\StaticHttpClient\PathBuilder\BasePath;
use FreeElephants\StaticHttpClient\PathBuilder\PrependBasePath;
use FreeElephants\StaticHttpClient\PathBuilder\Composite;
use FreeElephants\StaticHttpClient\PathBuilder\DefaultFileExtension;
use FreeElephants\StaticHttpClient\PathBuilder\AppendDefaultFileExtension;
use FreeElephants\StaticHttpClient\PathBuilder\PathBuilderInterface;
use FreeElephants\StaticHttpClient\PathBuilder\PrependHostnameAsDirectory;
use FreeElephants\StaticHttpClient\PathResolver\Exception\UnresolvablePathException;
use Nyholm\Psr7\Request;
use PHPUnit\Framework\TestCase;
Expand All @@ -18,21 +19,22 @@ public function testResolve(): void
{
$pathBuilderBasedResolver = new PathBuilderBasedResolver(
new Composite(
new BasePath(self::FIXTURE_PATH),
new AppendRequestPath(),
new DefaultFileExtension()
new PrependHostnameAsDirectory(),
new PrependBasePath(self::FIXTURE_PATH),
new AppendDefaultFileExtension('.html')
)
);

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

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

$this->expectException(UnresolvablePathException::class);
$pathBuilderBasedResolver->resolve(new Request('GET', 'http://example.com/foo'));
$pathBuilderBasedResolver->resolve(new Request('GET', 'http://example.com/bar'));
}
}
4 changes: 4 additions & 0 deletions tests/_fixtures/example.com/bar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html>
<head><title>bar</title></head>
<body></body>
</html>