{"id":17477468,"url":"https://github.com/testcontainers/testcontainers-php","last_synced_at":"2025-04-08T13:04:20.369Z","repository":{"id":61885647,"uuid":"556046544","full_name":"testcontainers/testcontainers-php","owner":"testcontainers","description":"https://www.testcontainers.org implementation for PHP","archived":false,"fork":false,"pushed_at":"2025-03-06T18:52:49.000Z","size":174,"stargazers_count":126,"open_issues_count":9,"forks_count":21,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-01T12:01:45.247Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/testcontainers.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-22T23:52:33.000Z","updated_at":"2025-03-29T16:08:48.000Z","dependencies_parsed_at":"2023-12-13T16:48:23.822Z","dependency_job_id":"50c5bf28-ce7a-4f36-8939-b7269be855c6","html_url":"https://github.com/testcontainers/testcontainers-php","commit_stats":{"total_commits":30,"total_committers":10,"mean_commits":3.0,"dds":0.7,"last_synced_commit":"dfb4c933a7db8867002a1f480ca5a1f9314548d8"},"previous_names":["testcontainers/testcontainers-php"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testcontainers%2Ftestcontainers-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testcontainers%2Ftestcontainers-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testcontainers%2Ftestcontainers-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testcontainers%2Ftestcontainers-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/testcontainers","download_url":"https://codeload.github.com/testcontainers/testcontainers-php/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247249864,"owners_count":20908263,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-18T20:08:08.327Z","updated_at":"2025-04-08T13:04:20.278Z","avatar_url":"https://github.com/testcontainers.png","language":"PHP","funding_links":[],"categories":["PHP","Testcontainers and PHP"],"sub_categories":[],"readme":"# Testcontainers for PHP\n\nTestcontainers is a PHP package that makes it simple to create and clean up container-based dependencies for automated integration/smoke tests. The package is inspired by the [Testcontainers](https://www.testcontainers.org/) project for Java.\n\n## Installation\n\nAdd this to your project with composer\n\n```bash\ncomposer req --dev testcontainers/testcontainers\n```\n    \n## Usage/Examples\n\n### Starting a general Container\n\n```php\n\u003c?php\n\nuse Testcontainers\\Container\\GenericContainer;\n\n$container = new GenericContainer('nginx:alpine');\n\n// set an environment variable\n$container-\u003ewithEnvironment([\n    'key1' =\u003e 'val1',\n    'key2' =\u003e 'val2'\n]);\n\n// enable health check for an container\n$container-\u003ewithHealthCheckCommand('curl --fail localhost');\n\n// mount current dir to /var/www/html\n$container-\u003ewithMount(__DIR__, '/var/www/html');\n```\n\nNormally you have to wait until the Container is ready. so for this you can define an wait rule:\n\n```php\n\nuse Testcontainers\\Container\\GenericContainer;\nuse Testcontainers\\Wait\\WaitForExec;\nuse Testcontainers\\Wait\\WaitForLog;\nuse Testcontainers\\Wait\\WaitForHttp;\nuse Testcontainers\\Wait\\WaitForHealthCheck;\nuse Testcontainers\\Wait\\WaitForHostPort;\n\n$container = new GenericContainer('nginx:alpine');\n\n// Run mysqladmin ping until the command returns exit code 0\n$container-\u003ewithWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1']));\n\n$container-\u003ewithWait(new WaitForExec(['mysqladmin', 'ping', '-h', '127.0.0.1']), function($exitCode, $contents) {\n    // throw exception if process result is bad\n});\n\n// Wait until that message is in the logs\n$container-\u003ewithWait(new WaitForLog('Ready to accept connections'));\n\n\n// Wait for an http request to succeed\n$container-\u003ewithWait(new WaitForHttp($port, $method = 'GET', $path = '/'));\n\n// Wait for all bound ports to be open\n$container-\u003ewithWait(new WaitForHostPort());\n\n// Wait until the docker heartcheck is green\n$container-\u003ewithWait(new WaitForHealthCheck());\n```\n\n### MySQL\n\n```php\n\u003c?php\n\nuse Testcontainers\\Modules\\MySQLContainer;\n\n$container = (new MySQLContainer('8.0'))\n    -\u003ewithMySQLDatabase('foo')\n    -\u003ewithMySQLUser('bar', 'baz')\n    -\u003estart();\n\n$pdo = new \\PDO(\n    sprintf(\n        'mysql:host=%s;port=%d',\n        $container-\u003egetHost(),\n        $container-\u003egetFirstMappedPort()\n    ),\n    'bar',\n    'baz',\n);\n\n// Do something with pdo\n```\n\n### MariaDB\n\n```php\n\u003c?php\n\nuse Testcontainers\\Modules\\MariaDBContainer;\n\n$container = $container = (new MariaDBContainer())\n    -\u003ewithMariaDBDatabase('foo')\n    -\u003ewithMariaDBUser('bar', 'baz')\n    -\u003estart();\n\n$pdo = new \\PDO(\n    sprintf(\n        'mysql:host=%s;port=%d',\n        $container-\u003egetHost(),\n        $container-\u003egetFirstMappedPort()\n    ),\n    'bar',\n    'baz',\n);\n\n// Do something with pdo\n```\n\n### PostgreSQL\n\n```php\n\u003c?php\n\nuse Testcontainers\\Modules\\PostgresContainer;\n\n$container = (new PostgresContainer())\n    -\u003ewithPostgresUser('bar')\n    -\u003ewithPostgresDatabase('foo')\n    -\u003estart();\n\n$pdo = new \\PDO(\n    sprintf(\n        'pgsql:host=%s;port=%d;dbname=foo',\n        self::$container-\u003egetHost(),\n        self::$container-\u003egetFirstMappedPort()\n    ),\n    'bar',\n    'test',\n);\n\n// Do something with pdo\n```\n\n### Redis\n\n```php\n\nuse Testcontainers\\Modules\\RedisContainer;\n\n$container = (new RedisContainer())\n    -\u003estart();\n\n$redis = new \\Redis();\n$redis-\u003econnect($container-\u003egetHost(), $container-\u003egetFirstMappedPort());\n\n// Do something with redis\n```\n\n### OpenSearch\n\n```php\n\nuse Testcontainers\\Modules\\OpenSearchContainer;\n\n$container = (new OpenSearchContainer())\n    -\u003ewithDisabledSecurityPlugin()\n    -\u003estart();\n\n// Do something with opensearch\n```\n\n### Use with symfony\n\n```yaml\n# config/packages/test/services.yaml\n\nparameters:\n  'doctrine.dbal.connection_factory.class': App\\Tests\\TestConnectionFactory\n```\n\n```php\n\nnamespace App\\Tests;\n\nuse Doctrine\\Bundle\\DoctrineBundle\\ConnectionFactory;\nuse Doctrine\\Common\\EventManager;\nuse Doctrine\\DBAL\\Configuration;\nuse Doctrine\\DBAL\\Tools\\DsnParser;\nuse Testcontainers\\Modules\\PostgresContainer;\n\nclass TestConnectionFactory extends ConnectionFactory\n{\n    static $testDsn;\n\n    public function __construct(array $typesConfig, ?DsnParser $dsnParser = null)\n    {\n        if (!$this::$testDsn) {\n            $psql = (new PostgresContainer())\n                -\u003ewithPostgresUser('user')\n                -\u003ewithPostgresPassword('password')\n                -\u003ewithPostgresDatabase('database')\n                -\u003estart();\n            $this::$testDsn = sprintf('postgresql://user:password@%s:%d/database?serverVersion=14\u0026charset=utf8', $psql-\u003egetAddress(), $psql-\u003egetFirstMappedPort());\n        }\n        parent::__construct($typesConfig, $dsnParser);\n    }\n\n\n    public function createConnection(array $params, ?Configuration $config = null, ?EventManager $eventManager = null, array $mappingTypes = [])\n    {\n        $params['url'] = $this::$testDsn;\n        return parent::createConnection($params, $config, $eventManager, $mappingTypes);\n    }\n\n}\n```\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftestcontainers%2Ftestcontainers-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftestcontainers%2Ftestcontainers-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftestcontainers%2Ftestcontainers-php/lists"}