|
| 1 | +# 🐳 Test-Containers for Quick Magento Development |
| 2 | +[](https://github.com/EcomDev/testcontainer-magento-data/actions/workflows/docker-images.yml) |
| 3 | +[](https://github.com/EcomDev/testcontainer-magento-data-php/actions/workflows/php-package.yml) |
| 4 | + |
| 5 | +This package simplifies the process of automated testing with real database and search engine |
| 6 | + |
| 7 | +## ✨ Features |
| 8 | + |
| 9 | +- 📦 **Pre-configured database and search containers**: Instantly spin up containers with ready-to-use Magento data |
| 10 | +- ⚙️ **Easy setup and use**: Use PHP package to automatically discard container after tests |
| 11 | +- 🎯 **Blazingly Fast**: Container takes only few seconds to start, so you can focus on testing instead of waiting for db initialization |
| 12 | + |
| 13 | +## 📋 Requirements |
| 14 | + |
| 15 | +- **🐳 Docker**: Ensure Docker is installed and operational on your system. |
| 16 | + |
| 17 | +## 📦 Available images |
| 18 | + |
| 19 | +All the available Docker image version can be found in build repository [EcomDev/testcontainer-magento-data](https://github.com/EcomDev/testcontainer-magento-data?tab=readme-ov-file#-available-images) |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +Use composer with `--dev` flag to add it as dependency for your tests |
| 24 | +```bash |
| 25 | +composer require --dev ecomdev/testcontainers-magento-data |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | +## Examples |
| 30 | + |
| 31 | +### MySQL container |
| 32 | + |
| 33 | +Create Latest Magento Database Build |
| 34 | +```php |
| 35 | +use EcomDev\TestContainers\MagentoData\DbContainerBuilder; |
| 36 | + |
| 37 | +$container = DbContainerBuilder::mysql() |
| 38 | + ->build(); |
| 39 | +``` |
| 40 | + |
| 41 | +Create Latest Magento Database Build with sample data |
| 42 | +```php |
| 43 | +use EcomDev\TestContainers\MagentoData\DbContainerBuilder; |
| 44 | + |
| 45 | +$container = DbContainerBuilder::mysql() |
| 46 | + ->withSampleData() |
| 47 | + ->build(); |
| 48 | +``` |
| 49 | + |
| 50 | +Create 2.4.7-p2 with sample data and fetch number of products |
| 51 | +```php |
| 52 | +use EcomDev\TestContainers\MagentoData\DbContainerBuilder; |
| 53 | +use PDO; |
| 54 | + |
| 55 | +$container = DbContainerBuilder::mysql() |
| 56 | + ->withMagentoVersion('2.4.7-p2') |
| 57 | + ->withSampleData() |
| 58 | + ->build(); |
| 59 | + |
| 60 | +$connectionSettings = $container->getConnectionSettings(); |
| 61 | +$connection = new PDO( |
| 62 | + $connectionSettings->dsn(), |
| 63 | + $connectionSettings->user, |
| 64 | + $connectionSettings->password |
| 65 | +); |
| 66 | + |
| 67 | +$result = $connection->query('SELECT COUNT(*) FROM catalog_product_entity'); |
| 68 | +// Outputs 2040 |
| 69 | +echo $result->fetch(PDO::FETCH_COLUMN); |
| 70 | +``` |
| 71 | + |
| 72 | +### MariaDB container |
| 73 | +Everything the same as for MySQL container, just a different builder method |
| 74 | + |
| 75 | +```php |
| 76 | +use EcomDev\TestContainers\MagentoData\DbContainerBuilder; |
| 77 | + |
| 78 | +$container = DbContainerBuilder::mariadb() |
| 79 | + ->withMagentoVersion('2.4.7-p2') |
| 80 | + ->withSampleData() |
| 81 | + ->build(); |
| 82 | +``` |
| 83 | + |
| 84 | +## OpenSearch container |
| 85 | + |
| 86 | +For OpenSearch container there is a different builder and container, that allows building base url for http connection |
| 87 | + |
| 88 | +Here is a small example |
| 89 | + |
| 90 | +```php |
| 91 | +use EcomDev\TestContainers\MagentoData\OpenSearchContainerBuilder; |
| 92 | +use GuzzleHttp\Client; |
| 93 | + |
| 94 | +$container = OpenSearchContainerBuilder::new() |
| 95 | + ->withSampleData() |
| 96 | + ->build(); |
| 97 | + |
| 98 | +$client = new Client([ |
| 99 | + 'base_uri' => $container->getBaseUrl() |
| 100 | +]); |
| 101 | + |
| 102 | +$result = json_decode( |
| 103 | + $client->get('magento2_product_1/_count')->getBody()->getContents(), |
| 104 | + true |
| 105 | +); |
| 106 | + |
| 107 | +// Outputs 181 |
| 108 | +echo $result['count']; |
| 109 | +``` |
| 110 | + |
| 111 | +## 📜 License |
| 112 | + |
| 113 | +This project is licensed under the MIT License. |
| 114 | + |
| 115 | +See the [LICENSE](LICENSE) file for more details. |
0 commit comments