Skip to content
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

Iri::__construct(): allow $iri to be stringable #613

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Iri.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use WpOrg\Requests\Exception\InvalidArgument;
use WpOrg\Requests\Ipv6;
use WpOrg\Requests\Port;
use WpOrg\Requests\Utility\InputValidator;

/**
* IRI parser/serialiser/normaliser
Expand Down Expand Up @@ -245,13 +246,13 @@ public function __unset($name) {
/**
* Create a new IRI object, from a specified string
*
* @param string|null $iri
* @param string|Stringable|null $iri
*
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $iri argument is not a string nor null.
* @throws \WpOrg\Requests\Exception\InvalidArgument When the passed $iri argument is not a string, Stringable or null.
*/
public function __construct($iri = null) {
if ($iri !== null && is_string($iri) === false) {
throw InvalidArgument::create(1, '$iri', 'string|null', gettype($iri));
if ($iri !== null && InputValidator::is_string_or_stringable($iri) === false) {
throw InvalidArgument::create(1, '$iri', 'string|Stringable|null', gettype($iri));
}

$this->set_iri($iri);
Expand Down Expand Up @@ -732,6 +733,9 @@ protected function set_iri($iri) {
if ($iri === null) {
return true;
}

$iri = (string) $iri;

if (isset($cache[$iri])) {
list($this->scheme,
$this->iuserinfo,
Expand All @@ -744,7 +748,7 @@ protected function set_iri($iri) {
return $return;
}

$parsed = $this->parse_iri((string) $iri);
$parsed = $this->parse_iri($iri);

$return = $this->set_scheme($parsed['scheme'])
&& $this->set_authority($parsed['authority'])
Expand Down
20 changes: 16 additions & 4 deletions tests/IriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

namespace WpOrg\Requests\Tests;

use stdClass;
use WpOrg\Requests\Exception\InvalidArgument;
use WpOrg\Requests\Iri;
use WpOrg\Requests\Tests\Fixtures\StringableObject;
Expand Down Expand Up @@ -416,6 +417,17 @@ public function testBadPort()
$this->assertNull($iri->port);
}

/**
* Safeguard that the constructor can accept Stringable objects as $iri.
*
* @covers \WpOrg\Requests\Iri::__construct
*
* @return void
*/
public function testConstructorAcceptsStringableIri() {
$this->assertInstanceOf(Iri::class, new Iri(new StringableObject('https://example.com/')));
}

/**
* Tests receiving an exception when an invalid input type is passed to the constructor.
*
Expand All @@ -429,7 +441,7 @@ public function testBadPort()
*/
public function testConstructorInvalidInput($iri) {
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage('Argument #1 ($iri) must be of type string|null');
$this->expectExceptionMessage('Argument #1 ($iri) must be of type string|Stringable|null');

new Iri($iri);
}
Expand All @@ -441,9 +453,9 @@ public function testConstructorInvalidInput($iri) {
*/
public function dataConstructorInvalidInput() {
return array(
'boolean false' => array(false),
'float' => array(1.1),
'stringable object' => array(new StringableObject('value')),
'boolean false' => array(false),
'float' => array(1.1),
'non-stringable object' => array(new stdClass('value')),
);
}
}