Coder Social home page Coder Social logo

luyadev / luya-testsuite Goto Github PK

View Code? Open in Web Editor NEW
6.0 7.0 7.0 291 KB

Providing PHPUnit Testcases and a built in Webserver to test your Application, Modules, Components, APIs or Classes.

Home Page: https://luya.io

License: MIT License

PHP 100.00%
phpunit testing testcase luya hacktoberfest yii2

luya-testsuite's Introduction

LUYA Test Suite

Build Status Test Coverage Total Downloads Latest Stable Version Join the chat at https://gitter.im/luyadev/luya

Providing PHPUnit Testcases and a built in Webserver to test your Application, Modules, Components, APIs or Classes.

Whats included?

Test Cases

  • Web application test case
  • Console application test case
  • Server (for APIs) test case
  • CMS Block test case
  • NgRest test case (for model, controller and API)

Traits

  • Message file compare trait
  • Migration file check trait

Fixtures

  • ActiveRecord fixture creates the table on loading based from array or rule definition.

See the full Documentation

Install

Add the luyadev/luya-testsuite package to the require-dev section of your composer.json file:

composer require luyadev/luya-testsuite --dev

Create a new folder tests inside your appliation folder and create a test classes:

namespace app\tests;

use Yii;

class MyTest extends \luya\testsuite\cases\WebApplicationTestCase
{
    public function getConfigArray()
    {
        return [
            'id' => 'mytestapp',
            'basePath' => dirname(__DIR__),
        ];
    }
    
    public function testInstance()
    {
        // add your phpunit tests here, like:
        $this->assertInstanceOf('luya\web\Application', Yii::$app);
        $this->assertInstanceOf('luya\base\Boot', $this->boot);
        $this->assertInstanceOf('luya\web\Application', $this->app);
    }
}

To run the unit tests while (assuming your tests are in directory tests/) run in your terminal:

./vendor/bin/phpunit tests/

In order to support sqlite fixtures install:

sudo apt-get install php-sqlite3 

Example Test Cases

Some example in how to use the LUYA Testsuite for different scenarios.

Testing API and Application

When working with APIs or Customer Websites somtimes you just want to test the Website itself, what is the response, does all the pages still work after my update? Therefore we have luya\testsuite\cases\ServerTestCase.

This example will run your LUYA application within a PHP Webserver and test the response status codes or contents for a set of given pages. In order to run this example create a MyWebsiteTest.php file inside the tests folder.

namespace app\tests;

class MyWebsiteTest extends ServerTestCase
{
   public function getConfigArray()
   {
      return [
          'id' => 'mytestapp',
          'basePath' => dirname(__DIR__),
      ];
  }
  
  public function testSites()
  {
      $this->assertUrlHomepageIsOk(); // checks the root url like: http://localhost/mywebsite.com
      $this->assertUrlIsOk('about'); // checks: http://localhost/mywebsite.com/about
      $this->assertUrlGetResponseContains('about/me', 'Hello World'); // checks: http://localhost/mywebsite.com/about/me
      $this->assertUrlIsError('errorpage'); // checks: http://localhost/mywebsite.com/errorpage
  }
}

As the Webserver process may run from a different permission level you have to ensure the assets/runtime folder has the required permissions.

Controller Function Test

We're using getMockBuilder() as shown in the multiple examples in the PHPUnit to setup the DefaultController and assert the registered module addressbook. To test the the runtime exception (caused by a database error), we use the mock method function:

public function testActionIndex()
{
    $module = Yii::$app->getModule('addressbook');
    
    $this->assertInstanceOf('luya\addressbook\frontend\Module', $module);
    $mock = $this->getMockBuilder(DefaultController::class)
        ->setConstructorArgs(["id" => "default", "module" => $module])
        ->getMock();
        
    $mock->method("actionIndex");
}

luya-testsuite's People

Contributors

arhell avatar boehsermoe avatar coollamer avatar denyadzi avatar dev7ch avatar hbugdoll avatar johnnymcweed avatar martinpetrasch avatar nadar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

luya-testsuite's Issues

Ignore safe attributes

as safe attributes are commonly not real attributes we can skip those columns when creating columns from rules.

safe attributes are used to setter/getter allowance for massive assignment.

New feature: CmsBlockGroupTestCase

What steps will reproduce the problem?

Currently, there is no test case for CMS block groups.

What is the expected result?

There should be a test case for CMS block groups.

Additional infos

With a test case for CMS block groups, tests could be done easily in a non repetitive way.
This would give a higher test coverage just by easily adding tests extending from that class.

PR is coming...

Add cms block test case

new cms block test case like

<?php

namespace cmstests;

class BlockTestCase extends CmsFrontendTestCase
{
    public $blockClass;
    
    /**
     * @var \luya\cms\base\PhpBlock
     */
    public $block;

    public function setUp()
    {
        parent::setUp();
        
        $class = $this->blockClass;
        $this->block = new $class();
    }
    
    public function renderFrontend()
    {
        $this->assertNotEmpty($this->block->blockGroup());
        $this->assertNotEmpty($this->block->name());
        $this->assertNotEmpty($this->block->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();
    }
    
    public function renderAdmin()
    {
        $twig = new \Twig_Environment(new \Twig_Loader_Filesystem());
        $temp = $twig->createTemplate($this->block->renderAdmin());
        return $temp->render(['cfgs' => $this->block->getCfgValues(), 'vars' => $this->block->getVarValues()]);
    }

    public function renderAdminNoSpace()
    {
        $text = trim(preg_replace('/\s+/', ' ', $this->renderAdmin()));
        
        return str_replace(['> ', ' <'], ['>', '<'], $text);
    }
        
    public function renderFrontendNoSpace()
    {
        $text = trim(preg_replace('/\s+/', ' ', $this->renderFrontend()));
        
        return str_replace(['> ', ' <'], ['>', '<'], $text);
    }
}

ServerTestCase not running on Windows

What steps will reproduce the problem?

Install and use test suite (ServerTestCase) on Windows.

What is the expected result?

Tests will run as expected

What do you get instead? (A Screenshot can help us a lot!)

Errors / not running

Additional infos

I think it's comming from createServer() and killServer() and that they should have operating system check and adjust the command. So it should be something similar to the following I think:

(Still missing something, so not the solution and no pr...)

protected function createServer($host, $port, $documentRoot)
{
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        $command = sprintf(PHP_BINARY . ' -S %s:%d -t %s >NUL 2>&1 &  ......(Tasklist?).......', $host, $port, $documentRoot);
    } else {
        $command = sprintf(PHP_BINARY . ' -S %s:%d -t %s >/dev/null 2>&1 & echo $!', $host, $port, $documentRoot);
    }
    $output = [];
    exec($command, $output);
    return (int) $output[0];
}

and

protected function killServer($pid)
{
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        exec('taskkill /pid ' . (int) $pid);
    } else {
        exec('kill -9 ' . (int) $pid);
    }
}
Q A
LUYA Version 1.2.1
LUYA Testsuite 1.0.30
PHP Version 7.4
Platform WAMP
Operating system Windows

Option to disable removing of safe attrributes

  • Options to disable the remove behavior of safe attributes, as they should not be removed by default.
  • Option to remove certain columns from the table creation (for setter/getters).

This breaks backward compatibility! introduced here: 3356974

Db component not defined while luyadev

While execute luyadev the core commands are disabled. But the bootstrap of the test suite still adding generatefixture to controllermap. Otherwise I will get an exception that the db component is not defined.
Or did I configure something wrong?

In line 24 of the Bootstrap should check the enableCoreCommands:

if ($app instanceof Application && $app->enableCoreCommands) {

Generic Test Helpers

  • A generic helper to test getMenu() of an admin module.
  • A generic helper to test queue jobs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.