-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests and fixed issues with NoCandidateFoundException (#132)
- Loading branch information
Showing
10 changed files
with
216 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit bootstrap="./vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true"> | ||
|
||
<testsuites> | ||
<testsuite name="HTTPlug unit tests"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<groups> | ||
<exclude> | ||
<group>NothingInstalled</group> | ||
</exclude> | ||
</groups> | ||
</phpunit> |
This file contains 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 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 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 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,23 @@ | ||
<?php | ||
|
||
namespace tests\Http\Discovery\Exception; | ||
|
||
use Http\Discovery\Exception as DiscoveryException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class InitializeExceptionTest extends TestCase | ||
{ | ||
public function testInitialize() | ||
{ | ||
$e[] = new DiscoveryException\ClassInstantiationFailedException(); | ||
$e[] = new DiscoveryException\NotFoundException(); | ||
$e[] = new DiscoveryException\PuliUnavailableException(); | ||
$e[] = new DiscoveryException\StrategyUnavailableException(); | ||
$e[] = new DiscoveryException\NoCandidateFoundException('CommonClasses', []); | ||
$e[] = DiscoveryException\DiscoveryFailedException::create($e); | ||
|
||
foreach ($e as $exception) { | ||
$this->assertInstanceOf(\Http\Discovery\Exception::class, $exception); | ||
} | ||
} | ||
} |
This file contains 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,67 @@ | ||
<?php | ||
|
||
namespace tests\Http\Discovery\Exception; | ||
|
||
use Http\Discovery\Exception as DiscoveryException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class NoCandidateFoundExceptionTest extends TestCase | ||
{ | ||
public function testInitialize() | ||
{ | ||
$candidates = [ | ||
['class' => 'Foo', 'condition' => true], | ||
['class' => 'Bar', 'condition' => false], | ||
]; | ||
$exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates); | ||
$this->assertInstanceOf(\Http\Discovery\Exception::class, $exception); | ||
$this->assertStringStartsWith('No valid candidate found using strategy "Foobar".', $exception->getMessage()); | ||
} | ||
|
||
public function testInitializeWithArrayCallable() | ||
{ | ||
$candidates = [ | ||
['class' => ['AnyClass', 'anyFunction'], 'condition' => true], | ||
]; | ||
|
||
$exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates); | ||
$this->assertInstanceOf(\Http\Discovery\Exception::class, $exception); | ||
} | ||
|
||
public function testInitializeWithObjectCallable() | ||
{ | ||
$obj = new \stdClass(); | ||
$candidates = [ | ||
['class' => [$obj, 'anyFunction'], 'condition' => true], | ||
]; | ||
|
||
$exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates); | ||
$this->assertInstanceOf(\Http\Discovery\Exception::class, $exception); | ||
} | ||
|
||
public function testInitializeWithClosure() | ||
{ | ||
$obj = \Closure::fromCallable(function () { | ||
$x = 2; | ||
}); | ||
$candidates = [ | ||
['class' => $obj, 'condition' => true], | ||
]; | ||
|
||
$exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates); | ||
$this->assertInstanceOf(\Http\Discovery\Exception::class, $exception); | ||
} | ||
|
||
public function testInitializeWithAnonymousFunction() | ||
{ | ||
$func = function () { | ||
$x = 2; | ||
}; | ||
$candidates = [ | ||
['class' => $func, 'condition' => true], | ||
]; | ||
|
||
$exception = new DiscoveryException\NoCandidateFoundException('Foobar', $candidates); | ||
$this->assertInstanceOf(\Http\Discovery\Exception::class, $exception); | ||
} | ||
} |
This file contains 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,47 @@ | ||
<?php | ||
|
||
namespace tests\Http\Discovery; | ||
|
||
use Http\Discovery\Exception\NotFoundException; | ||
use Http\Discovery\Psr17FactoryDiscovery; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\RequestFactoryInterface; | ||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Message\ServerRequestFactoryInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
use Psr\Http\Message\UploadedFileFactoryInterface; | ||
use Psr\Http\Message\UriFactoryInterface; | ||
|
||
class Psr17FactoryDiscoveryTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider getFactories | ||
*/ | ||
public function testFind($method, $interface) | ||
{ | ||
$callable = [Psr17FactoryDiscovery::class, $method]; | ||
$client = $callable(); | ||
$this->assertInstanceOf($interface, $client); | ||
} | ||
|
||
/** | ||
* @group NothingInstalled | ||
* @dataProvider getFactories | ||
*/ | ||
public function testNotFound($method) | ||
{ | ||
$callable = [Psr17FactoryDiscovery::class, $method]; | ||
$this->expectException(NotFoundException::class); | ||
$callable(); | ||
} | ||
|
||
public function getFactories() | ||
{ | ||
yield ['findRequestFactory', RequestFactoryInterface::class]; | ||
yield ['findResponseFactory', ResponseFactoryInterface::class]; | ||
yield ['findServerRequestFactory', ServerRequestFactoryInterface::class]; | ||
yield ['findStreamFactory', StreamFactoryInterface::class]; | ||
yield ['findUploadedFileFactory', UploadedFileFactoryInterface::class]; | ||
yield ['findUrlFactory', UriFactoryInterface::class]; | ||
} | ||
} |
This file contains 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,26 @@ | ||
<?php | ||
|
||
namespace tests\Http\Discovery; | ||
|
||
use Http\Discovery\Exception\NotFoundException; | ||
use Http\Discovery\Psr18ClientDiscovery; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Client\ClientInterface; | ||
|
||
class Psr18ClientDiscoveryTest extends TestCase | ||
{ | ||
public function testFind() | ||
{ | ||
$client = Psr18ClientDiscovery::find(); | ||
$this->assertInstanceOf(ClientInterface::class, $client); | ||
} | ||
|
||
/** | ||
* @group NothingInstalled | ||
*/ | ||
public function testNotFound() | ||
{ | ||
$this->expectException(NotFoundException::class); | ||
$client = Psr18ClientDiscovery::find(); | ||
} | ||
} |