-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Duncan McClean <[email protected]> Co-authored-by: Jason Varga <[email protected]>
- Loading branch information
1 parent
62524db
commit 73c48a4
Showing
2 changed files
with
116 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Tests\Modifiers; | ||
|
||
use Statamic\Modifiers\Modify; | ||
use Tests\TestCase; | ||
|
||
class AttributeTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* | ||
* @dataProvider attributeProvider | ||
*/ | ||
public function it_converts_to_attribute($value, $expected) | ||
{ | ||
$this->assertEquals($expected, $this->modify($value, 'foo')); | ||
} | ||
|
||
public static function attributeProvider() | ||
{ | ||
return [ | ||
'string' => ['bar baz', ' foo="bar baz"'], | ||
'entities' => ['{<!&>}', ' foo="{<!&>}"'], | ||
'integer' => [1, ' foo="1"'], | ||
'integer > 1' => [2, ' foo="2"'], | ||
'negative integer' => [-1, ' foo="-1"'], | ||
'float' => [1.5, ' foo="1.5"'], | ||
'empty string' => ['', ''], | ||
'true' => [true, ' foo'], | ||
'false' => [false, ''], | ||
'array' => [['one' => ['two' => 'three']], ' foo="{"one":{"two":"three"}}"'], | ||
'empty array' => [[], ''], | ||
'collection' => [collect(['one' => 'two']), ' foo="{"one":"two"}"'], | ||
'empty collection' => [collect(), ''], | ||
'object with __toString' => [new AttributeTestStringable, ' foo="Test"'], | ||
]; | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_exception_without_argument() | ||
{ | ||
$this->expectException(\Exception::class); | ||
$this->expectExceptionMessage('Attribute name is required.'); | ||
|
||
$this->modify('value', null); | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_exception_when_value_is_an_object_without_toString_method() | ||
{ | ||
$this->expectException(\Error::class); | ||
$this->expectExceptionMessage('Object of class Tests\Modifiers\AttributeTestNotStringable could not be converted to string'); | ||
|
||
$this->modify(new AttributeTestNotStringable, 'foo'); | ||
} | ||
|
||
private function modify($value, $attribute) | ||
{ | ||
return Modify::value($value)->attribute($attribute)->fetch(); | ||
} | ||
} | ||
|
||
class AttributeTestStringable | ||
{ | ||
public function __toString() | ||
{ | ||
return 'Test'; | ||
} | ||
} | ||
|
||
class AttributeTestNotStringable | ||
{ | ||
} |