Abstract Class luya\testsuite\cases\CmsBlockTestCase

Inheritanceluya\testsuite\cases\CmsBlockTestCase » luya\testsuite\cases\WebApplicationTestCase » luya\testsuite\cases\BaseTestSuite » Yoast\PHPUnitPolyfills\TestCases\TestCase
Available since version1.0.6
Source Code https://github.com/luyadev/luya-testsuite/blob/master/src/cases/CmsBlockTestCase.php

Cms module block TestCase.

Testing a CMS Block for the CMS Layout. Example usage:

class TextBlockTest extends CmsBlockTestCase
{
    public $blockClass = 'luya\generic\blocks\TextBlock';

    public function getConfigArray()
    {
        return [
            'id' => 'testTextBlock',
            'basePath' => dirname(__DIR__),
        ];
    }

    public function testAdminAndFrontendRender()
    {
        $this->assertSame('<p>Removes spaces and br from frontend view.</p>', $this->renderFrontendNoSpace());
        $this->assertSame('<p>Admin View</p>', $this->renderAdminNoSpace());
    }

    public function testFrontendWithVars()
    {
        $this->block->setVarValues([
             'text' => 'Hello World',
        ]);

        $this->assertSame('<p>Hello World', $this->renderFrontendNoSpace());
    }
}

On order to test an image object from storage you could use

$this->block->setVarValues(['image' => (object) ['source' => 'image.jpg', 'caption' => 'image caption']]);

Public Methods

Hide inherited methods

Method Description Defined By
afterSetup() Method which is executed after the setUp() method in order to trigger post setup functions. luya\testsuite\cases\CmsBlockTestCase
assertContainsNoSpace() Assert Contains without spaces but with newlines. luya\testsuite\cases\BaseTestSuite
assertContainsTrimmed() Same as assertContains but trims the needle and haystack content in order to compare. luya\testsuite\cases\BaseTestSuite
assertSameNoSpace() This assert Same option allows you to compare two strings but removing spaces and tabes, so its more easy to work with readable contents but better comparing. luya\testsuite\cases\BaseTestSuite
assertSameTrimmed() Assert Same but trim content (remove, double spaces, tabs and newlines. luya\testsuite\cases\BaseTestSuite
beforeSetup() Method which is executed before the setUp() method in order to inject data on before Setup. luya\testsuite\cases\WebApplicationTestCase
beforeTearDown() This method is triggered before the application test case tearDown() method is running. luya\testsuite\cases\BaseTestSuite
bootApplication() luya\testsuite\cases\WebApplicationTestCase
fixture() Get Fixture Object luya\testsuite\cases\BaseTestSuite
fixtures() Defines a list of fixtures classes which can be loaded. luya\testsuite\cases\BaseTestSuite
getConfigArray() Provide Configurtion Array. luya\testsuite\cases\BaseTestSuite
invokeMethod() Call a private or protected method from an object and return the value. luya\testsuite\cases\BaseTestSuite
renderAdmin() Renders the admin view of a blockl. luya\testsuite\cases\CmsBlockTestCase
renderAdminNoSpace() Render the admin view, but auto remove all spaces. luya\testsuite\cases\CmsBlockTestCase
renderFrontend() Renders the frontend view of a block. luya\testsuite\cases\CmsBlockTestCase
renderFrontendNoSpace() Render the frontend view, but auto remove all spaces. luya\testsuite\cases\CmsBlockTestCase
setupFixtures() Create all fixtures from fixtures() list. luya\testsuite\cases\BaseTestSuite
tearDownFixtures() Run cleanup() on all loaded fixtures. luya\testsuite\cases\BaseTestSuite

Protected Methods

Hide inherited methods

Method Description Defined By
set_up() luya\testsuite\cases\BaseTestSuite
tear_down() luya\testsuite\cases\BaseTestSuite
trimContent() No Spaces and No Newline Trims the given text. Remove whitespaces, tabs and other chars in order to compare readable formated texts. luya\testsuite\cases\BaseTestSuite
trimSpaces() No Spaces with Newline luya\testsuite\cases\BaseTestSuite

Property Details

Hide inherited properties

$block public property
$blockClass public property

The path to the block to be tested.

public string $blockClass null

Method Details

Hide inherited methods

afterSetup() public method (available since version 1.0.2)

Method which is executed after the setUp() method in order to trigger post setup functions.

Make sure to call the parent afterSetup() method when overwriting this method.

public void afterSetup ( )

                public function afterSetup()
{
    parent::afterSetup();
    
    if (!$this->blockClass) {
        throw new InvalidConfigException("The 'blockClass' property can not be empty.");
    }
    
    $class = $this->blockClass;
    $this->block = new $class();
}

            
assertContainsNoSpace() public method (available since version 1.0.8)

Defined in: luya\testsuite\cases\BaseTestSuite::assertContainsNoSpace()

Assert Contains without spaces but with newlines.

public boolean assertContainsNoSpace ( $needle, $haystack )
$needle string
$haystack string

                public function assertContainsNoSpace($needle, $haystack)
{
    return $this->assertStringContainsString($this->trimSpaces($needle), $this->trimSpaces($haystack));
}

            
assertContainsTrimmed() public method

Defined in: luya\testsuite\cases\BaseTestSuite::assertContainsTrimmed()

Same as assertContains but trims the needle and haystack content in order to compare.

This will also remove newlines.

public boolean assertContainsTrimmed ( $needle, $haystack )
$needle string
$haystack string

                public function assertContainsTrimmed($needle, $haystack)
{
    return self::assertStringContainsString($this->trimContent($needle), $this->trimContent($haystack));
}

            
assertSameNoSpace() public method (available since version 1.0.8)

Defined in: luya\testsuite\cases\BaseTestSuite::assertSameNoSpace()

This assert Same option allows you to compare two strings but removing spaces and tabes, so its more easy to work with readable contents but better comparing.

This wont remove new lines.

public boolean assertSameNoSpace ( $needle, $haystack )
$needle string
$haystack string

                public function assertSameNoSpace($needle, $haystack)
{
    return $this->assertSame($this->trimSpaces($needle), $this->trimSpaces($haystack));
}

            
assertSameTrimmed() public method (available since version 1.0.8)

Defined in: luya\testsuite\cases\BaseTestSuite::assertSameTrimmed()

Assert Same but trim content (remove, double spaces, tabs and newlines.

public boolean assertSameTrimmed ( $needle, $haystack )
$needle string
$haystack string

                public function assertSameTrimmed($needle, $haystack)
{
    return $this->assertSame($this->trimContent($needle), $this->trimContent($haystack));
}

            
beforeSetup() public method

Defined in: luya\testsuite\cases\WebApplicationTestCase::beforeSetup()

Method which is executed before the setUp() method in order to inject data on before Setup.

Make sure to call the parent beforeSetup() method when overwriting this method.

See also luya\testsuite\cases\BaseTestSuite::beforeSetup().

public void beforeSetup ( )

                public function beforeSetup()
{
    parent::beforeSetup();
    
    $_SERVER['SCRIPT_FILENAME'] = 'index.php';
    $_SERVER['SCRIPT_NAME'] =  '/index.php';
    $_SERVER['REQUEST_URI'] = '/';
}

            
beforeTearDown() public method (available since version 1.0.2)

Defined in: luya\testsuite\cases\BaseTestSuite::beforeTearDown()

This method is triggered before the application test case tearDown() method is running.

public void beforeTearDown ( )

                public function beforeTearDown()
{
}

            
bootApplication() public method (available since version 1.0.2)
public void bootApplication ( luya\base\Boot $boot )
$boot luya\base\Boot

                public function bootApplication(Boot $boot)
{
    $boot->applicationWeb();
}

            
fixture() public method

Defined in: luya\testsuite\cases\BaseTestSuite::fixture()

Get Fixture Object

public luya\testsuite\fixtures\ActiveRecordFixture fixture ( $fixtureClass )
$fixtureClass string

                public function fixture($fixtureClass)
{
    if (is_array($this->_fixtures)) {
        return array_key_exists($fixtureClass, $this->_fixtures) ? $this->_fixtures[$fixtureClass] : false;
    }
    return false;
}

            
fixtures() public method (available since version 1.1.0)

Defined in: luya\testsuite\cases\BaseTestSuite::fixtures()

Defines a list of fixtures classes which can be loaded.

Example fixtures list:

public function fixtures()
{
   return [
       'app\fixtures\MyTestFixture',
       MySuperFixture::class,
   ];
}
public array fixtures ( )

                public function fixtures()
{
    return [];
}

            
getConfigArray() public abstract method

Defined in: luya\testsuite\cases\BaseTestSuite::getConfigArray()

Provide Configurtion Array.

public abstract void getConfigArray ( )

                abstract public function getConfigArray();

            
invokeMethod() public method (available since version 1.0.8)

Defined in: luya\testsuite\cases\BaseTestSuite::invokeMethod()

Call a private or protected method from an object and return the value.

public function testProtectedMethod()
{
    // assuming MyObject has a protected method like:
    // protected function hello($title)
    // {
    //     return $title;
    // }
    $object = new MyObject();

    $this->assertSame('Hello World', $this->invokeMethod($object, 'hello', ['Hello World']));
}
public mixed invokeMethod ( &$object, $methodName, array $parameters = [] )
$object object

The object the method exists from.

$methodName string

The name of the method which should be called.

$parameters array

An array of paremters which should be passed to the method.

                public function invokeMethod(&$object, $methodName, array $parameters = [])
{
    $reflection = new \ReflectionClass(get_class($object));
    $method = $reflection->getMethod($methodName);
    $method->setAccessible(true);
    return $method->invokeArgs($object, $parameters);
}

            
renderAdmin() public method

Renders the admin view of a blockl.

public string renderAdmin ( )

                public function renderAdmin()
{
    $twig = new Environment(new FilesystemLoader());
    $temp = $twig->createTemplate($this->block->renderAdmin());
    return $temp->render([
        'cfgs' => $this->block->getCfgValues(),
        'vars' => $this->block->getVarValues(),
        'extras' => $this->block->getExtraVarValues(),
    ]);
}

            
renderAdminNoSpace() public method

Render the admin view, but auto remove all spaces.

public mixed renderAdminNoSpace ( )

                public function renderAdminNoSpace()
{
    return $this->trimContent($this->renderAdmin());
}

            
renderFrontend() public method

Renders the frontend view of a block.

public string renderFrontend ( )

                public function renderFrontend()
{
    $icon = $this->block->icon();
    if (empty($icon)) {
        $icon = 'fa';
    }
    $this->assertNotEmpty($this->block->blockGroup());
    $this->assertNotEmpty($this->block->name());
    $this->assertTrue(is_string($icon));
    $this->assertTrue(is_array($this->block->config()));
    $this->assertTrue(is_array($this->block->extraVars()));
    $this->assertFalse(is_array($this->block->renderAdmin()));
    $this->assertNotNull($this->block->getFieldHelp());
    return $this->block->renderFrontend();
}

            
renderFrontendNoSpace() public method

Render the frontend view, but auto remove all spaces.

public string renderFrontendNoSpace ( )

                public function renderFrontendNoSpace()
{
    return $this->trimContent($this->renderFrontend());
}

            
set_up() protected method

Defined in: luya\testsuite\cases\BaseTestSuite::set_up()

See also \PHPUnit\Framework\TestCase::setUp().

protected void set_up ( )

                protected function set_up() {
    parent::set_up();
    $this->beforeSetup();
    
    $boot = new Boot();
    $boot->setConfigArray($this->getConfigArray());
    $boot->mockOnly = true;
    $boot->setBaseYiiFile('vendor/yiisoft/yii2/Yii.php');
    $this->bootApplication($boot);
    $this->boot = $boot;
    $this->app = $boot->app;
    
    $this->afterSetup();
    $this->setupFixtures();
}

            
setupFixtures() public method (available since version 1.1.0)

Defined in: luya\testsuite\cases\BaseTestSuite::setupFixtures()

Create all fixtures from fixtures() list.

public void setupFixtures ( )

                public function setupFixtures()
{
    if ($this->_fixtures === null) {
        $loadedFixtures = [];
        foreach ($this->fixtures() as $fixtureClass) {
            $loadedFixtures[$fixtureClass] = Yii::createObject($fixtureClass);
        }
        $this->_fixtures = $loadedFixtures;
    }
}

            
tearDownFixtures() public method (available since version 1.1.0)

Defined in: luya\testsuite\cases\BaseTestSuite::tearDownFixtures()

Run cleanup() on all loaded fixtures.

public void tearDownFixtures ( )

                public function tearDownFixtures()
{
    if (is_array($this->_fixtures)) {
        /** @var ActiveRecordFixture $object */
        foreach ($this->_fixtures as $object) {
            $object->cleanup();
        }
    }
}

            
tear_down() protected method

Defined in: luya\testsuite\cases\BaseTestSuite::tear_down()

See also \PHPUnit\Framework\TestCase::tearDown().

protected void tear_down ( )

                protected function tear_down() {
    // Any clean up needed related to `set_up()`.
    parent::tear_down();
    $this->beforeTearDown();
    $this->tearDownFixtures();
    unset($this->app, $this->boot);
}

            
trimContent() protected method

Defined in: luya\testsuite\cases\BaseTestSuite::trimContent()

No Spaces and No Newline Trims the given text. Remove whitespaces, tabs and other chars in order to compare readable formated texts.

protected string trimContent ( $text )
$text string
return string

The trimmed text.

                protected function trimContent($text)
{
    return str_replace(['> ', ' <'], ['>', '<'], trim(preg_replace('/\s+/', ' ', $text)));
}

            
trimSpaces() protected method

Defined in: luya\testsuite\cases\BaseTestSuite::trimSpaces()

No Spaces with Newline

Removes tabs and spaces from a string. But keeps newlines.

protected string trimSpaces ( $text )
$text string

                protected function trimSpaces($text)
{
    $lines = null;
    foreach (preg_split("/((\r?\n)|(\r\n?))/", $text) as $line) {
        if (!empty($line)) {
            $lines .= $this->trimContent($line) . PHP_EOL;
        }
    }
    return $lines;
}