-
-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[5.x] Support rendering model attributes in Antlers (#10869)
Co-authored-by: Duncan McClean <[email protected]> Co-authored-by: Jason Varga <[email protected]> Co-authored-by: John Koster <[email protected]>
- Loading branch information
1 parent
be8113d
commit 5df78e3
Showing
2 changed files
with
120 additions
and
3 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,72 @@ | ||
<?php | ||
|
||
namespace Tests\Antlers\Runtime; | ||
|
||
use Illuminate\Database\Eloquent\Casts\Attribute; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Statamic\Tags\Tags; | ||
use Tests\Antlers\ParserTestCase; | ||
|
||
class ModelTest extends ParserTestCase | ||
{ | ||
#[Test, DataProvider('modelProvider')] | ||
public function attributes_are_returned($attribute, $expected) | ||
{ | ||
$model = new FakeModel; | ||
$model->title = 'foo'; | ||
|
||
$this->assertSame($expected, $this->renderString("{{ model:$attribute }}", ['model' => $model])); | ||
} | ||
|
||
#[Test, DataProvider('modelProvider')] | ||
public function attributes_are_returned_in_tag_pair($attribute, $expected) | ||
{ | ||
$model = new FakeModel; | ||
$model->title = 'foo'; | ||
|
||
$this->assertSame($expected, $this->renderString("{{ model }}{{ $attribute }}{{ /model }}", ['model' => $model])); | ||
} | ||
|
||
public static function modelProvider() | ||
{ | ||
return [ | ||
'column' => ['title', 'foo'], | ||
'accessor' => ['alfa_bravo', 'charlie'], | ||
'old accessor' => ['delta_echo', 'foxtrot'], | ||
]; | ||
} | ||
|
||
#[Test] | ||
public function variable_references_receive_models() | ||
{ | ||
(new class extends Tags | ||
{ | ||
public static $handle = 'tag'; | ||
|
||
public function index() | ||
{ | ||
$src = $this->params->get('value'); | ||
|
||
return $src instanceof FakeModel ? 'Yes' : 'No'; | ||
} | ||
})::register(); | ||
|
||
$this->assertSame('Yes', $this->renderString('{{ %tag :value="model" }}', ['model' => new FakeModel])); | ||
} | ||
} | ||
|
||
class FakeModel extends \Illuminate\Database\Eloquent\Model | ||
{ | ||
public function alfaBravo(): Attribute | ||
{ | ||
return Attribute::make( | ||
get: fn () => 'charlie', | ||
); | ||
} | ||
|
||
public function getDeltaEchoAttribute() | ||
{ | ||
return 'foxtrot'; | ||
} | ||
} |