Skip to content

Commit 33461dc

Browse files
add test for view component class (#31665)
1 parent 568c1b3 commit 33461dc

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

tests/View/ViewComponentTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,34 @@ public function testDataExposure()
1717
$this->assertEquals('world', $variables['hello']());
1818
$this->assertEquals('taylor', $variables['hello']('taylor'));
1919
}
20+
21+
public function testPublicMethodsWithNoArgsAreEagerlyInvokedAndNotCached()
22+
{
23+
$component = new TestSampleViewComponent;
24+
25+
$this->assertEquals(0, $component->counter);
26+
$variables = $component->data();
27+
$this->assertEquals(1, $component->counter);
28+
29+
$this->assertEquals('noArgs val', $variables['noArgs']);
30+
$this->assertEquals(0, $variables['counter']);
31+
32+
// make sure non-public members are not invoked nor counted.
33+
$this->assertEquals(1, $component->counter);
34+
$this->assertArrayHasKey('publicHello', $variables);
35+
$this->assertArrayNotHasKey('protectedHello', $variables);
36+
$this->assertArrayNotHasKey('privateHello', $variables);
37+
38+
$this->assertArrayNotHasKey('protectedCounter', $variables);
39+
$this->assertArrayNotHasKey('privateCounter', $variables);
40+
41+
// test each time we invoke data(), the non-argument methods are invoked
42+
$this->assertEquals(1, $component->counter);
43+
$component->data();
44+
$this->assertEquals(2, $component->counter);
45+
$component->data();
46+
$this->assertEquals(3, $component->counter);
47+
}
2048
}
2149

2250
class TestViewComponent extends Component
@@ -33,3 +61,41 @@ public function hello($string = 'world')
3361
return $string;
3462
}
3563
}
64+
65+
class TestSampleViewComponent extends Component
66+
{
67+
public $counter = 0;
68+
69+
protected $protectedCounter = 0;
70+
71+
private $privateCounter = 0;
72+
73+
public function render()
74+
{
75+
return 'test';
76+
}
77+
78+
public function publicHello($string = 'world')
79+
{
80+
$this->counter = 100;
81+
82+
return $string;
83+
}
84+
85+
public function noArgs()
86+
{
87+
$this->counter++;
88+
89+
return 'noArgs val';
90+
}
91+
92+
protected function protectedHello()
93+
{
94+
$this->counter++;
95+
}
96+
97+
private function privateHello()
98+
{
99+
$this->counter++;
100+
}
101+
}

0 commit comments

Comments
 (0)