Skip to content

Implements Symfony's dependency injection in PHPUnit test cases

License

Notifications You must be signed in to change notification settings

NicolasGuilloux/phpunit-dependency-injection-bundle

Repository files navigation

phpunit dependency injection bundle badge badge maintainability contributions welcome brightgreen license MIT blue PHP 7 Symfony 4%2B black Symfony 5%2B black

Short description

Table of content

1. Installation

Execute the composer command to install the bundle:

composer require nicolasguilloux/phpunit-dependency-injection-bundle --dev

Now add the configuration for the test environment and change it to your needs:

phpunit_dependency_injection:
    tests_namespace: 'App\Tests'

Now, it is just a matter of implementing the trait in the corresponding test in the setUp method.

Warning: Note that the class is still a TestCase, so you won’t be able to use the constructor to inject dependencies. This is where the Required annotation/attribute becomes handy. Checkout the Symfony’s documentation.

use NicolasGuilloux\PhpunitDependencyInjectionBundle\TestCase\AutowiringTestTrait;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Contracts\Service\Attribute\Required;

final class RandomTest extends KernelTestCase
{
    use AutowiringTestTrait;

    #[Required]
    public LoggerInterface $logger;

    public function setUp(): void
    {
        parent::setUp();

        self::bootKernel();
        $this->autowire(self::getContainer());
    }

    public function testSomething(): void
    {
        self::assertInstanceOf(LoggerInterface::class, $this->logger);
    }
}

2. Motivations

Checkout the Motivations article.

3. Documentation

4. Versioning

phpunit-dependency-injection-bundle follows semantic versioning. In short the scheme is MAJOR.MINOR.PATCH where 1. MAJOR is bumped when there is a breaking change, 2. MINOR is bumped when a new feature is added in a backward-compatible way, 3. PATCH is bumped when a bug is fixed in a backward-compatible way.

Versions bellow 1.0.0 are considered experimental and breaking changes may occur at any time.

5. Contributing

Contributions are welcomed! There are many ways to contribute, and we appreciate all of them. Here are some of the major ones:

  • Bug Reports: While we strive for quality software, bugs can happen, and we can’t fix issues we’re not aware of. So please report even if you’re not sure about it or just want to ask a question. If anything the issue might indicate that the documentation can still be improved!

  • Feature Request: You have a use case not covered by the current api? Want to suggest a change or add something? We’d be glad to read about it and start a discussion to try to find the best possible solution.

  • Pull Request: Want to contribute code or documentation? We’d love that! If you need help to get started, GitHub as documentation on pull requests. We use the "fork and pull model" were contributors push changes to their personal fork and then create pull requests to the main repository. Please make your pull requests against the master branch.

As a reminder, all contributors are expected to follow our Code of Conduct.

6. License

phpunit-dependency-injection-bundle is distributed under the terms of the MIT license.

See LICENSE for details.

7. Hacking

You might use Docker and docker-compose to hack the project. Check out the following commands.

# Start the project
docker-compose up -d

# Install dependencies
docker-compose exec application composer install

# Run tests
docker-compose exec application bin/phpunit

# Run a bash within the container
docker-compose exec application bash

About

Implements Symfony's dependency injection in PHPUnit test cases

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published