luyadev / luya-testsuite
TestCases and Data for LUYA Modules and Components. Makes testing less pain.
Installs: 70 424
Dependents: 76
Suggesters: 0
Security: 0
Stars: 6
Watchers: 7
Forks: 7
Type:luya-core
Requires
- curl/curl: *
- twig/twig: ^3.0
- yoast/phpunit-polyfills: ^0.2.0
Requires (Dev)
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- dev-master / 3.0.x-dev
- 3.0.2
- 3.0.1
- 3.0.0
- 2.1.0
- 2.0.x-dev
- 2.0.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.x-dev
- 1.0.31
- 1.0.30
- 1.0.29
- 1.0.28
- 1.0.27
- 1.0.26
- 1.0.25.2
- 1.0.25.1
- 1.0.25
- 1.0.24
- 1.0.23
- 1.0.22
- 1.0.21
- 1.0.20.2
- 1.0.20.1
- 1.0.20
- 1.0.19
- 1.0.18
- 1.0.17.2
- 1.0.17.1
- 1.0.17
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13.3
- 1.0.13.2
- 1.0.13.1
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-10-30 01:26:35 UTC
README
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"); }