Abstract Class luya\testsuite\cases\NgRestModelTestCase

Inheritanceluya\testsuite\cases\NgRestModelTestCase » luya\testsuite\cases\WebApplicationTestCase » luya\testsuite\cases\BaseTestSuite » Yoast\PHPUnitPolyfills\TestCases\TestCase
Available since version1.1.0

A Test Case for Models.

Setup sqlite for model access and runs ngrest model methods.

class CityTest extends NgRestModelTestCase
{
  public $modelClass = City::class;

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

  public function testCreate()
  {
     $model = $this->getModel();
     $model->title = 'foobar';
     $this->assertTrue($model->save());
  }
}

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\BaseTestSuite
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\NgRestModelTestCase
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
getFixture() Get the current modelClass Fixture luya\testsuite\cases\NgRestModelTestCase
getModel() Get New model luya\testsuite\cases\NgRestModelTestCase
invokeMethod() Call a private or protected method from an object and return the value. luya\testsuite\cases\BaseTestSuite
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

$modelClass public property

The path to the ngrest model.

public string $modelClass null

Method Details

Hide inherited methods

afterSetup() public method (available since version 1.0.2)

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

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()
{
}

            
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)
{
    // ensure the admin module is registered, if not do so.
    $config = ArrayHelper::merge([
        'components' => [
            'session' => ['class' => 'luya\testsuite\components\DummySession'],
            'cache' => ['class' => 'yii\caching\DummyCache'],
            'db' => ['class' => 'yii\db\Connection', 'dsn' => 'sqlite::memory:'],
        ]
    ], $boot->getConfigArray());
    
    // set the new config.
    $boot->setConfigArray($config);
    
    // boot the application
    $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();

            
getFixture() public method

Get the current modelClass Fixture

public luya\testsuite\fixtures\NgRestModelFixture getFixture ( )

                public function getFixture()
{
    if ($this->fixture($this->modelClass)) {
        return $this->fixture($this->modelClass);
    }
    return new NgRestModelFixture([
        'modelClass' => $this->modelClass,
    ]);
}

            
getModel() public method

Get New model

public luya\testsuite\fixtures\NgRestModelFixture getModel ( )

                public function getModel()
{
    return $this->getFixture()->newModel;
}

            
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);
}

            
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;
}