Skip to content

Commit 31d0d16

Browse files
author
Ryuta Hamasaki
committed
add mysql container for tests
1 parent c86b829 commit 31d0d16

File tree

5 files changed

+47
-28
lines changed

5 files changed

+47
-28
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ PUSHER_APP_CLUSTER=mt1
4949
# Laravel Sail
5050
APP_PORT=
5151
FORWARD_DB_PORT=
52+
FORWARD_TEST_DB_PORT=
5253
FORWARD_REDIS_PORT=

.env.testing

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
APP_KEY=base64:K3LNFxut+foe7SIjNQmmiZQLwcV/NazJHxhB4wlt9hw=
2+
DB_CONNECTION=mysql
3+
DB_HOST=mysql-test
4+
DB_PORT=3306
5+
DB_DATABASE=modular_monolith_laravel
6+
DB_USERNAME=sail
7+
DB_PASSWORD=password

docker-compose.yml

+17
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ services:
4343
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
4444
retries: 3
4545
timeout: 5s
46+
mysql-test:
47+
image: 'mysql/mysql-server:8.0'
48+
ports:
49+
- '${FORWARD_TEST_DB_PORT:-3306}:3306'
50+
environment:
51+
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
52+
MYSQL_ROOT_HOST: "%"
53+
MYSQL_DATABASE: '${DB_DATABASE}'
54+
MYSQL_USER: '${DB_USERNAME}'
55+
MYSQL_PASSWORD: '${DB_PASSWORD}'
56+
MYSQL_ALLOW_EMPTY_PASSWORD: 1
57+
networks:
58+
- sail
59+
healthcheck:
60+
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
61+
retries: 3
62+
timeout: 5s
4663
redis:
4764
image: 'redis:alpine'
4865
ports:
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
<?php
22

3-
namespace Laracon\Inventory\Tests\Feature\Controllers;
4-
3+
use Illuminate\Testing\Fluent\AssertableJson;
54
use Laracon\Inventory\Domain\Models\Product;
6-
use Tests\TestCase;
75

8-
class ProductControllerTest extends TestCase
9-
{
10-
/** @test */
11-
public function index_returns_paginated_response()
12-
{
13-
Product::factory(30)->create();
6+
use function Pest\Laravel\getJson;
7+
8+
uses(Tests\TestCase::class);
149

15-
$response = $this->getJson('/products?page=2')
16-
->assertStatus(200)
17-
->assertJsonCount(10, 'data')
18-
->assertJsonStructure([
19-
'data',
20-
'links',
21-
'meta'
22-
])->decodeResponseJson();
10+
test('index returns paginated response', function () {
11+
Product::factory(30)->create();
2312

24-
$meta = $response['meta'];
25-
$this->assertEquals(10, $meta['per_page']);
26-
$this->assertEquals(2, $meta['current_page']);
27-
$this->assertEquals(3, $meta['last_page']);
28-
$this->assertEquals(11, $meta['from']);
29-
$this->assertEquals(20, $meta['to']);
30-
$this->assertEquals(30, $meta['total']);
31-
}
32-
}
13+
getJson('/inventory-module/products?page=2')
14+
->assertOk()
15+
->assertJson(fn (AssertableJson $json) =>
16+
$json->has('data', 10)
17+
->has('links')
18+
->has('meta')
19+
->where('meta.per_page', 10)
20+
->where('meta.current_page', 2)
21+
->where('meta.last_page', 3)
22+
->where('meta.from', 11)
23+
->where('meta.to', 20)
24+
->where('meta.total', 30)
25+
);
26+
});

tests/TestCase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Tests;
44

5-
use Illuminate\Foundation\Testing\RefreshDatabase;
5+
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
66
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
77

88
abstract class TestCase extends BaseTestCase
99
{
10-
use CreatesApplication, RefreshDatabase;
10+
use CreatesApplication, LazilyRefreshDatabase;
1111
}

0 commit comments

Comments
 (0)