diff --git a/src/Illuminate/View/Component.php b/src/Illuminate/View/Component.php index 329035c47c81..150129ca65f9 100644 --- a/src/Illuminate/View/Component.php +++ b/src/Illuminate/View/Component.php @@ -324,6 +324,7 @@ protected function ignoredMethods() return array_merge([ 'data', 'render', + 'resolve', 'resolveView', 'shouldRender', 'view', diff --git a/tests/View/ViewComponentTest.php b/tests/View/ViewComponentTest.php index c7ac6cda96ec..a51be9f3cd59 100644 --- a/tests/View/ViewComponentTest.php +++ b/tests/View/ViewComponentTest.php @@ -5,6 +5,7 @@ use Illuminate\View\Component; use Illuminate\View\ComponentAttributeBag; use PHPUnit\Framework\TestCase; +use ReflectionMethod; class ViewComponentTest extends TestCase { @@ -19,6 +20,44 @@ public function testDataExposure() $this->assertSame('taylor', $variables['hello']('taylor')); } + public function testIgnoredMethodsAreNotExposedToViewData() + { + $component = new class extends Component + { + protected $except = ['goodbye']; + + public function render() + { + return 'test'; + } + + public function hello() + { + return 'hello world'; + } + + public function goodbye() + { + return 'goodbye'; + } + }; + + $data = $component->data(); + + $this->assertArrayHasKey('hello', $data); + $this->assertArrayNotHasKey('goodbye', $data); + + $reflectionMethod = new ReflectionMethod($component, 'ignoredMethods'); + + $reflectionMethod->setAccessible(true); + + $ignoredMethods = $reflectionMethod->invoke($component); + + foreach ($ignoredMethods as $method) { + $this->assertArrayNotHasKey($method, $data); + } + } + public function testAttributeParentInheritance() { $component = new TestViewComponent;