Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Aug 18, 2021
1 parent 1a37f3d commit 42a71fd
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function blade(string $template, array $data = [])
*
* @param string $componentClass
* @param \Illuminate\Contracts\Support\Arrayable|array $data
* @return \Illuminate\Testing\TestView
* @return \Illuminate\Testing\TestComponent
*/
protected function component(string $componentClass, array $data = [])
{
Expand Down
192 changes: 159 additions & 33 deletions src/Illuminate/Testing/TestComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,166 @@

namespace Illuminate\Testing;

use Illuminate\Testing\TestView;
use Illuminate\Testing\Assert as PHPUnit;
use Illuminate\Testing\Constraints\SeeInOrder;
use Illuminate\View\Component;


class TestComponent extends TestView
class TestComponent
{
/**
* 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};
}
/**
* The original component.
*
* @var \Illuminate\View\Component
*/
public $component;

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

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

$this->rendered = $view->render();
}

/**
* Assert that the given string is contained within the rendered component.
*
* @param string $value
* @param bool $escape
* @return $this
*/
public function assertSee($value, $escape = true)
{
$value = $escape ? e($value) : $value;

PHPUnit::assertStringContainsString((string) $value, $this->rendered);

return $this;
}

/**
* Assert that the given strings are contained in order within the rendered component.
*
* @param array $values
* @param bool $escape
* @return $this
*/
public function assertSeeInOrder(array $values, $escape = true)
{
$values = $escape ? array_map('e', ($values)) : $values;

PHPUnit::assertThat($values, new SeeInOrder($this->rendered));

return $this;
}

/**
* Assert that the given string is contained within the rendered component text.
*
* @param string $value
* @param bool $escape
* @return $this
*/
public function assertSeeText($value, $escape = true)
{
$value = $escape ? e($value) : $value;

PHPUnit::assertStringContainsString((string) $value, strip_tags($this->rendered));

return $this;
}

/**
* Assert that the given strings are contained in order within the rendered component text.
*
* @param array $values
* @param bool $escape
* @return $this
*/
public function assertSeeTextInOrder(array $values, $escape = true)
{
$values = $escape ? array_map('e', ($values)) : $values;

PHPUnit::assertThat($values, new SeeInOrder(strip_tags($this->rendered)));

return $this;
}

/**
* Assert that the given string is not contained within the rendered component.
*
* @param string $value
* @param bool $escape
* @return $this
*/
public function assertDontSee($value, $escape = true)
{
$value = $escape ? e($value) : $value;

PHPUnit::assertStringNotContainsString((string) $value, $this->rendered);

return $this;
}

/**
* Assert that the given string is not contained within the rendered component text.
*
* @param string $value
* @param bool $escape
* @return $this
*/
public function assertDontSeeText($value, $escape = true)
{
$value = $escape ? e($value) : $value;

PHPUnit::assertStringNotContainsString((string) $value, strip_tags($this->rendered));

return $this;
}

/**
* Get the string contents of the rendered component.
*
* @return string
*/
public function __toString()
{
return $this->rendered;
}

/**
* Dynamically access properties on the underlying component.
*
* @param string $attribute
* @return mixed
*/
public function __get($attribute)
{
return $this->component->{$attribute};
}

/**
* Dynamically call methods on the underlying component.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->component->{$method}(...$parameters);
}
}
9 changes: 8 additions & 1 deletion tests/Foundation/Testing/Concerns/InteractsWithViewsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ public function testComponentCanAccessPublicProperties()
$exampleComponent = new class extends Component
{
public $foo = 'bar';
public function speak()
{
return 'hello';
}
public function render()
{
return '';
return 'rendered content';
}
};

$component = $this->component(get_class($exampleComponent));

$this->assertEquals('bar', $component->foo);
$this->assertEquals('hello', $component->speak());
$component->assertSee('content');
}
}

0 comments on commit 42a71fd

Please sign in to comment.