keboola / php-test-utils
PHP test utilities skeleton.
Installs: 43
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/keboola/php-test-utils
Requires
- php: >=8.4
- ext-json: *
- doctrine/orm: ^3.5
- keboola/service-client: ^1.4
- keboola/storage-api-php-client-branch-wrapper: ^6.4
- psr/log: ^3.0
- symfony/framework-bundle: ^7.3
- symfony/uid: ^v7.3
Requires (Dev)
- infection/infection: ^0.31
- keboola/coding-standard: >=16.0
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.4
- symfony/browser-kit: ^7.3
- symfony/dotenv: ^v7.3
This package is auto-updated.
Last update: 2025-10-20 11:03:07 UTC
README
Utilities to make writing PHPUnit tests easier. Currently provides helpers for working with environment variables so that they're properly validated:
Usage
TestEnvVarsTrait
use PHPUnit\Framework\TestCase; use Keboola\PhpTestUtils\TestEnvVarsTrait; final class MyEnvTest extends TestCase { use TestEnvVarsTrait; public function testOptionalEnv(): void { $clientOptions = new ClientOptions( url: new ServiceClient(self::getRequiredEnv('HOSTNAME_SUFFIX'))->getConnectionServiceUrl(), token: self::getRequiredEnv('TEST_STORAGE_API_TOKEN_SNOWFLAKE'), ); }
- getOptionalEnv(name): returns the env var value as a non-empty string, or
nullif not set/empty. - getRequiredEnv(name): returns the env var value as a non-empty string; fails the env if missing.
- overrideEnv(name, value|null): sets or unsets the variable consistently in
$_ENVand the process viaputenv.
Array and Object Property Assertions
The library also provides small helpers for asserting nested array values and private object properties via traits:
- AssertArrayPropertySameTrait: assert that a dot-separated path inside an array equals the expected scalar value.
- AssertObjectPropertyValueTrait: assert that an object's property has the expected value.
use PHPUnit\Framework\TestCase; use Keboola\PhpTestUtils\AssertArrayPropertySameTrait; use Keboola\PhpTestUtils\AssertObjectPropertyValueTrait; final class MyAssertionsTest extends TestCase { use AssertArrayPropertySameTrait; use AssertObjectPropertyValueTrait; public function testHelpers(): void { // Assert nested array property value $row = ['customer' => ['id' => 123, 'name' => 'Acme']]; self::assertArrayPropertySame(123, $row, 'customer.id'); // Assert (even private) object property value $obj = new class() { private string $token = 'abc'; }; self::assertObjectPropertyValue('abc', $obj, 'token'); } }
Development
Clone this repository and init the workspace with following command:
git clone https://github.com/keboola/platform-libraries.git
cd php-test-utils
docker-compose build
docker-compose run --rm dev composer install --no-scripts
Create .env.local file with following contents:
Set the token to master storage token to a Snowflake project.
HOSTNAME_SUFFIX=keboola.com TEST_STORAGE_API_TOKEN_SNOWFLAKE=xxx-xxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Run the test suite using this command:
docker-compose run --rm dev composer tests
License
MIT licensed, see LICENSE file.