Skip to content

Commit e366d83

Browse files
authored
Merge branch '2.x' into feat/auth-improvements
2 parents 231c7c5 + b1459db commit e366d83

File tree

15 files changed

+140
-23
lines changed

15 files changed

+140
-23
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@
225225
"scripts": {
226226
"phpunit": "@php -d memory_limit=2G vendor/bin/phpunit --display-warnings --display-skipped --display-deprecations --display-errors --display-notices",
227227
"coverage": "vendor/bin/phpunit --coverage-html build/reports/html --coverage-clover build/reports/clover.xml",
228-
"mago:fmt": "vendor/bin/mago fmt && vendor/bin/mago lint --fix --potentially-unsafe --fmt",
229-
"mago:lint": "vendor/bin/mago lint --minimum-level=note",
228+
"fmt": "vendor/bin/mago fmt && vendor/bin/mago lint --fix --potentially-unsafe --fmt",
229+
"lint": "vendor/bin/mago lint --minimum-level=note",
230230
"phpstan": "vendor/bin/phpstan analyse src tests --memory-limit=1G",
231231
"rector": "vendor/bin/rector process --no-ansi",
232232
"merge": "php -d\"error_reporting = E_ALL & ~E_DEPRECATED\" vendor/bin/monorepo-builder merge",
@@ -236,13 +236,13 @@
236236
"./bin/release"
237237
],
238238
"qa": [
239-
"composer mago:fmt",
239+
"composer fmt",
240240
"composer merge",
241241
"./bin/validate-packages",
242242
"./tempest discovery:clear --no-interaction",
243243
"composer rector",
244244
"composer phpunit",
245-
"composer mago:lint",
245+
"composer lint",
246246
"composer phpstan"
247247
]
248248
}

docs/1-essentials/07-testing.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ composer phpunit
2222

2323
## Test-specific discovery locations
2424

25-
Tempest will only discover non-dev namespaces defined in composer.json automatically. That means that `{:hl-keyword:require-dev:}` namespaces aren't discovered automatically. Whenever you need Tempest to discover test-specific locations, you may specify them within the `discoverTestLocations()` method of the provided `IntegrationTest` class.
25+
Tempest will only discover non-dev namespaces defined in composer.json automatically. That means that `{:hl-keyword:require-dev:}` namespaces aren't discovered automatically. Whenever you need Tempest to discover test-specific locations, you may specify them within the `discoverTestLocations()` method of the provided `IntegrationTest` class.
2626

2727
On top of that, Tempest _will_ look for files in the `tests/Fixtures` directory and discover them by default. You can override this behavior by providing your own implementation of `discoverTestLocations()`, where you can return an array of `DiscoveryLocation` objects (or nothing).
2828

@@ -46,7 +46,7 @@ final class HomeControllerTest extends IntegrationTest
4646
If you want to test code that interacts with the database, your test class can call the `setupDatabase()` method. This method will create and migrate a clean database for you on the fly.
4747

4848
```php
49-
class TodoControllerTest extends IntegrationTest
49+
final class TodoControllerTest extends IntegrationTest
5050
{
5151
protected function setUp(): void
5252
{
@@ -60,8 +60,6 @@ class TodoControllerTest extends IntegrationTest
6060
Most likely, you'll want to use a test-specific database connection. You can create a `database.config.php` file anywhere within test-specific discovery locations, and Tempest will use that connection instead of the project's default. For example, you can create a file `tests/Fixtures/database.config.php` like so:
6161

6262
```php tests/Fixtures/database.config.php
63-
<?php
64-
6563
use Tempest\Database\Config\SQLiteConfig;
6664

6765
return new SQLiteConfig(
@@ -72,7 +70,7 @@ return new SQLiteConfig(
7270
By default, no tables will be migrated. You can choose to provide a list of migrations that will be run for every test that calls `setupDatabase()`, or you can run specific migrations on a per-test basis.
7371

7472
```php
75-
class TodoControllerTest extends IntegrationTest
73+
final class TodoControllerTest extends IntegrationTest
7674
{
7775
protected function migrateDatabase(): void
7876
{
@@ -85,7 +83,7 @@ class TodoControllerTest extends IntegrationTest
8583
```
8684

8785
```php
88-
class TodoControllerTest extends IntegrationTest
86+
final class TodoControllerTest extends IntegrationTest
8987
{
9088
public function test_create_todo(): void
9189
{

packages/container/src/GenericContainer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,15 @@ private function autowire(string $className, mixed ...$params): object
419419
}
420420

421421
foreach ($classReflector->getProperties() as $property) {
422-
if ($property->hasAttribute(Inject::class) && ! $property->isInitialized($instance)) {
422+
$inject = $property->getAttribute(Inject::class);
423+
424+
if ($inject && ! $property->isInitialized($instance)) {
423425
if ($property->hasAttribute(Proxy::class)) {
424426
$property->set($instance, $property->getType()->asClass()->getReflection()->newLazyProxy(
425-
fn () => $this->get($property->getType()->getName()),
427+
fn () => $this->get($property->getType()->getName(), $inject->tag),
426428
));
427429
} else {
428-
$property->set($instance, $this->get($property->getType()->getName()));
430+
$property->set($instance, $this->get($property->getType()->getName(), $inject->tag));
429431
}
430432
}
431433
}

packages/container/src/Inject.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99
#[Attribute(Attribute::TARGET_PROPERTY)]
1010
final readonly class Inject
1111
{
12+
public function __construct(
13+
public ?string $tag = null,
14+
) {}
1215
}

packages/container/tests/ContainerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,13 @@ public function test_builtin_dependency_initializer(): void
427427
public function test_inject(): void
428428
{
429429
$container = new GenericContainer();
430+
$container->singleton(InjectB::class, $bTagged = new InjectB(), 'tagged');
430431

431432
/** @var InjectA $a */
432433
$a = $container->get(InjectA::class);
433434

434435
$this->assertInstanceOf(InjectB::class, $a->getB());
436+
$this->assertSame($bTagged, $a->getBTagged());
435437
}
436438

437439
public function test_unregister(): void

packages/container/tests/Fixtures/InjectA.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@
1111
#[Inject]
1212
private InjectB $b; // @phpstan-ignore-line
1313

14+
#[Inject('tagged')]
15+
private InjectB $bTagged; // @phpstan-ignore-line
16+
1417
public function getB(): InjectB
1518
{
1619
return $this->b;
1720
}
21+
22+
public function getBTagged(): InjectB
23+
{
24+
return $this->bTagged;
25+
}
1826
}

packages/database/src/Migrations/MigrationManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private function getTableDefinitions(): array
306306
);
307307
}
308308

309-
private function getMigrationHash(MigratesUp $migration): string
309+
private function getMigrationHash(MigratesUp|MigratesDown $migration): string
310310
{
311311
$sql = '';
312312

packages/database/src/Migrations/RunnableMigrations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class RunnableMigrations implements IteratorAggregate
1919
public function __construct(
2020
private array $migrations = [],
2121
) {
22-
usort($this->migrations, static fn (MigratesUp $a, MigratesUp $b) => $a->name <=> $b->name);
22+
usort($this->migrations, static fn (MigratesUp|MigratesDown $a, MigratesUp|MigratesDown $b) => strnatcmp($a->name, $b->name));
2323
}
2424

2525
public function getIterator(): Traversable
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Database\Tests\Migrations;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use PHPUnit\Framework\TestCase;
10+
use Tempest\Database\MigratesUp;
11+
use Tempest\Database\Migrations\RunnableMigrations;
12+
use Tempest\Database\QueryStatement;
13+
use Tempest\Database\QueryStatements\RawStatement;
14+
15+
final class RunnableMigrationsTest extends TestCase
16+
{
17+
#[DataProvider('provide_migrations')]
18+
#[Test]
19+
public function migration_ordering(array $migrationNames, array $expectedOrder): void
20+
{
21+
$migrations = array_map(fn (string $name) => $this->createDatabaseMigration($name), $migrationNames);
22+
23+
$this->assertSame(
24+
expected: $expectedOrder,
25+
actual: array_map(
26+
callback: static fn (MigratesUp $migration) => $migration->name,
27+
array: iterator_to_array(new RunnableMigrations($migrations)),
28+
),
29+
);
30+
}
31+
32+
public static function provide_migrations(): array
33+
{
34+
return [
35+
[
36+
'migrationNames' => [
37+
'migration1_1',
38+
'migration1_10',
39+
'2025-10-12_create_idbn_table',
40+
'migration1_2',
41+
'2025-01-12_create_author_table',
42+
'2025-08-10_create_user_table',
43+
'2025-08-01_create_book_table',
44+
'2025-08-12_create_chapter_table',
45+
],
46+
'expectedOrder' => [
47+
'2025-01-12_create_author_table',
48+
'2025-08-01_create_book_table',
49+
'2025-08-10_create_user_table',
50+
'2025-08-12_create_chapter_table',
51+
'2025-10-12_create_idbn_table',
52+
'migration1_1',
53+
'migration1_2',
54+
'migration1_10',
55+
],
56+
],
57+
];
58+
}
59+
60+
private function createDatabaseMigration(string $name): MigratesUp
61+
{
62+
return new class($name) implements MigratesUp {
63+
public function __construct(
64+
public string $name,
65+
) {}
66+
67+
public function up(): QueryStatement
68+
{
69+
return new RawStatement('SELECT 1');
70+
}
71+
};
72+
}
73+
}

packages/kv-store/tests/PhpRedisClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function cleanup(): void
3939
{
4040
try {
4141
$this->redis->flush();
42-
} catch (Throwable) {
42+
} catch (Throwable) { // @mago-expect best-practices/no-empty-catch-clause
4343
}
4444
}
4545

0 commit comments

Comments
 (0)