Skip to content

Commit

Permalink
Fix tests namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Nov 2, 2024
1 parent f3cb979 commit a697ed2
Show file tree
Hide file tree
Showing 19 changed files with 179 additions and 268 deletions.
11 changes: 1 addition & 10 deletions lib/FileList.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ICanBoogie\HTTP;

use ArrayAccess;
Expand All @@ -19,7 +10,7 @@
use function count;

/**
* Representation of a list of request files.
* Represents a list of request files.
*
* @implements ArrayAccess<string, File>
* @implements IteratorAggregate<string, File>
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ parameters:
level: 5
paths:
- lib
scanFiles:
- tests/bootstrap.php
ignoreErrors:
- "#.*get_.* is unused#"
- "#.*set_.* is unused#"
8 changes: 5 additions & 3 deletions tests/FileInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
* file that was distributed with this source code.
*/

namespace ICanBoogie\HTTP;
namespace Test\ICanBoogie\HTTP;

use ICanBoogie\HTTP\FileInfo;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class FileInfoTest extends \PHPUnit\Framework\TestCase
class FileInfoTest extends TestCase
{
/**
* @param $pathname
Expand All @@ -25,7 +27,7 @@ public function test_resolve_type($pathname, $expected)
$this->assertEquals($expected, FileInfo::resolve_type($pathname));
}

public static function provide_test_resolve_type()
public static function provide_test_resolve_type(): array
{
$bytes = create_file();

Expand Down
18 changes: 11 additions & 7 deletions tests/FileListTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

namespace ICanBoogie\HTTP;
namespace Test\ICanBoogie\HTTP;

class FileListTest extends \PHPUnit\Framework\TestCase
use ICanBoogie\HTTP\File;
use ICanBoogie\HTTP\FileList;
use PHPUnit\Framework\TestCase;

class FileListTest extends TestCase
{
public function test_from_self_should_return_clone()
public function test_from_self_should_return_clone(): void
{
$files1 = new FileList();
$files2 = FileList::from($files1);
Expand All @@ -13,13 +17,13 @@ public function test_from_self_should_return_clone()
$this->assertNotSame($files1, $files2);
}

public function test_should_return_null_if_offset_not_defined()
public function test_should_return_null_if_offset_not_defined(): void
{
$files = new FileList();
$this->assertNull($files['undefined']);
}

public function test_should_create_instance_by_setting_offset()
public function test_should_create_instance_by_setting_offset(): void
{
$files = new FileList();
$files['one'] = [ 'pathname' => __FILE__ ];
Expand All @@ -28,7 +32,7 @@ public function test_should_create_instance_by_setting_offset()
$this->assertEquals(__FILE__, $files['one']->pathname);
}

public function test_should_remove_offset()
public function test_should_remove_offset(): void
{
$files = new FileList();
$files['one'] = [ 'pathname' => __FILE__ ];
Expand All @@ -37,7 +41,7 @@ public function test_should_remove_offset()
$this->assertNull($files['one']);
}

public function test_should_iterate()
public function test_should_iterate(): void
{
$expected = [

Expand Down
92 changes: 63 additions & 29 deletions tests/FileResponseTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
<?php

namespace ICanBoogie\HTTP;
namespace Test\ICanBoogie\HTTP;

use ICanBoogie\DateTime;
use ICanBoogie\HTTP\FileResponse;
use ICanBoogie\HTTP\Headers;
use ICanBoogie\HTTP\Request;
use ICanBoogie\HTTP\RequestMethod;
use ICanBoogie\HTTP\RequestOptions;
use ICanBoogie\HTTP\RequestRange;
use ICanBoogie\HTTP\ResponseStatus;
use LogicException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\Rule\InvokedCount;
Expand Down Expand Up @@ -36,7 +43,7 @@ public function test_closure_body(int $status, \Closure $expected_provider)

$response = $this
->getMockBuilder(FileResponse::class)
->setConstructorArgs([ __FILE__, Request::from()])
->setConstructorArgs([ __FILE__, Request::from() ])
->onlyMethods([ 'send_file', 'send_headers' ])
->getMock();
$response
Expand Down Expand Up @@ -93,8 +100,13 @@ public function test_invoke(string $cache_control, bool $is_modified, int $expec
}

#[DataProvider('provide_test_invoke_with_range')]
public function test_invoke_with_range(string $cache_control, bool $is_modified, bool $is_satisfiable, bool $is_total, int $expected)
{
public function test_invoke_with_range(
string $cache_control,
bool $is_modified,
bool $is_satisfiable,
bool $is_total,
int $expected
): void {
$headers = new Headers();
$headers['If-Range'] = $etag = "123";

Expand Down Expand Up @@ -171,22 +183,26 @@ public function test_send_body(): void
}

#[DataProvider('provide_test_get_content_type')]
public function test_get_content_type(string $expected, string $file, array $options = [], array $headers = [])
{
public function test_get_content_type(
string $expected,
string $file,
array $options = [],
array $headers = []
): void {
$response = new FileResponse($file, Request::from(), $options, $headers);
$this->assertEquals($expected, (string) $response->headers->content_type);
$this->assertEquals($expected, (string)$response->headers->content_type);
}

public static function provide_test_get_content_type(): array
{
return [

[ 'application/octet-stream', create_file() ],
[ 'text/plain', create_file(), [ FileResponse::OPTION_MIME => 'text/plain'] ],
[ 'text/plain', create_file(), [], [ 'Content-Type' => 'text/plain'] ],
[ 'text/plain', create_file(), [ FileResponse::OPTION_MIME => 'text/plain' ] ],
[ 'text/plain', create_file(), [], [ 'Content-Type' => 'text/plain' ] ],
[ 'image/png', create_image('.png') ],
[ 'text/plain', create_image('.png'), [ FileResponse::OPTION_MIME => 'text/plain'] ],
[ 'text/plain', create_image('.png'), [], [ 'Content-Type' => 'text/plain'] ],
[ 'text/plain', create_image('.png'), [ FileResponse::OPTION_MIME => 'text/plain' ] ],
[ 'text/plain', create_image('.png'), [], [ 'Content-Type' => 'text/plain' ] ],

];
}
Expand All @@ -208,7 +224,7 @@ public static function provide_test_get_etag(): array

[ $file_hash, $file ],
[ $file_hash_custom, $file, [ FileResponse::OPTION_ETAG => $file_hash_custom ] ],
[ $file_hash_custom, $file, [ ], [ 'ETag' => $file_hash_custom ] ],
[ $file_hash_custom, $file, [], [ 'ETag' => $file_hash_custom ] ],

];
}
Expand All @@ -232,8 +248,8 @@ public static function provide_test_get_expires(): array
[ $expires_default, $file ],
[ $expires2, $file, [ FileResponse::OPTION_EXPIRES => $expires2_str ] ],
[ $expires2, $file, [ FileResponse::OPTION_EXPIRES => $expires2 ] ],
[ $expires2, $file, [ ], [ 'Expires' => $expires2_str ] ],
[ $expires2, $file, [ ], [ 'Expires' => $expires2 ] ],
[ $expires2, $file, [], [ 'Expires' => $expires2_str ] ],
[ $expires2, $file, [], [ 'Expires' => $expires2 ] ],

];
}
Expand Down Expand Up @@ -273,13 +289,31 @@ public static function provide_test_get_is_modified(): array

return [

[ true, [ ] ],
[ true, [ 'If-Modified-Since' => (string) $modified_since ] ],
[ true, [ 'If-Modified-Since' => (string) $modified_since ], $modified_time_older ],
[ true, [ 'If-Modified-Since' => (string) $modified_since, 'If-None-Match' => uniqid() ], $modified_time_older ],
[ true, [ 'If-Modified-Since' => (string) $modified_since, 'If-None-Match' => uniqid() ], $modified_time_older ],
[ true, [ 'If-Modified-Since' => (string) $modified_since, 'If-None-Match' => $etag ], $modified_time_newer, $etag ],
[ false, [ 'If-Modified-Since' => (string) $modified_since, 'If-None-Match' => $etag ], $modified_time_older, $etag ],
[ true, [] ],
[ true, [ 'If-Modified-Since' => (string)$modified_since ] ],
[ true, [ 'If-Modified-Since' => (string)$modified_since ], $modified_time_older ],
[
true,
[ 'If-Modified-Since' => (string)$modified_since, 'If-None-Match' => uniqid() ],
$modified_time_older
],
[
true,
[ 'If-Modified-Since' => (string)$modified_since, 'If-None-Match' => uniqid() ],
$modified_time_older
],
[
true,
[ 'If-Modified-Since' => (string)$modified_since, 'If-None-Match' => $etag ],
$modified_time_newer,
$etag
],
[
false,
[ 'If-Modified-Since' => (string)$modified_since, 'If-None-Match' => $etag ],
$modified_time_older,
$etag
],

];
}
Expand All @@ -289,13 +323,13 @@ public function test_filename(string $file, string|bool $filename, string $expec
{
$response = new FileResponse($file, Request::from(), [ FileResponse::OPTION_FILENAME => $filename ]);

$this->assertEquals('binary', (string) $response->headers['Content-Transfer-Encoding']);
$this->assertEquals('File Transfer', (string) $response->headers['Content-Description']);
$this->assertEquals('binary', (string)$response->headers['Content-Transfer-Encoding']);
$this->assertEquals('File Transfer', (string)$response->headers['Content-Description']);
$this->assertEquals('attachment', $response->headers->content_disposition->type);
$this->assertEquals($expected, $response->headers->content_disposition->filename);
}

public static function provide_test_filename()
public static function provide_test_filename(): array
{
$file = create_file();
$filename = "Filename" . uniqid() . ".png";
Expand All @@ -309,7 +343,7 @@ public static function provide_test_filename()
}

#[DataProvider('provide_test_accept_ranges')]
public function test_accept_ranges(RequestMethod $method, string $type)
public function test_accept_ranges(RequestMethod $method, string $type): void
{
$request = Request::from([ Request::OPTION_URI => '/', 'method' => $method ]);

Expand All @@ -321,10 +355,10 @@ public function test_accept_ranges(RequestMethod $method, string $type)

/* @var $response FileResponse */

$this->assertStringContainsString("Accept-Ranges: $type", (string) $response);
$this->assertStringContainsString("Accept-Ranges: $type", (string)$response);
}

public static function provide_test_accept_ranges()
public static function provide_test_accept_ranges(): array
{
return [

Expand All @@ -337,7 +371,7 @@ public static function provide_test_accept_ranges()
}

#[DataProvider('provide_test_range_response')]
public function test_range_response(string $bytes, string $pathname, string $expected)
public function test_range_response(string $bytes, string $pathname, string $expected): void
{
$etag = sha1_file($pathname);

Expand Down Expand Up @@ -369,7 +403,7 @@ public function test_range_response(string $bytes, string $pathname, string $exp
$this->assertSame($expected, $content);
}

public static function provide_test_range_response()
public static function provide_test_range_response(): array
{
$pathname = create_file();
$data = file_get_contents($pathname);
Expand Down
Loading

0 comments on commit a697ed2

Please sign in to comment.