Testing

Yabasi framework provides a robust testing infrastructure to ensure the reliability and stability of your applications. It uses PHPUnit as the testing framework, allowing you to write unit tests, integration tests, and feature tests with ease.

Setting Up the Testing Environment

Yabasi comes with a pre-configured phpunit.xml file that defines test suites and environment variables for testing.

phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../vendor/autoload.php"
         colors="true"
         verbose="true"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./Unit</directory>
        </testsuite>
        <testsuite name="Integration">
            <directory suffix="Test.php">./Integration</directory>
        </testsuite>
    </testsuites>
    <php>
        <env name="APP_ENV" value="testing"/>
    </php>
</phpunit>

Base Test Case

Yabasi provides a base test case class that sets up the testing environment and offers helpful utility methods for your tests.

YabasiTestCase.php
namespace Yabasi\Tests;

use PHPUnit\Framework\TestCase;
use Yabasi\Application;
use Yabasi\Container\Container;

class YabasiTestCase extends TestCase
{
    protected $app;
    protected $container;

    protected function setUp(): void
    {
        parent::setUp();
        $this->container = new Container();
        $this->app = new Application($this->container);
    }

    protected function tearDown(): void
    {
        $this->app = null;
        $this->container = null;
        parent::tearDown();
    }
}

Writing Unit Tests

Unit tests are used to test the smallest parts of your application in isolation. Here's an example of a simple unit test:

ExampleTest.php
namespace Yabasi\Tests\Unit;

use Yabasi\Application;
use Yabasi\Tests\YabasiTestCase;

class ExampleTest extends YabasiTestCase
{
    public function testExample()
    {
        $this->assertTrue(true);
    }

    public function testApplicationInstance()
    {
        $this->assertInstanceOf(Application::class, $this->app);
    }
}

Integration Tests

Integration tests are used to test how different components of your application work together. Here's an example of a router integration test:

RouterTest.php
namespace Yabasi\Tests\Integration;

use Yabasi\Http\Request;
use Yabasi\Routing\Router;
use Yabasi\Tests\YabasiTestCase;

class RouterTest extends YabasiTestCase
{
    public function testBasicRouting()
    {
        $router = $this->app->make(Router::class);
        $router->get('/test', function() {
            return 'Hello Test';
        });

        $request = $this->createRequest('/test', 'GET');
        $response = $router->dispatch($request);

        $this->assertEquals('Hello Test', $response->getContent());
    }

    private function createRequest($uri, $method, $parameters = [], $cookies = [], $files = [], $server = [])
    {
        // Implementation of createRequest method
    }
}

Running Tests

To run your tests, use the PHPUnit command from your project root:

./vendor/bin/phpunit

You can also run specific test suites or individual test files:

./vendor/bin/phpunit --testsuite Unit
./vendor/bin/phpunit tests/Unit/ExampleTest.php

Best Practices

  • Write tests for all new features and bug fixes
  • Aim for high test coverage, but focus on critical paths
  • Keep tests fast and independent of each other
  • Use descriptive test method names
  • Follow the Arrange-Act-Assert (AAA) pattern in your tests
  • Use data providers for testing multiple scenarios
  • Mock external dependencies to isolate the code being tested

By following these testing practices and utilizing Yabasi's testing infrastructure, you can ensure the reliability and maintainability of your applications.

Find the documentation helpful?

Show your support by starring our project on GitHub

Star on GitHub