Statistics
27807
42
5
102d
Badges
Dependencies

At this moment I don't have time, energy and money to maintain this project. But it's a shame so if you depend on this project and you want to become a sponsor or develop it further please don't hesitate to contact me. Otherwise, I am not able to guarantee bright future of this repo... :)

Build Status

Tested against PHP 5.6 and 7.0. Please read this wiki.

Heavily inspired by these GitHub projects:

And article(s):

Simple test bench for Nette Framework projects

Write integration tests as simple as possible. This project helps you to write tests very quickly. DRY! The main goal of this project is to make testing very simple for everyone and help with the difficult start.

You can find few examples in this readme or take a look to the tests folder in this project.

Installation

$ composer require mrtnzlml/testbench

Testbench itself doesn't need database. But if you want to use Testbench\TDoctrine or Testbench\TNetteDatabase trait you have to setup database for the first connection. You should use standard database configuration - nothing special. You can see example in tests/tests.local.neon.dist file. Empty database is good enough for Testbench. Supported databases are:

  • Doctrine 2
  • Nette\Database

For another databases please send PR or open issue.

Minimal code

At first you need classic bootstrap file (just example, DIY):

<?php

require __DIR__ . '/../vendor/autoload.php';

Testbench\Bootstrap::setup(__DIR__ . '/_temp', function (\Nette\Configurator $configurator) {
	$configurator->createRobotLoader()->addDirectory([
		__DIR__ . '/../app',
	])->register();

	$configurator->addParameters([
		'appDir' => __DIR__ . '/../app',
	]);

	$configurator->addConfig(__DIR__ . '/../app/config/config.neon');
	$configurator->addConfig(__DIR__ . '/tests.neon');
});

It's important, that we are not creating dependency injection container here. You can use autoload from composer if you don't want to use robot loader. You should also create config file e.g. tests.neon. This file is needed only for database tests at this moment. In this file you should configure your project before tests:

testbench:
	sqls: #what should be loaded after empty database creation
		- %appDir%/../sqls/1.sql
		- %appDir%/../sqls/2.sql

And you are ready to go:

<?php //HomepagePresenterTest.phpt

require __DIR__ . '/../bootstrap.php';

/**
 * @testCase
 */
class HomepagePresenterTest extends \Tester\TestCase
{

	use \Testbench\TPresenter;

	public function testRenderDefault()
	{
		$this->checkAction('Homepage:default');
	}

	public function testRenderDefaultModule()
	{
		$this->checkAction('Module:Homepage:default');
	}

}

(new HomepagePresenterTest())->run();

You can easily write cover with tests UI\Controls, restricted areas, forms, signals, redirects, ...

Please read this article.

Give it a shot!

Look at the tests in this project. You'll see how to use it properly. There are examples in tests folder or in the wiki. Learn how to use these traits:

  • v2.4 v2.4

    • 41ba977 Add support for shared databases
    • e42fefb Use Tester\Environment::THREAD instead of PID
    • 7a546e1 TestbenchExtension: fix options configuration in prepareNetteDatabase
    • 1723771 TPresenter: add support for checkAjaxSignal test method
    • 93b7680 TestbenchExtension: use default config parameters
    • a38c52d TPresenter: use IPresenterFactory::createPresenter to create a presenter instance
    • e2fd316 Add fake $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_NAME']
    • 7c2624f Scaffold: generate boolean values for checkbox fields
    • 7b6b3ff Scaffold: do not add submit buttons into generated test
  • v2.3

    New features and changes:

    • TCompiledContainer::refreshContainer: possibility to refresh container with parameters
    • TComponent: support for anonymous classes (PHP 7)
    • TPresenter: testing forms fields if they are defined as required (experimantal)
    • ControlMock: new fake links format: <a href="link|data!#hash(0=10, a=20, b=30)">
    • added Scaffold
    • fake database is now used every time needed
    • automatically use -C switch if possible
    • added support for Doctrine migrations (enable using testbench.migrations: yes in tests config)
    • fixed compatibility with Nette 2.4
    • dropped support for outdated PHP 5.4 and 5.5
    • minor fixes

    Feel free to improve Testbench and send PR.

  • v2.2

    TPresenter::checkRedirect() - improve error message:

    screenshot from 2016-05-12 13 25 27

    This release also fixes wrong behavior while checking URL path after redirect. It was possible to use / as path and URL /x/y/z passed without error. This wasn't intended. Now it throws error, because it is error. You can fix it by changing path to the /x/y/z or by using regular expressin /.* for example.

  • v2.1.6

    With this update Testbench is going to show you what kind of error (and where) happened during form test:

    screenshot from 2016-04-24 00 29 36

  • v2.1.1

    Now it's possible to test forms with CSRF protection. Also there are new shortcuts for checking RUNLEVEL: markTestAsSlow and markTestAsVerySlow in Testbench\TCompiledContainer.

  • v2.1

    This release introduces Testbench\TNetteDatabase trait with getContext method.

  • v2.0.4

    Now it's possible to use multiple traits at once without error. Also internal trait methods are protected now, therefore you can create custom test case with traits instead of Tester\TestCase.

  • v2.0.1

    901246d TPresenter: Allow login with own identity implementation

  • v2.0

    Introducing new traits:

    And new mocks:

    • ApplicationRequestMock
    • ConnectionMock
    • ControlMock
    • HttpRequestMock
    • PresenterMock

    There is also new TestbnechExtension, RUNLEVEL environment variable for skipping slow tests and run-tests script so you can run tests from website root by vendor/bin/run-tests ... or RUNLEVEL=5 vendor/bin/run-tests for running with env variable.

    Testbench (TDoctrine trait respectively) behaves differently with MySQL and PostgreSQL. Theoretically (well practically) it's quicker with MySQL because it's possible to jump between test databases using USE ... statement.

    For more information please read README file and WIKI

Is this addon outdated? Did you find an issue? Please report it.

Componette Componette admin@componette.com