-
Notifications
You must be signed in to change notification settings - Fork 0
Add basic unit-tests #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Potherca
wants to merge
8
commits into
main
Choose a base branch
from
test/unit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8da9b5b
Change PHPUnit config to use `build/` directory for temporary files.
Potherca f1ed933
Delete dummy test.
Potherca 49218be
Add unit-tests for Exception class.
Potherca 06b3375
Add first draft unit-tests for Server class.
Potherca 839c373
Add `build/` directory to git ignore file.
Potherca f004a70
Add PHP 8.4 to GitHub Action (GHA) test matrix.
Potherca de099b8
Update `laminas-diactoros` from v2 to v3.
Potherca 6278319
Add fix in ServerTest for PHP 8.4 + PHPUnit 9 issue.
Potherca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Directories to ignore | ||
/build | ||
/vendor | ||
|
||
# Files to ignore | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
namespace Unit; | ||
|
||
use Pdsinterop\Solid\Resources\Exception; | ||
use PHPUnit\Framework\TestCase; | ||
use TypeError; | ||
|
||
/** | ||
* @coversDefaultClass \Pdsinterop\Solid\Resources\Exception | ||
* @covers ::create | ||
*/ | ||
class ExceptionTest extends TestCase | ||
{ | ||
const MOCK_CONTEXT = ['Test']; | ||
const MOCK_MESSAGE = 'Error: %s'; | ||
|
||
/** | ||
* @testdox Exception should complain when called without a message | ||
*/ | ||
public function testCreateWithoutMessage(): void | ||
{ | ||
$this->expectException(TypeError::class); | ||
$this->expectExceptionMessageMatches('/Too few arguments .+ 0 passed/'); | ||
|
||
Exception::create(); | ||
} | ||
|
||
/** | ||
* @testdox Exception should complain when called without a context | ||
*/ | ||
public function testCreateWithoutContext(): void | ||
{ | ||
$this->expectException(TypeError::class); | ||
$this->expectExceptionMessageMatches('/Too few arguments .+ 1 passed/'); | ||
|
||
Exception::create(self::MOCK_MESSAGE); | ||
} | ||
|
||
/** | ||
* @testdox Exception should complain when called with invalid message | ||
*/ | ||
public function testCreateWithInvalidMessage(): void | ||
{ | ||
$this->expectException(TypeError::class); | ||
$this->expectExceptionMessageMatches('/Argument #1 .+ must be of type string/'); | ||
|
||
Exception::create(null, self::MOCK_CONTEXT); | ||
} | ||
|
||
/** | ||
* @testdox Exception should complain when called with invalid context | ||
*/ | ||
public function testCreateWithInvalidContext(): void | ||
{ | ||
$this->expectException(TypeError::class); | ||
$this->expectExceptionMessageMatches('/Argument #2 .+ must be of type array/'); | ||
|
||
Exception::create(self::MOCK_MESSAGE, null); | ||
} | ||
|
||
/** | ||
* @testdox Exception should complain when given context does not match provided message format | ||
*/ | ||
public function testCreateWithIncorrectContext(): void | ||
{ | ||
$this->expectException(\ValueError::class); | ||
$this->expectExceptionMessageMatches('/The arguments array must contain 1 items?, 0 given/'); | ||
|
||
Exception::create(self::MOCK_MESSAGE, []); | ||
} | ||
|
||
/** | ||
* @testdox Exception should be created when called with valid message and context | ||
*/ | ||
public function testCreateWithMessageAndContext(): void | ||
{ | ||
$expected = Exception::class; | ||
$actual = Exception::create(self::MOCK_MESSAGE, self::MOCK_CONTEXT); | ||
|
||
$this->assertInstanceOf($expected, $actual); | ||
} | ||
|
||
/** | ||
* @testdox Created Exception should have the correct message when called with a message and context | ||
*/ | ||
public function testCreateFormatsErrorMessage(): void | ||
{ | ||
$expected = 'Error: Test'; | ||
$actual = Exception::create(self::MOCK_MESSAGE, self::MOCK_CONTEXT)->getMessage(); | ||
|
||
$this->assertSame($expected, $actual); | ||
} | ||
|
||
/** | ||
* @testdox Exception should be created when called with a message, context and previous exception | ||
*/ | ||
public function testCreateSetsPreviousException(): void | ||
{ | ||
$expected = new \Exception('Previous exception'); | ||
$actual = Exception::create(self::MOCK_MESSAGE, self::MOCK_CONTEXT, $expected)->getPrevious(); | ||
|
||
$this->assertSame($expected, $actual); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
<?php | ||
|
||
namespace Pdsinterop\Solid\Resources; | ||
|
||
use League\Flysystem\FilesystemInterface; | ||
use Pdsinterop\Rdf\Flysystem\Plugin\AsMime; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use Psr\Http\Message\UriInterface; | ||
use TypeError; | ||
|
||
/** | ||
* @coversDefaultClass \Pdsinterop\Solid\Resources\Server | ||
* @covers \Pdsinterop\Solid\Resources\Server | ||
*/ | ||
class ServerTest extends TestCase | ||
{ | ||
////////////////////////////////// FIXTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | ||
|
||
const MOCK_HTTP_METHOD = 'MOCK'; | ||
const MOCK_PATH = '/mock/path'; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
$phpUnitVersion = \PHPUnit\Runner\Version::id(); | ||
|
||
/* PHP 8.4.0 and PHPUnit 9 triggers a Deprecation Warning, which PHPUnit | ||
* promotes to an Exception, which causes tests to fail.This is fixed in | ||
* PHPUnit v10. As a workaround for v9, instead of loading the real | ||
* interface, a fixed interface is loaded on the fly. | ||
*/ | ||
if ( | ||
version_compare(PHP_VERSION, '8.4.0', '>=') | ||
&& version_compare($phpUnitVersion, '9.0.0', '>=') | ||
&& version_compare($phpUnitVersion, '10.0.0', '<') | ||
) { | ||
$file = __DIR__ . '/../../vendor/league/flysystem/src/FilesystemInterface.php'; | ||
$contents = file_get_contents($file); | ||
$contents = str_replace(['<?php','Handler $handler = null'], ['','?Handler $handler = null'], $contents); | ||
eval($contents); | ||
} | ||
} | ||
|
||
/////////////////////////////////// TESTS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | ||
|
||
/** | ||
* @testdox Server should complain when instantiated without a filesystem | ||
* @covers ::__construct | ||
*/ | ||
public function testServerConstructWithoutFilesystem() | ||
{ | ||
$this->expectException(TypeError::class); | ||
$this->expectExceptionMessageMatches('/Too few arguments .+ 0 passed/'); | ||
|
||
new Server(); | ||
} | ||
|
||
/** | ||
* @testdox Server should complain when instantiated without a response | ||
* @covers ::__construct | ||
*/ | ||
public function testServerConstructWithoutResponse() | ||
{ | ||
$this->expectException(TypeError::class); | ||
$this->expectExceptionMessageMatches('/Too few arguments .+ 1 passed/'); | ||
|
||
$mockFilesystem = $this->createMock(FilesystemInterface::class); | ||
|
||
new Server($mockFilesystem); | ||
} | ||
|
||
/** | ||
* @testdox Server should be instantiated when given a filesystem and a response | ||
* @covers ::__construct | ||
*/ | ||
public function testServerConstructWithFilesystemAndResponse() | ||
{ | ||
$mockFilesystem = $this->createMock(FilesystemInterface::class); | ||
$mockResponse = $this->createMock(ResponseInterface::class); | ||
|
||
$server = new Server($mockFilesystem, $mockResponse); | ||
|
||
$this->assertInstanceOf(Server::class, $server); | ||
} | ||
|
||
/** | ||
* @testdox Server should complain when asked to RespondToRequest without a request | ||
* @covers ::respondToRequest | ||
*/ | ||
public function testServerRespondToRequestWithoutRequest() | ||
{ | ||
$this->expectException(TypeError::class); | ||
$this->expectExceptionMessageMatches('/Too few arguments .+ 0 passed/'); | ||
|
||
$mockFilesystem = $this->createMock(FilesystemInterface::class); | ||
$mockResponse = $this->createMock(ResponseInterface::class); | ||
|
||
$server = new Server($mockFilesystem, $mockResponse); | ||
|
||
$server->respondToRequest(); | ||
} | ||
|
||
/** | ||
* @testdox Server should complain when asked to RespondToRequest with a request with an unknown HTTP method | ||
* | ||
* @covers ::respondToRequest | ||
* | ||
* @uses \Pdsinterop\Solid\Resources\Exception | ||
*/ | ||
public function testServerRespondToRequestWithUnknownHttpMethod() | ||
{ | ||
// Assert | ||
$this->expectException(Exception::class); | ||
$this->expectExceptionMessage(vsprintf(Server::ERROR_UNKNOWN_HTTP_METHOD, [self::MOCK_HTTP_METHOD])); | ||
|
||
// Arrange | ||
$mockRequest = $this->createMockRequest(); | ||
$mockResponse = $this->createMock(ResponseInterface::class); | ||
$mockFilesystem = $this->createMockFilesystem(); | ||
|
||
//Act | ||
$server = new Server($mockFilesystem, $mockResponse); | ||
$server->respondToRequest($mockRequest); | ||
} | ||
|
||
/** | ||
* @testdox Server should return provided response when asked to RespondToRequest with va lid request | ||
* | ||
* @covers ::respondToRequest | ||
*/ | ||
public function testServerRespondToRequestWithRequest() | ||
{ | ||
// Arrange | ||
$mockFilesystem = $this->createMockFilesystem(); | ||
$mockRequest = $this->createMockRequest('GET'); | ||
$expected = $this->createMockResponse(); | ||
|
||
// Act | ||
$server = new Server($mockFilesystem, $expected); | ||
$actual = $server->respondToRequest($mockRequest); | ||
|
||
// Assert | ||
$this->assertSame($expected, $actual); | ||
} | ||
|
||
////////////////////////////// MOCKS AND STUBS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | ||
|
||
public function createMockFilesystem(): FilesystemInterface|MockObject | ||
{ | ||
$mockFilesystem = $this->getMockBuilder(FilesystemInterface::class) | ||
->onlyMethods([ | ||
'addPlugin', 'copy', 'createDir', 'delete', 'deleteDir', 'get', 'getMetadata', 'getMimetype', 'getSize', 'getTimestamp', 'getVisibility', 'has', 'listContents', 'put', 'putStream', 'read', 'readAndDelete', 'readStream', 'rename', 'setVisibility', 'update', 'updateStream', 'write', 'writeStream' | ||
]) | ||
->addMethods(['asMime']) | ||
->getMock(); | ||
|
||
$mockAsMime = $this->getMockBuilder(AsMime::class) | ||
// ->onlyMethods(['getMimetype', 'getSize', 'getTimestamp']) | ||
->addMethods(['has']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$mockFilesystem->method('asMime')->willReturn($mockAsMime); | ||
|
||
return $mockFilesystem; | ||
} | ||
|
||
public function createMockRequest($httpMethod = self::MOCK_HTTP_METHOD): ServerRequestInterface|MockObject | ||
{ | ||
$mockRequest = $this->createMock(ServerRequestInterface::class); | ||
|
||
$mockUri = $this->createMock(UriInterface::class); | ||
$mockUri->method('getPath')->willReturn(self::MOCK_PATH); | ||
|
||
$mockBody = $this->createMock(StreamInterface::class); | ||
|
||
$mockRequest->method('getUri')->willReturn($mockUri); | ||
$mockRequest->method('getQueryParams')->willReturn([]); | ||
$mockRequest->method('getMethod')->willReturn($httpMethod); | ||
$mockRequest->method('getBody')->willReturn($mockBody); | ||
// $mockRequest->method('getMethod')->willReturn('GET'); | ||
$mockRequest->method('getHeaderLine')->willReturn(''); | ||
|
||
return $mockRequest; | ||
} | ||
|
||
public function createMockResponse(): ResponseInterface|MockObject | ||
{ | ||
$mockResponse = $this->createMock(ResponseInterface::class); | ||
$mockBody = $this->createMock(StreamInterface::class); | ||
|
||
$mockResponse->method('getBody')->willReturn($mockBody); | ||
$mockResponse->method('withStatus')->willReturnSelf(); | ||
|
||
return $mockResponse; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ylebre Not sure whether to go for
^3
or^v2 | ^v3
here...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless we are sure v3 works for older php versions, we should do 2|3.