Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

timgavin's avatar
Level 11

Testing packages with a PHP library

I'm creating an open source library (which has nothing to do with Laravel) and have a question about tests. I'd go to Stack Overflow with this, but we know how that would turn out lol

Keep in mind I'm fairly new to testing, so this may be an obvious answer...

I've created Service Providers for this library and need to run tests for them. The services are not designed to work independently of the main library; they actually include traits from the library.

So I'm wondering: should I mock the library for the tests, or install it and build the tests using the library?

0 likes
1 reply
LaryAI's avatar
Level 58

When testing a PHP library, especially one that includes service providers and traits, you have a couple of options. The choice between mocking the library or installing it and building the tests using the library depends on the context and what you aim to achieve with your tests.

Option 1: Mocking the Library

Mocking is useful when you want to isolate the unit of code you are testing. This approach is beneficial for unit tests where you want to test the behavior of a specific component without relying on the actual implementation of the library.

Example:

use PHPUnit\Framework\TestCase;
use YourLibrary\ServiceProvider;
use YourLibrary\SomeService;

class ServiceProviderTest extends TestCase
{
    public function testServiceProvider()
    {
        // Create a mock of the library
        $mockLibrary = $this->createMock(YourLibrary::class);

        // Configure the mock to return specific values or behaviors
        $mockLibrary->method('someMethod')->willReturn('someValue');

        // Inject the mock into the service provider
        $serviceProvider = new ServiceProvider($mockLibrary);

        // Perform assertions
        $this->assertEquals('expectedValue', $serviceProvider->someMethod());
    }
}

Option 2: Installing the Library

Installing the library and building tests using it is more suitable for integration tests. This approach ensures that your service providers and traits work correctly with the actual implementation of the library.

Example:

use PHPUnit\Framework\TestCase;
use YourLibrary\ServiceProvider;
use YourLibrary\MainLibrary;

class ServiceProviderIntegrationTest extends TestCase
{
    public function testServiceProviderWithLibrary()
    {
        // Install and initialize the main library
        $mainLibrary = new MainLibrary();

        // Initialize the service provider with the main library
        $serviceProvider = new ServiceProvider($mainLibrary);

        // Perform assertions
        $this->assertEquals('expectedValue', $serviceProvider->someMethod());
    }
}

Best Practices

  1. Unit Tests: Use mocks to isolate and test individual components.
  2. Integration Tests: Use the actual library to ensure components work together as expected.
  3. Balance: A good test suite often includes both unit and integration tests to cover different aspects of the code.

Conclusion

For your case, if you are fairly new to testing, start with mocking the library for unit tests to get a good grasp of how your service providers behave in isolation. Once comfortable, add integration tests to ensure everything works together as expected.

By combining both approaches, you can achieve a comprehensive test suite that ensures your library is robust and reliable.

Please or to participate in this conversation.