Skip to content

Commit

Permalink
Temp
Browse files Browse the repository at this point in the history
  • Loading branch information
andaniel05 committed Apr 22, 2020
1 parent 28ec4b9 commit 93123ec
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 18 deletions.
20 changes: 3 additions & 17 deletions src/Event/RenderEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@

namespace ThenLabs\ComposedViews\Event;

use ThenLabs\ComposedViews\ProxyToCrawlerTrait;
use ThenLabs\Components\Event\Event;
use Wa72\HtmlPageDom\HtmlPageCrawler;
use BadMethodCallException;

/**
* @author Andy Daniel Navarro Taño <andaniel05@gmail.com>
*/
class RenderEvent extends Event
{
use ProxyToCrawlerTrait;

/**
* @var string|null
*/
protected $view;

/**
* @var HtmlPageCrawler
*/
protected $crawler;

/**
* @var array
*/
Expand All @@ -47,15 +44,4 @@ public function getData(): array
{
return $this->data;
}

public function __call($method, $arguments)
{
$callback = [$this->crawler, $method];

if (is_callable($callback)) {
return call_user_func_array($callback, $arguments);
} else {
throw new BadMethodCallException("Unknow method '{$method}'.");
}
}
}
29 changes: 29 additions & 0 deletions src/ProxyToCrawlerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace ThenLabs\ComposedViews;

use Wa72\HtmlPageDom\HtmlPageCrawler;
use BadMethodCallException;

/**
* @author Andy Daniel Navarro Taño <andaniel05@gmail.com>
*/
trait ProxyToCrawlerTrait
{
/**
* @var HtmlPageCrawler
*/
protected $crawler;

public function __call($method, $arguments)
{
$callback = [$this->crawler, $method];

if (is_callable($callback)) {
return call_user_func_array($callback, $arguments);
} else {
throw new BadMethodCallException("Unknow method '{$method}'.");
}
}
}
1 change: 1 addition & 0 deletions src/TextView.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class TextView extends AbstractView implements DependencyInterface
{
use EditableDependencyTrait;
use ProxyToCrawlerTrait;

protected $content;

Expand Down
2 changes: 1 addition & 1 deletion tests/Event/RenderEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
setTestCaseClass(TestCase::class);

testCase('RenderEventTest.php', function () {
test('is a proxy to the proxyCrawler property', function () {
test('is a proxy to the crawler property', function () {
$method = uniqid('method');
$argument = uniqid();
$result = uniqid();
Expand Down
35 changes: 35 additions & 0 deletions tests/TextViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use ThenLabs\ComposedViews\AbstractView;
use ThenLabs\ComposedViews\TextView;
use ThenLabs\Components\DependencyInterface;
use Wa72\HtmlPageDom\HtmlPageCrawler;
use BadMethodCallException;

setTestCaseNamespace(__NAMESPACE__);
setTestCaseClass(TestCase::class);
Expand All @@ -27,6 +29,39 @@
$this->assertEquals($this->content, $this->view->render());
});

test('is a proxy to the crawler property', function () {
$method = uniqid('method');
$argument = uniqid();
$result = uniqid();

$crawler = $this->getMockBuilder(HtmlPageCrawler::class)
->disableOriginalConstructor()
->setMethods([$method])
->getMock();
$crawler->expects($this->once())
->method($method)
->with($this->equalTo($argument))
->willReturn($result);

$textView = new TextView('');

// install the crawler inside the view.
(function () use ($crawler) {
$this->crawler = $crawler;
})->call($textView);

$this->assertEquals($result, $textView->{$method}($argument));
});

test('throwns an BadMethodCallException when the called method not exists', function () {
$method = uniqid('method');
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage("Unknow method '{$method}'.");

$textView = new TextView('');
$textView->{$method}();
});

testCase('$view->setName($name)', function () {
setUp(function () {
$this->name = uniqid();
Expand Down

0 comments on commit 93123ec

Please sign in to comment.