forked from thephpleague/plates
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
924ed1b
commit e202d34
Showing
6 changed files
with
324 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
namespace DMJohnson\Contemplate\Template; | ||
|
||
/** | ||
* Wrapper for controller objects. | ||
* | ||
* The controller should be a function or other callable which can be imported via the resolver. | ||
*/ | ||
class Controller extends Resolvable{ | ||
/** | ||
* Execute the controller code and return its value | ||
*/ | ||
public function call(array $args = []){ | ||
return call_user_func_array($this->import(), $args); | ||
} | ||
|
||
/** Alias for call() */ | ||
public function __invoke(...$args){ | ||
return call_user_func_array($this->import(), $args); | ||
} | ||
|
||
/** Shortcut for `$this->engine->addData()` */ | ||
public function addData(array $data=[], $templates=null){ | ||
return $this->engine->addData($data, $templates); | ||
} | ||
|
||
/** Add data for the template with the same name as this controller */ | ||
public function addDataAssociated(array $data=[]){ | ||
return $this->engine->addData($data, $this->name->getName()); | ||
} | ||
|
||
/** | ||
* Delegate this action, or part of this action, to a different controller | ||
* (e.g. a form handler for a specific form on a page) | ||
* | ||
* @param string $name The controller to delegate to | ||
* @param string|null $type The controller type to use | ||
*/ | ||
public function delegate($name, array $params = [], $type = Resolvable::TYPE_CONTROLLER_DELEGATE){ | ||
return $this->engine->callController($name, $type, $params); | ||
} | ||
|
||
/** | ||
* Delegate this action, or part of this action, to the controller with the same name, but | ||
* of a different type (e.g. call a GET or DELETE controller from the POST controller) | ||
* | ||
* @param string $type The controller type to use. | ||
*/ | ||
public function delegateAssociated(array $params = [], $type = Resolvable::TYPE_CONTROLLER_DELEGATE){ | ||
return $this->engine->callController($this->name->getName(), $type, $params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DMJohnson\Contemplate\Tests\Template; | ||
|
||
use DMJohnson\Contemplate\Engine; | ||
use DMJohnson\Contemplate\Template\Controller; | ||
use DMJohnson\Contemplate\Template\Resolvable; | ||
use org\bovigo\vfs\vfsStream; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ControllerTest extends TestCase | ||
{ | ||
private $controller; | ||
|
||
protected function setUp(): void | ||
{ | ||
vfsStream::setup('templates'); | ||
|
||
$engine = new Engine(vfsStream::url('templates')); | ||
$engine->setFileExtension('delegate.php', Resolvable::TYPE_CONTROLLER_DELEGATE); | ||
$engine->setFileExtension('get.php', Resolvable::TYPE_CONTROLLER_GET); | ||
$engine->setFileExtension('post.php', Resolvable::TYPE_CONTROLLER_POST); | ||
|
||
$this->controller = new Controller($engine, 'controller'); | ||
} | ||
|
||
public function testCanCreateInstance() | ||
{ | ||
$this->assertInstanceOf(Controller::class, $this->controller); | ||
} | ||
|
||
public function testCall() | ||
{ | ||
vfsStream::create( | ||
array( | ||
'controller.php' => '<?php return function(){return "Hello World";};', | ||
) | ||
); | ||
|
||
$this->assertSame('Hello World', $this->controller->call()); | ||
} | ||
|
||
public function testCallWithParameters() | ||
{ | ||
vfsStream::create( | ||
array( | ||
'controller.php' => '<?php return function($string){return $string;};', | ||
) | ||
); | ||
|
||
$this->assertSame('Hello World', $this->controller->call(['Hello World'])); | ||
} | ||
|
||
public function testCallWithParametersViaInvoke() | ||
{ | ||
vfsStream::create( | ||
array( | ||
'controller.php' => '<?php return function($string){return $string;};', | ||
) | ||
); | ||
|
||
$this->assertSame('Hello World', ($this->controller)('Hello World')); | ||
} | ||
|
||
public function testCallDoesNotExist() | ||
{ | ||
// The template "controller" could not be found at "vfs://templates/controller.php". | ||
$this->expectException(\LogicException::class); | ||
var_dump($this->controller->call()); | ||
} | ||
|
||
public function testCallException() | ||
{ | ||
// error | ||
$this->expectException('Exception'); | ||
vfsStream::create( | ||
array( | ||
'controller.php' => '<?php return function(){throw new Exception("error");}; ?>', | ||
) | ||
); | ||
var_dump($this->controller->call()); | ||
} | ||
|
||
public function testCallDoesNotLeakVariables() | ||
{ | ||
vfsStream::create( | ||
array( | ||
'controller.php' => '<?php $defined = get_defined_vars(); return function() use ($defined){return $defined;};', | ||
) | ||
); | ||
|
||
$this->assertSame([], $this->controller->call()); | ||
} | ||
|
||
public function testDelegate() | ||
{ | ||
vfsStream::create( | ||
array( | ||
'other.delegate.php' => '<?php return function(){return "Delegate to the delegate";};', | ||
) | ||
); | ||
|
||
$this->assertSame('Delegate to the delegate', $this->controller->delegate('other')); | ||
} | ||
|
||
public function testDelegateAssociated() | ||
{ | ||
vfsStream::create( | ||
array( | ||
'controller.delegate.php' => '<?php return function(){return "Delegate to the delegate";};', | ||
) | ||
); | ||
|
||
$this->assertSame('Delegate to the delegate', $this->controller->delegateAssociated()); | ||
} | ||
|
||
public function testDelegateAssociatedWithType() | ||
{ | ||
vfsStream::create( | ||
array( | ||
'controller.get.php' => '<?php return function(){return "Delegate to the delegate";};', | ||
) | ||
); | ||
|
||
$this->assertSame('Delegate to the delegate', $this->controller->delegateAssociated(type:Resolvable::TYPE_CONTROLLER_GET)); | ||
} | ||
|
||
public function testDelegateWithParams() | ||
{ | ||
vfsStream::create( | ||
array( | ||
'other.delegate.php' => '<?php return function($string){return $string;};', | ||
) | ||
); | ||
|
||
$this->assertSame('Delegate to the delegate', $this->controller->delegate('other', ['Delegate to the delegate'])); | ||
} | ||
|
||
public function testDelegateAssociatedWithParams() | ||
{ | ||
vfsStream::create( | ||
array( | ||
'controller.delegate.php' => '<?php return function($string){return $string;};', | ||
) | ||
); | ||
|
||
$this->assertSame('Delegate to the delegate', $this->controller->delegateAssociated(['Delegate to the delegate'])); | ||
} | ||
|
||
} |