Testing

Automated testing helps ensure that software meets quality and performance goals by making it easy to repeat individual tests or test suites quickly and easily during development. This increases coverage, provides faster feedback to developers, and increases productivity. Automation is also important for running tests at key points in the development process, such as when code is checked in, features are integrated, or versions are released.

Assegai includes features to help developers and teams automate various types of tests, including unit tests, end-to-end (e2e) tests, and integration tests. These tests are essential for ensuring that software releases meet quality and performance goals, increasing coverage and providing a faster feedback loop to developers. Assegai aims to promote development best practices, including effective testing, by making it easier to set up and automate these tests.

Assegai offers a range of features to assist in the testing process, including:

  • Generating default unit tests for components and e2e tests for applications
  • Providing a test runner to load isolated modules and applications
  • Integrating with codeception by default, while also being compatible with other testing tools
  • Making the Assegai dependency injection system available in the testing environment for easy mocking of components"

As mentioned, you can use any testing framework that you like, as Assegai doesn't force any specific tooling. Simply replace the elements needed (such as the test runner), and you will still enjoy the benefits of Assegai's ready-made testing facilities.

Installation

To get started with testing in Assegai, you will need to install the required package. You can do this by running the following command:

$ composer require --dev assegai/testing

Unit testing

Once you have the package installed, you can use the following code to create a test case in PHP:

use Assegai\Testing\TestCase;

class MyTestCase extends TestCase
{
  public function testSomething()
  {
    $this->assertTrue(true);
  }
}

You can then use a testing framework such as PHPUnit to run your test cases.

vendor/bin/phpunit

Assegai provides integration with Codeception out-of-the-box, so you can also use that to run your tests if you prefer.

Behavior driven development (BDD)

BDD (Behavior-Driven Development) is a software development methodology that focuses on defining the expected behavior of a system and implementing that behavior through the use of tests. It is an extension of TDD (Test-Driven Development) and is often used in conjunction with Agile development methodologies.

Assegai promotes the use of BDD as a way to ensure that a software application meets the desired behavior and requirements. It does this by providing a set of tools and practices that enable developers to create and execute tests that verify the desired behavior of the system.

One of the key principles of BDD is the use of human-readable, natural language statements to define the expected behavior of a system. These statements, called "user stories," describe the actions that a user can take and the expected outcomes of those actions. For example, a user story might describe a scenario in which a user logs into a system and is able to view their account information.

To test this behavior, Assegai provides a tool called Codeception, which is a BDD-style testing framework for PHP. Codeception allows developers to write tests in a natural language syntax, making it easier to understand and maintain the tests over time.

Using BDD with Assegai helps ensure that a software application meets the desired behavior and requirements, and enables developers to create a more robust and reliable application. By focusing on the expected behavior of the system and using tools like Codeception to verify that behavior, developers can create a better quality product and improve the overall development process.

Example

To get started, you will need to install the required dependencies. This can be done by running the following command in your terminal:

$ composer require --dev codeception/codeception codeception/module-asserts \
codeception/module-phpmailer codeception/module-webdriver

Once the dependencies are installed, you can generate a functional test suite for your project by running the following command:

$ ./vendor/bin/codecept generate:suite functional

This will create a new functional directory in your project's root directory, containing the necessary configuration files and directories for running functional tests.

To write a functional test for the SpearsService, you will need to create a new test case file in the functional directory. The file should be named SpearsServiceTest.php, and should contain the following code:

src/tests/Functional/SpearsServiceTest.php
namespace Tests\Functional;

use Assegai\Testing\TestCase;
use App\Services\SpearsService;

class SpearsServiceTest extends TestCase
{
    /** @var SpearsService */
    protected $spearsService;

    protected function setUp(): void
    {
        parent::setUp();
        $this->spearsService = $this->container->get(SpearsService::class);
    }

    public function testGetAllSpears()
    {
        $spears = $this->spearsService->getAllSpears();
        $this->assertIsArray($spears);
        $this->assertGreaterThan(0, count($spears));
    }
}

This test case contains a single test method, testGetAllSpears, which verifies that the getAllSpears method of the SpearsService returns an array of spears with at least one element.

To run this test, you can use the following command:

./vendor/bin/codecept run functional

This will execute all functional tests in your project, including the SpearsServiceTest test case. If the test passes, you should see a message indicating that the test passed. If the test fails, you will see a detailed error message indicating what went wrong.

You can also run a specific test case by specifying the name of the test case file, like so:

./vendor/bin/codecept run functional SpearsServiceTest

This can be useful for debugging or for running a specific test when you only want to focus on a particular feature or aspect of your application.