Skip to content

Commit

Permalink
switch to attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mikey179 committed Jan 28, 2024
1 parent c4395d3 commit 3d8b7f3
Show file tree
Hide file tree
Showing 56 changed files with 2,834 additions and 4,117 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/main/php/response/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function __construct(private Headers $headers)
*
* If reason phrase is null it will use the default reason phrase for given
* status code.
*
* @throws InvalidArgumentException when no reason phrase supplied and code is unknown
*/
public function setCode(int $code, string $reasonPhrase = null): self
{
Expand Down
3 changes: 2 additions & 1 deletion src/main/php/response/mimetypes/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use stubbles\sequence\Sequence;
use stubbles\streams\OutputStream;
use stubbles\webapp\response\Error;
use Traversable;

use function stubbles\sequence\castToArray;
use function stubbles\values\typeOf;
Expand Down Expand Up @@ -55,7 +56,7 @@ public function serialize(mixed $resource, OutputStream $out): OutputStream
{
if (is_scalar($resource) || $resource instanceof Error) {
$out->writeLine((string) $resource);
} elseif (is_array($resource) || $resource instanceof \Traversable) {
} elseif (is_array($resource) || $resource instanceof Traversable) {
$this->serializeIterable($resource, $out);
} elseif (is_object($resource)) {
$this->serializeIterable(castToArray($resource), $out);
Expand Down
4 changes: 4 additions & 0 deletions src/main/php/response/mimetypes/MimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function specialise(string $mimeType): self
* serializes resource to output stream
*
* It returns the output stream that was passed.
*
* @template T of OutputStream
* @param T $out
* @return T
*/
abstract public function serialize(
mixed $resource,
Expand Down
91 changes: 34 additions & 57 deletions src/test/php/UriPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
* file that was distributed with this source code.
*/
namespace stubbles\webapp;

use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

use function bovigo\assert\assertThat;
Expand All @@ -18,69 +23,53 @@
* Tests for stubbles\webapp\UriPath.
*
* @since 2.0.0
* @group core
*/
#[Group('core')]
class UriPathTest extends TestCase
{
/**
* instance to test
*
* @var UriPath
*/
private $uriPath;
private UriPath $uriPath;

protected function setUp(): void
{
$this->uriPath = new UriPath('/hello/{name}', '/hello/world/foo');
}

/**
* @test
*/
#[Test]
public function returnsGivenConfiguredPath(): void
{
assertThat($this->uriPath->configured(), equals('/hello/{name}'));
}

/**
* @test
* @since 4.0.0
*/
#[Test]
public function returnsGivenActualPath(): void
{
assertThat($this->uriPath->actual(), equals('/hello/world/foo'));
}

/**
* @test
* @since 4.0.0
*/
#[Test]
public function castingToStringReturnsActualPath(): void
{
assertThat((string) $this->uriPath, equals('/hello/world/foo'));
}

/**
* @return array<mixed[]>
*/
public static function providePathArguments(): array
public static function providePathArguments(): Generator
{
return [['/hello/mikey', '/hello/{name}', ['name' => 'mikey']],
['/hello/303/mikey', '/hello/{id}/{name}', ['id' => '303', 'name' => 'mikey']]
];
yield ['/hello/mikey', '/hello/{name}', ['name' => 'mikey']];
yield ['/hello/303/mikey', '/hello/{id}/{name}', ['id' => '303', 'name' => 'mikey']];
}

/**
* @param string $calledPath
* @param string $configuredPath
* @param array<string,string> $expectedArguments
* @test
* @dataProvider providePathArguments
*/
#[Test]
#[DataProvider('providePathArguments')]
public function returnsPathArguments(
string $calledPath,
string $configuredPath,
array $expectedArguments
string $calledPath,
string $configuredPath,
array $expectedArguments
): void {
$uriPath = new UriPath($configuredPath, $calledPath);
foreach ($expectedArguments as $name => $value) {
Expand All @@ -89,53 +78,41 @@ public function returnsPathArguments(
}
}

/**
* @test
*/
#[Test]
public function doesNotHaveNonGivenArgument(): void
{
assertFalse($this->uriPath->hasArgument('id'));
}

/**
* @test
*/
#[Test]
public function returnsNullForNonGivenArgument(): void
{
assertNull($this->uriPath->readArgument('id')->unsecure());
}

/**
* @return array<mixed[]>
*/
public static function provideRemainingPath(): array
public static function provideRemainingPath(): Generator
{
return [['/hello/mikey', '/hello/{name}', null],
['/hello/303/mikey', '/hello/{id}/{name}', ''],
['/hello/303/mikey/foo', '/hello/{id}/{name}', '/foo'],
['/hello', '/hello', ''],
['/hello/world;name', '/hello/[a-z0-9]+;?', 'name'],
['/hello/world', '/hello/?', 'world'],
['/', '/', '']
];
yield ['/hello/mikey', '/hello/{name}', null];
yield ['/hello/303/mikey', '/hello/{id}/{name}', ''];
yield ['/hello/303/mikey/foo', '/hello/{id}/{name}', '/foo'];
yield ['/hello', '/hello', ''];
yield ['/hello/world;name', '/hello/[a-z0-9]+;?', 'name'];
yield ['/hello/world', '/hello/?', 'world'];
yield ['/', '/', ''];
}

/**
* @test
* @dataProvider provideRemainingPath
*/
#[Test]
#[DataProvider('provideRemainingPath')]
public function returnsRemainingPath(
string $calledPath,
string $configuredPath,
?string $expected
string $calledPath,
string $configuredPath,
?string $expected
): void {
$uriPath = new UriPath($configuredPath, $calledPath);
assertThat($uriPath->remaining(), equals($expected));
}

/**
* @test
*/
#[Test]
public function returnsDefaultIfRemainingPathIsNull(): void
{
$this->uriPath = new UriPath('/hello/{name}', '/hello/world');
Expand Down
Loading

0 comments on commit 3d8b7f3

Please sign in to comment.