Skip to content

Commit 5cc2a6f

Browse files
committed
Adds missing tests and behavior
1 parent bca9732 commit 5cc2a6f

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace spec\Http\Discovery\UriFactory;
4+
5+
use Psr\Http\Message\UriInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class DiactorosFactorySpec extends ObjectBehavior
9+
{
10+
function let()
11+
{
12+
if (!class_exists('Zend\Diactoros\Uri')) {
13+
throw new SkippingException('Diactoros is not available');
14+
}
15+
}
16+
17+
function it_is_initializable()
18+
{
19+
$this->shouldHaveType('Http\Discovery\UriFactory\DiactorosFactory');
20+
}
21+
22+
function it_is_a_uri_factory()
23+
{
24+
$this->shouldImplement('Http\Message\UriFactory');
25+
}
26+
27+
function it_creates_a_uri_from_string()
28+
{
29+
$this->createUri('http://php-http.org')->shouldHaveType('Psr\Http\Message\UriInterface');
30+
}
31+
32+
function it_creates_a_uri_from_uri(UriInterface $uri)
33+
{
34+
$this->createUri($uri)->shouldReturn($uri);
35+
}
36+
37+
function it_throws_an_exception_when_uri_is_invalid()
38+
{
39+
$this->shouldThrow('InvalidArgumentException')->duringCreateUri(null);
40+
}
41+
}

spec/UriFactory/GuzzleFactorySpec.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace spec\Http\Discovery\UriFactory;
4+
5+
use Psr\Http\Message\UriInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
class GuzzleFactorySpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->shouldHaveType('Http\Discovery\UriFactory\GuzzleFactory');
13+
}
14+
15+
function it_is_a_uri_factory()
16+
{
17+
$this->shouldImplement('Http\Message\UriFactory');
18+
}
19+
20+
function it_creates_a_uri_from_string()
21+
{
22+
$this->createUri('http://php-http.org')->shouldHaveType('Psr\Http\Message\UriInterface');
23+
}
24+
25+
function it_creates_a_uri_from_uri(UriInterface $uri)
26+
{
27+
$this->createUri($uri)->shouldReturn($uri);
28+
}
29+
30+
function it_throws_an_exception_when_uri_is_invalid()
31+
{
32+
$this->shouldThrow('InvalidArgumentException')->duringCreateUri(null);
33+
}
34+
}

src/UriFactory/DiactorosFactory.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Http\Discovery\UriFactory;
1313

1414
use Http\Message\UriFactory;
15+
use Psr\Http\Message\UriInterface;
1516
use Zend\Diactoros\Uri;
1617

1718
/**
@@ -26,6 +27,12 @@ class DiactorosFactory implements UriFactory
2627
*/
2728
public function createUri($uri)
2829
{
29-
return new Uri($uri);
30+
if ($uri instanceof UriInterface) {
31+
return $uri;
32+
} elseif (is_string($uri)) {
33+
return new Uri($uri);
34+
}
35+
36+
throw new \InvalidArgumentException('URI must be a string or UriInterface');
3037
}
3138
}

0 commit comments

Comments
 (0)