mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
Create unit tests for WidgetMaker trait
This commit is contained in:
parent
f4487076b7
commit
dc6098c089
@ -24,7 +24,9 @@ trait WidgetMaker
|
||||
*/
|
||||
public function makeWidget($class, $configuration = [])
|
||||
{
|
||||
$controller = ($this->controller) ?: $this;
|
||||
$controller = property_exists($this, 'controller') && $this->controller
|
||||
? $this->controller
|
||||
: $this;
|
||||
|
||||
if (!class_exists($class)) {
|
||||
throw new SystemException(Lang::get('backend::lang.widget.not_registered', [
|
||||
|
@ -5,23 +5,6 @@ use Backend\Classes\WidgetManager;
|
||||
|
||||
class WidgetManagerTest extends TestCase
|
||||
{
|
||||
public function testMakeWidget()
|
||||
{
|
||||
$manager = WidgetManager::instance();
|
||||
$widget = $manager->makeWidget('Backend\Widgets\Search');
|
||||
$this->assertTrue($widget instanceof \Backend\Widgets\Search);
|
||||
|
||||
$controller = new Controller;
|
||||
$widget = $manager->makeWidget('Backend\Widgets\Search', $controller);
|
||||
$this->assertInstanceOf('Backend\Widgets\Search', $widget);
|
||||
$this->assertInstanceOf('Backend\Classes\Controller', $widget->getController());
|
||||
|
||||
$config = ['test' => 'config'];
|
||||
$widget = $manager->makeWidget('Backend\Widgets\Search', null, $config);
|
||||
$this->assertInstanceOf('Backend\Widgets\Search', $widget);
|
||||
$this->assertEquals('config', $widget->getConfig('test'));
|
||||
}
|
||||
|
||||
public function testListFormWidgets()
|
||||
{
|
||||
$manager = WidgetManager::instance();
|
||||
|
60
tests/unit/backend/traits/WidgetMakerTest.php
Normal file
60
tests/unit/backend/traits/WidgetMakerTest.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
use Backend\Classes\Controller;
|
||||
|
||||
class ExampleTraitClass
|
||||
{
|
||||
use \Backend\Traits\WidgetMaker;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->controller = new Controller;
|
||||
}
|
||||
}
|
||||
|
||||
class WidgetMakerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* The object under test.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
private $traitObject;
|
||||
|
||||
/**
|
||||
* Sets up the fixture.
|
||||
*
|
||||
* This method is called before a test is executed.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$traitName = 'Backend\Traits\WidgetMaker';
|
||||
$this->traitObject = $this->getObjectForTrait($traitName);
|
||||
}
|
||||
|
||||
public function testTraitObject()
|
||||
{
|
||||
$maker = $this->traitObject;
|
||||
|
||||
$widget = $maker->makeWidget('Backend\Widgets\Search');
|
||||
$this->assertTrue($widget instanceof \Backend\Widgets\Search);
|
||||
}
|
||||
|
||||
public function testMakeWidget()
|
||||
{
|
||||
$manager = new ExampleTraitClass;
|
||||
|
||||
$controller = new Controller;
|
||||
$widget = $manager->makeWidget('Backend\Widgets\Search');
|
||||
$this->assertInstanceOf('Backend\Widgets\Search', $widget);
|
||||
$this->assertInstanceOf('Backend\Classes\Controller', $widget->getController());
|
||||
|
||||
$config = ['test' => 'config'];
|
||||
$widget = $manager->makeWidget('Backend\Widgets\Search', $config);
|
||||
$this->assertInstanceOf('Backend\Widgets\Search', $widget);
|
||||
$this->assertEquals('config', $widget->getConfig('test'));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user