Skip to content

Commit 2683452

Browse files
[7.x ] Add view components tests (#31669)
* add tests for components * Merge branch '7.x' into add_view_components_tests
1 parent 72b6f4c commit 2683452

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

tests/View/ViewComponentTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@ public function testPublicMethodsWithNoArgsAreEagerlyInvokedAndNotCached()
4646
$this->assertEquals(3, $component->counter);
4747
}
4848

49+
public function testItIgnoresExceptedMethodsAndProperties()
50+
{
51+
$component = new TestExceptedViewComponent();
52+
$variables = $component->data();
53+
54+
// Ignored methods (with no args) are not invoked behind the scenes.
55+
$this->assertEquals('Otwell', $component->taylor);
56+
57+
$this->assertArrayNotHasKey('hello', $variables);
58+
$this->assertArrayNotHasKey('hello2', $variables);
59+
$this->assertArrayNotHasKey('taylor', $variables);
60+
}
61+
62+
public function testMethodsOverridePropertyValues()
63+
{
64+
$component = new TestHelloPropertyHelloMethodComponent();
65+
$variables = $component->data();
66+
$this->assertArrayHasKey('hello', $variables);
67+
$this->assertEquals('world', $variables['hello']());
68+
69+
// protected methods do not override public properties.
70+
$this->assertArrayHasKey('world', $variables);
71+
$this->assertEquals('world property', $variables['world']);
72+
}
73+
4974
public function testAttributesAreMergedNotOverwritten()
5075
{
5176
$component = new TestDefaultAttributesComponent;
@@ -111,6 +136,50 @@ private function privateHello()
111136
}
112137
}
113138

139+
class TestExceptedViewComponent extends Component
140+
{
141+
protected $except = ['hello', 'hello2', 'taylor'];
142+
143+
public $taylor = 'Otwell';
144+
145+
public function hello($string = 'world')
146+
{
147+
return $string;
148+
}
149+
150+
public function hello2()
151+
{
152+
return $this->taylor = '';
153+
}
154+
155+
public function render()
156+
{
157+
return 'test';
158+
}
159+
}
160+
161+
class TestHelloPropertyHelloMethodComponent extends Component
162+
{
163+
public function render()
164+
{
165+
return 'test';
166+
}
167+
168+
public $hello = 'hello property';
169+
170+
public $world = 'world property';
171+
172+
public function hello($string = 'world')
173+
{
174+
return $string;
175+
}
176+
177+
protected function world($string = 'world')
178+
{
179+
return $string;
180+
}
181+
}
182+
114183
class TestDefaultAttributesComponent extends Component
115184
{
116185
public function __construct()

0 commit comments

Comments
 (0)