Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Testing: Access component properties from the return value of $this->component() #38396

Merged
merged 5 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\View as ViewFacade;
use Illuminate\Support\MessageBag;
use Illuminate\Support\ViewErrorBag;
use Illuminate\Testing\TestComponent;
use Illuminate\Testing\TestView;
use Illuminate\View\View;

Expand Down Expand Up @@ -59,9 +60,11 @@ protected function component(string $componentClass, array $data = [])

$view = value($component->resolveView(), $data);

return $view instanceof View
? new TestView($view->with($component->data()))
: new TestView(view($view, $component->data()));
$view = $view instanceof View
? $view->with($component->data())
: view($view, $component->data());

return new TestComponent($component, $view);
}

/**
Expand Down
41 changes: 41 additions & 0 deletions src/Illuminate/Testing/TestComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Illuminate\Testing;

use Illuminate\Testing\TestView;
use Illuminate\View\Component;


class TestComponent extends TestView
{
/**
* The original view.
*
* @var \Illuminate\View\Component
*/
protected $component;

/**
* The rendered view contents.
*
* @var string
*/
protected $rendered;

/**
* Create a new test view instance.
*
* @param \Illuminate\View\Component $view
* @return void
*/
public function __construct(Component $component, $view)
{
$this->component = $component;
$this->rendered = $view->render();
}

public function __get($attribute)
{
return $this->component->{$attribute};
}
}
16 changes: 16 additions & 0 deletions tests/Foundation/Testing/Concerns/InteractsWithViewsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Foundation\Testing\Concerns;

use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
use Illuminate\View\Component;
use Orchestra\Testbench\TestCase;

class InteractsWithViewsTest extends TestCase
Expand All @@ -15,4 +16,19 @@ public function testBladeCorrectlyRendersString()

$this->assertEquals('test ', $string);
}

public function testComponentCanAccessPublicProperties()
{
$exampleComponent = new class extends Component
{
public $foo = 'bar';
public function render()
{
return '';
}
};
$component = $this->component(get_class($exampleComponent));

$this->assertEquals('bar', $component->foo);
}
}