Skip to content

Commit c112ef7

Browse files
authored
Switch tests to SQLite to remove the need for MySQL server (#2616)
1 parent 9956dc5 commit c112ef7

16 files changed

+103
-171
lines changed

.github/workflows/build-ci.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,6 @@ jobs:
2222
php:
2323
- '8.1'
2424
- '8.2'
25-
services:
26-
mysql:
27-
image: mysql:8.0
28-
ports:
29-
- 3307:3306
30-
env:
31-
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
32-
MYSQL_DATABASE: 'unittest'
33-
MYSQL_ROOT_PASSWORD:
3425

3526
steps:
3627
- uses: actions/checkout@v4
@@ -77,8 +68,6 @@ jobs:
7768
./vendor/bin/phpunit --coverage-clover coverage.xml
7869
env:
7970
MONGODB_URI: 'mongodb://127.0.0.1/?replicaSet=rs'
80-
MYSQL_HOST: 0.0.0.0
81-
MYSQL_PORT: 3307
8271
- uses: codecov/codecov-action@v3
8372
with:
8473
token: ${{ secrets.CODECOV_TOKEN }}

CONTRIBUTING.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ Before submitting a pull request:
3939
## Run Tests
4040

4141
The full test suite requires PHP cli with mongodb extension, a running MongoDB server and a running MySQL server.
42-
Tests requiring MySQL will be skipped if it is not running.
4342
Duplicate the `phpunit.xml.dist` file to `phpunit.xml` and edit the environment variables to match your setup.
4443

4544
```bash
46-
$ docker-compose up -d mongodb mysql
45+
$ docker-compose up -d mongodb
4746
$ docker-compose run tests
4847
```
4948

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ RUN apt-get update && \
66
apt-get install -y autoconf pkg-config libssl-dev git unzip libzip-dev zlib1g-dev && \
77
pecl install mongodb && docker-php-ext-enable mongodb && \
88
pecl install xdebug && docker-php-ext-enable xdebug && \
9-
docker-php-ext-install -j$(nproc) pdo_mysql zip
9+
docker-php-ext-install -j$(nproc) zip
1010

1111
COPY --from=composer:2.6.2 /usr/bin/composer /usr/local/bin/composer
1212

docker-compose.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,9 @@ services:
1212
working_dir: /code
1313
environment:
1414
MONGODB_URI: 'mongodb://mongodb/'
15-
MYSQL_HOST: 'mysql'
1615
depends_on:
1716
mongodb:
1817
condition: service_healthy
19-
mysql:
20-
condition: service_started
21-
22-
mysql:
23-
container_name: mysql
24-
image: mysql:8.0
25-
ports:
26-
- "3306:3306"
27-
environment:
28-
MYSQL_ROOT_PASSWORD:
29-
MYSQL_DATABASE: unittest
30-
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
3118

3219
mongodb:
3320
container_name: mongodb

docs/eloquent-models.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ If you want this functionality to work both ways, your SQL-models will need to u
427427

428428
**This functionality only works for `hasOne`, `hasMany` and `belongsTo`.**
429429

430-
The MySQL model should use the `HybridRelations` trait:
430+
The SQL model should use the `HybridRelations` trait:
431431

432432
```php
433433
use MongoDB\Laravel\Eloquent\HybridRelations;

phpunit.xml.dist

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,14 @@
1111
>
1212
<coverage/>
1313
<testsuites>
14-
<testsuite name="all">
14+
<testsuite name="Test Suite">
1515
<directory>tests/</directory>
1616
</testsuite>
17-
<testsuite name="schema">
18-
<file>tests/SchemaTest.php</file>
19-
</testsuite>
20-
<testsuite name="seeder">
21-
<file>tests/SeederTest.php</file>
22-
</testsuite>
23-
<testsuite name="builder">
24-
<file>tests/QueryBuilderTest.php</file>
25-
<file>tests/QueryTest.php</file>
26-
</testsuite>
27-
<testsuite name="transaction">
28-
<file>tests/TransactionTest.php</file>
29-
</testsuite>
30-
<testsuite name="model">
31-
<file>tests/ModelTest.php</file>
32-
<file>tests/RelationsTest.php</file>
33-
</testsuite>
34-
<testsuite name="relations">
35-
<file>tests/RelationsTest.php</file>
36-
<file>tests/EmbeddedRelationsTest.php</file>
37-
</testsuite>
38-
<testsuite name="mysqlrelations">
39-
<file>tests/RelationsTest.php</file>
40-
</testsuite>
41-
<testsuite name="validation">
42-
<file>tests/ValidationTest.php</file>
43-
</testsuite>
4417
</testsuites>
4518
<php>
4619
<env name="MONGODB_URI" value="mongodb://mongodb/"/>
4720
<env name="MONGODB_DATABASE" value="unittest"/>
48-
<env name="MYSQL_HOST" value="mysql"/>
49-
<env name="MYSQL_PORT" value="3306"/>
50-
<env name="MYSQL_DATABASE" value="unittest"/>
51-
<env name="MYSQL_USERNAME" value="root"/>
21+
<env name="SQLITE_DATABASE" value=":memory:"/>
5222
<env name="QUEUE_CONNECTION" value="database"/>
5323
</php>
5424
<source>

src/Query/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public function toMql(): array
276276
$group['_id'][$column] = '$' . $column;
277277

278278
// When grouping, also add the $last operator to each grouped field,
279-
// this mimics MySQL's behaviour a bit.
279+
// this mimics SQL's behaviour a bit.
280280
$group[$column] = ['$last' => '$' . $column];
281281
}
282282

tests/HybridRelationsTest.php

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace MongoDB\Laravel\Tests;
66

7-
use Illuminate\Database\MySqlConnection;
7+
use Illuminate\Database\SQLiteConnection;
88
use Illuminate\Support\Facades\DB;
99
use MongoDB\Laravel\Tests\Models\Book;
10-
use MongoDB\Laravel\Tests\Models\MysqlBook;
11-
use MongoDB\Laravel\Tests\Models\MysqlRole;
12-
use MongoDB\Laravel\Tests\Models\MysqlUser;
1310
use MongoDB\Laravel\Tests\Models\Role;
11+
use MongoDB\Laravel\Tests\Models\SqlBook;
12+
use MongoDB\Laravel\Tests\Models\SqlRole;
13+
use MongoDB\Laravel\Tests\Models\SqlUser;
1414
use MongoDB\Laravel\Tests\Models\User;
1515
use PDOException;
1616

@@ -21,90 +21,90 @@ public function setUp(): void
2121
parent::setUp();
2222

2323
try {
24-
DB::connection('mysql')->select('SELECT 1');
24+
DB::connection('sqlite')->select('SELECT 1');
2525
} catch (PDOException) {
26-
$this->markTestSkipped('MySQL connection is not available.');
26+
$this->markTestSkipped('SQLite connection is not available.');
2727
}
2828

29-
MysqlUser::executeSchema();
30-
MysqlBook::executeSchema();
31-
MysqlRole::executeSchema();
29+
SqlUser::executeSchema();
30+
SqlBook::executeSchema();
31+
SqlRole::executeSchema();
3232
}
3333

3434
public function tearDown(): void
3535
{
36-
MysqlUser::truncate();
37-
MysqlBook::truncate();
38-
MysqlRole::truncate();
36+
SqlUser::truncate();
37+
SqlBook::truncate();
38+
SqlRole::truncate();
3939
}
4040

41-
public function testMysqlRelations()
41+
public function testSqlRelations()
4242
{
43-
$user = new MysqlUser();
44-
$this->assertInstanceOf(MysqlUser::class, $user);
45-
$this->assertInstanceOf(MySqlConnection::class, $user->getConnection());
43+
$user = new SqlUser();
44+
$this->assertInstanceOf(SqlUser::class, $user);
45+
$this->assertInstanceOf(SQLiteConnection::class, $user->getConnection());
4646

47-
// Mysql User
47+
// SQL User
4848
$user->name = 'John Doe';
4949
$user->save();
5050
$this->assertIsInt($user->id);
5151

5252
// SQL has many
5353
$book = new Book(['title' => 'Game of Thrones']);
5454
$user->books()->save($book);
55-
$user = MysqlUser::find($user->id); // refetch
55+
$user = SqlUser::find($user->id); // refetch
5656
$this->assertCount(1, $user->books);
5757

5858
// MongoDB belongs to
5959
$book = $user->books()->first(); // refetch
60-
$this->assertEquals('John Doe', $book->mysqlAuthor->name);
60+
$this->assertEquals('John Doe', $book->sqlAuthor->name);
6161

6262
// SQL has one
6363
$role = new Role(['type' => 'admin']);
6464
$user->role()->save($role);
65-
$user = MysqlUser::find($user->id); // refetch
65+
$user = SqlUser::find($user->id); // refetch
6666
$this->assertEquals('admin', $user->role->type);
6767

6868
// MongoDB belongs to
6969
$role = $user->role()->first(); // refetch
70-
$this->assertEquals('John Doe', $role->mysqlUser->name);
70+
$this->assertEquals('John Doe', $role->sqlUser->name);
7171

7272
// MongoDB User
7373
$user = new User();
7474
$user->name = 'John Doe';
7575
$user->save();
7676

7777
// MongoDB has many
78-
$book = new MysqlBook(['title' => 'Game of Thrones']);
79-
$user->mysqlBooks()->save($book);
78+
$book = new SqlBook(['title' => 'Game of Thrones']);
79+
$user->sqlBooks()->save($book);
8080
$user = User::find($user->_id); // refetch
81-
$this->assertCount(1, $user->mysqlBooks);
81+
$this->assertCount(1, $user->sqlBooks);
8282

8383
// SQL belongs to
84-
$book = $user->mysqlBooks()->first(); // refetch
84+
$book = $user->sqlBooks()->first(); // refetch
8585
$this->assertEquals('John Doe', $book->author->name);
8686

8787
// MongoDB has one
88-
$role = new MysqlRole(['type' => 'admin']);
89-
$user->mysqlRole()->save($role);
88+
$role = new SqlRole(['type' => 'admin']);
89+
$user->sqlRole()->save($role);
9090
$user = User::find($user->_id); // refetch
91-
$this->assertEquals('admin', $user->mysqlRole->type);
91+
$this->assertEquals('admin', $user->sqlRole->type);
9292

9393
// SQL belongs to
94-
$role = $user->mysqlRole()->first(); // refetch
94+
$role = $user->sqlRole()->first(); // refetch
9595
$this->assertEquals('John Doe', $role->user->name);
9696
}
9797

9898
public function testHybridWhereHas()
9999
{
100-
$user = new MysqlUser();
101-
$otherUser = new MysqlUser();
102-
$this->assertInstanceOf(MysqlUser::class, $user);
103-
$this->assertInstanceOf(MySqlConnection::class, $user->getConnection());
104-
$this->assertInstanceOf(MysqlUser::class, $otherUser);
105-
$this->assertInstanceOf(MySqlConnection::class, $otherUser->getConnection());
106-
107-
//MySql User
100+
$user = new SqlUser();
101+
$otherUser = new SqlUser();
102+
$this->assertInstanceOf(SqlUser::class, $user);
103+
$this->assertInstanceOf(SQLiteConnection::class, $user->getConnection());
104+
$this->assertInstanceOf(SqlUser::class, $otherUser);
105+
$this->assertInstanceOf(SQLiteConnection::class, $otherUser->getConnection());
106+
107+
// SQL User
108108
$user->name = 'John Doe';
109109
$user->id = 2;
110110
$user->save();
@@ -130,19 +130,19 @@ public function testHybridWhereHas()
130130
new Book(['title' => 'Harry Planter']),
131131
]);
132132

133-
$users = MysqlUser::whereHas('books', function ($query) {
133+
$users = SqlUser::whereHas('books', function ($query) {
134134
return $query->where('title', 'LIKE', 'Har%');
135135
})->get();
136136

137137
$this->assertEquals(2, $users->count());
138138

139-
$users = MysqlUser::whereHas('books', function ($query) {
139+
$users = SqlUser::whereHas('books', function ($query) {
140140
return $query->where('title', 'LIKE', 'Harry%');
141141
}, '>=', 2)->get();
142142

143143
$this->assertEquals(1, $users->count());
144144

145-
$books = Book::whereHas('mysqlAuthor', function ($query) {
145+
$books = Book::whereHas('sqlAuthor', function ($query) {
146146
return $query->where('name', 'LIKE', 'Other%');
147147
})->get();
148148

@@ -151,14 +151,14 @@ public function testHybridWhereHas()
151151

152152
public function testHybridWith()
153153
{
154-
$user = new MysqlUser();
155-
$otherUser = new MysqlUser();
156-
$this->assertInstanceOf(MysqlUser::class, $user);
157-
$this->assertInstanceOf(MySqlConnection::class, $user->getConnection());
158-
$this->assertInstanceOf(MysqlUser::class, $otherUser);
159-
$this->assertInstanceOf(MySqlConnection::class, $otherUser->getConnection());
160-
161-
//MySql User
154+
$user = new SqlUser();
155+
$otherUser = new SqlUser();
156+
$this->assertInstanceOf(SqlUser::class, $user);
157+
$this->assertInstanceOf(SQLiteConnection::class, $user->getConnection());
158+
$this->assertInstanceOf(SqlUser::class, $otherUser);
159+
$this->assertInstanceOf(SQLiteConnection::class, $otherUser->getConnection());
160+
161+
// SQL User
162162
$user->name = 'John Doe';
163163
$user->id = 2;
164164
$user->save();
@@ -171,18 +171,18 @@ public function testHybridWith()
171171
$this->assertIsInt($otherUser->id);
172172
// Clear to start
173173
Book::truncate();
174-
MysqlBook::truncate();
174+
SqlBook::truncate();
175175
// Create books
176-
// Mysql relation
177-
$user->mysqlBooks()->saveMany([
178-
new MysqlBook(['title' => 'Game of Thrones']),
179-
new MysqlBook(['title' => 'Harry Potter']),
176+
// SQL relation
177+
$user->sqlBooks()->saveMany([
178+
new SqlBook(['title' => 'Game of Thrones']),
179+
new SqlBook(['title' => 'Harry Potter']),
180180
]);
181181

182-
$otherUser->mysqlBooks()->saveMany([
183-
new MysqlBook(['title' => 'Harry Plants']),
184-
new MysqlBook(['title' => 'Harveys']),
185-
new MysqlBook(['title' => 'Harry Planter']),
182+
$otherUser->sqlBooks()->saveMany([
183+
new SqlBook(['title' => 'Harry Plants']),
184+
new SqlBook(['title' => 'Harveys']),
185+
new SqlBook(['title' => 'Harry Planter']),
186186
]);
187187
// SQL has many Hybrid
188188
$user->books()->saveMany([
@@ -196,12 +196,12 @@ public function testHybridWith()
196196
new Book(['title' => 'Harry Planter']),
197197
]);
198198

199-
MysqlUser::with('books')->get()
199+
SqlUser::with('books')->get()
200200
->each(function ($user) {
201201
$this->assertEquals($user->id, $user->books->count());
202202
});
203203

204-
MysqlUser::whereHas('mysqlBooks', function ($query) {
204+
SqlUser::whereHas('sqlBooks', function ($query) {
205205
return $query->where('title', 'LIKE', 'Harry%');
206206
})
207207
->with('books')

tests/Models/Book.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function author(): BelongsTo
2424
return $this->belongsTo(User::class, 'author_id');
2525
}
2626

27-
public function mysqlAuthor(): BelongsTo
27+
public function sqlAuthor(): BelongsTo
2828
{
29-
return $this->belongsTo(MysqlUser::class, 'author_id');
29+
return $this->belongsTo(SqlUser::class, 'author_id');
3030
}
3131
}

tests/Models/Role.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public function user(): BelongsTo
1818
return $this->belongsTo(User::class);
1919
}
2020

21-
public function mysqlUser(): BelongsTo
21+
public function sqlUser(): BelongsTo
2222
{
23-
return $this->belongsTo(MysqlUser::class);
23+
return $this->belongsTo(SqlUser::class);
2424
}
2525
}

0 commit comments

Comments
 (0)