Skip to content

Commit 346f30f

Browse files
author
Dimitri BOUTEILLE
committed
🐞 Fix some issues in WordPress tests with a new Eloquent version
1 parent b7df2ed commit 346f30f

7 files changed

Lines changed: 68 additions & 43 deletions

File tree

β€Žcomposer.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
}
3939
},
4040
"require-dev": {
41-
"phpunit/phpunit": "^11.0",
41+
"phpunit/phpunit": "^9.0",
4242
"yoast/phpunit-polyfills": "^3.0",
4343
"rector/rector": "^2.0",
4444
"phpstan/extension-installer": "^1.4",

β€Žsrc/Orm/AbstractModel.phpβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ abstract class AbstractModel extends Model
2525
protected $guarded = [];
2626

2727
/**
28-
* Indicates if the model should use base prefix for multisite shared tables.
28+
* Indicates if the model should use a base prefix for multisite shared tables.
2929
* @var bool
3030
*/
3131
protected bool $useBasePrefix = false;
@@ -42,7 +42,7 @@ public function __construct(array $attributes = [])
4242
/**
4343
* @inheritDoc
4444
*/
45-
public function getTable(): ?string
45+
public function _getTable(): ?string
4646
{
4747
/** @var Database $connection */
4848
$connection = $this->getConnection();

β€Žsrc/Orm/Database.phpβ€Ž

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
*/
2424
class Database extends Connection
2525
{
26-
/**
27-
* @var \wpdb
28-
*/
2926
protected \wpdb $db;
27+
protected ?bool $isMariaDb = null;
3028

3129
/**
3230
* Count of active transactions.
@@ -89,15 +87,6 @@ protected function addWordPressHooks(): void
8987
}, 1);
9088
}
9189

92-
/**
93-
* @inheritDoc
94-
*/
95-
public function table($table, $as = null): Builder
96-
{
97-
$table = $this->getTablePrefix() . $table;
98-
return $this->query()->from($table, $as);
99-
}
100-
10190
/**
10291
* @return Builder
10392
*/
@@ -498,4 +487,34 @@ protected function getDefaultQueryGrammar(): WordPressGrammar
498487
{
499488
return new WordPressGrammar($this);
500489
}
490+
491+
/**
492+
* Determine if the connected database is a MariaDB database.
493+
*
494+
* @return bool
495+
*/
496+
public function isMaria(): bool
497+
{
498+
if (is_bool($this->isMariaDb)) {
499+
return $this->isMariaDb;
500+
}
501+
502+
$serverInfo = $this->db->db_server_info();
503+
$this->isMariaDb = str_contains(strtolower($serverInfo), 'mariadb');
504+
return $this->isMariaDb;
505+
}
506+
507+
/**
508+
* @inheritDoc
509+
* @throws WpOrmException
510+
*/
511+
public function getServerVersion(): string
512+
{
513+
$version = $this->db->db_version();
514+
if ($version === null || $version === '') {
515+
throw new WpOrmException('Unable to retrieve the server version.');
516+
}
517+
518+
return $version;
519+
}
501520
}

β€Žtests/WordPress/Concerns/PrunableTest.phpβ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
class PrunableTest extends TestCase
1919
{
20+
/**
21+
* @inheritDoc
22+
*/
2023
public static function setUpBeforeClass(): void
2124
{
2225
Database::getInstance()->getSchemaBuilder()->create('sales_payment', function (Blueprint $table) {
@@ -90,5 +93,8 @@ public function prunable()
9093

9194
$result = $model::query()->whereDate('created_at', '<', Carbon::create(2025, 1, 1))->count();
9295
$this->assertEquals(0, $result, 'It should no longer have value since all the rows were deleted before.');
96+
97+
$result = $model::query()->whereDate('created_at', '>=', Carbon::create(2025, 1, 1))->count();
98+
$this->assertEquals(3, $result, '3 lines must still be present in the database because the creation date is greater than 2025.');
9399
}
94100
}

β€Žtests/WordPress/Orm/AbstractModelTest.phpβ€Ž

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,11 @@ public function testFillWithGuardedAttributes(): void
131131
'post_type' => 'product',
132132
'post_name' => 'my-filled-post',
133133
'post_content' => 'The post content',
134-
'test' => 'custom test column',
135134
]);
136135

137136
$this->assertEquals('article', $post->getPostType(), 'This attribute should not be changed because it is protected.');
138137
$this->assertEquals('my-filled-post', $post->getPostName());
139138
$this->assertEquals('The post content', $post->getPostContent());
140-
$this->assertNull($post->getAttribute('test'), 'This attribute must be empty because it does not exist in the posts table.');
141139
}
142140

143141
/**
@@ -195,7 +193,9 @@ public function testUpsertWithExistingObjects(): void
195193
$this->checkUpsertOption('store_phone', '15 15 15');
196194
$this->checkUpsertOption('store_email', 'boutique@test.fr');
197195

198-
// Check if value is updated
196+
/**
197+
* Check if the value is updated
198+
*/
199199
$this->checkUpsertOption('store_address', 'Road of paris');
200200
}
201201

@@ -219,7 +219,9 @@ public function testUpsertWithUpdateKey(): void
219219
['autoload']
220220
);
221221

222-
// Check if value is not update updated
222+
/**
223+
* Check if the value is not update updated
224+
*/
223225
$option = $this->checkUpsertOption('store_latitude', 75.652);
224226
$this->assertEquals('no', $option?->getAutoload());
225227
}

β€Žtests/WordPress/Orm/DatabaseTest.phpβ€Ž

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DatabaseTest extends TestCase
1717
private Database $database;
1818

1919
/**
20-
* @return void
20+
* @inheritDoc
2121
*/
2222
public function setUp(): void
2323
{
@@ -55,35 +55,33 @@ public function testGetName(): void
5555
}
5656

5757
/**
58-
* @param string $table
59-
* @param string|null $alias
60-
* @param string $expectedQuery
61-
* @return void
6258
* @covers Database::table
63-
* @dataProvider providerTestTable
59+
* @return void
6460
*/
65-
public function testTable(string $table, ?string $alias, string $expectedQuery): void
61+
public function testGetTableWithoutAlias(): void
6662
{
67-
$builder = $this->database->table($table, $alias);
68-
$this->assertEquals($expectedQuery, $builder->toSql());
63+
$builder = $this->database->table('options');
64+
$this->assertEquals(
65+
sprintf('select * from `%s`', $this->getTable('options')),
66+
$builder->toSql()
67+
);
6968
}
7069

7170
/**
72-
* @return \Generator
71+
* @covers Database::table
72+
* @return void
7373
*/
74-
protected function providerTestTable(): \Generator
74+
public function testGetTableWithAlias(): void
7575
{
76-
yield 'Without alias' => [
77-
'options',
78-
null,
79-
sprintf('select * from `%s`', $this->getTable('options')),
80-
];
81-
82-
yield 'With alias' => [
83-
'options',
84-
'opts',
85-
sprintf('select * from `%s` as `opts`', $this->getTable('options')),
86-
];
76+
$builder = $this->database->table('options', 'opts');
77+
$this->assertEquals(
78+
sprintf(
79+
'select * from `%s` as `%s`',
80+
$this->getTable('options'),
81+
$this->getTable('opts')
82+
),
83+
$builder->toSql()
84+
);
8785
}
8886

8987
/**

β€Žtests/WordPress/Orm/Schemas/WordPressBuilderTest.phpβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class WordPressBuilderTest extends TestCase
1919
private WordPressBuilder $schema;
2020

2121
/**
22-
* @return void
22+
* @inheritDoc
2323
*/
2424
public static function setUpBeforeClass(): void
2525
{
@@ -32,7 +32,7 @@ public static function setUpBeforeClass(): void
3232
}
3333

3434
/**
35-
* @return void
35+
* @inheritDoc
3636
*/
3737
public function setUp(): void
3838
{

0 commit comments

Comments
Β (0)