Skip to content

Commit edcf429

Browse files
committed
Add static config method
1 parent 545495e commit edcf429

7 files changed

+96
-42
lines changed

README.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ use Cycle\Schema\Provider\SimpleCacheSchemaProvider;
4949
use Cycle\Schema\Provider\Support\SchemaProviderPipeline;
5050

5151
$pipeline = (new SchemaProviderPipeline($container))->withConfig([
52-
SimpleCacheSchemaProvider::class => ['key' => 'cycle-schema'],
53-
FromFilesSchemaProvider::class => ['files' => ['runtime/schema1.php', 'runtime/schema2.php']],
52+
SimpleCacheSchemaProvider::class => SimpleCacheSchemaProvider::config(key: 'cycle-schema'),
53+
FromFilesSchemaProvider::class => FromFilesSchemaProvider::config(files: [
54+
'runtime/schema1.php',
55+
'runtime/schema2.php',
56+
]),
5457
]);
5558

5659
$schema = new Schema($pipeline->read());
@@ -73,9 +76,12 @@ use Cycle\Schema\Provider\Support\MergeSchemaProvider;
7376
use Cycle\Schema\Provider\Support\SchemaProviderPipeline;
7477

7578
$pipeline = (new SchemaProviderPipeline($container))->withConfig([
76-
SimpleCacheSchemaProvider::class => ['key' => 'cycle-schema'],
79+
SimpleCacheSchemaProvider::class => SimpleCacheSchemaProvider::config(key: 'cycle-schema'),
7780
MergeSchemaProvider::class => [
78-
FromFilesSchemaProvider::class => ['files' => ['runtime/schema1.php', 'runtime/schema2.php']],
81+
FromFilesSchemaProvider::class => FromFilesSchemaProvider::config(files: [
82+
'runtime/schema1.php',
83+
'runtime/schema2.php',
84+
]),
7985
CustomSchemaProvider::class => ['some' => 'config'],
8086
],
8187
]);

src/FromFilesSchemaProvider.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
final class FromFilesSchemaProvider implements SchemaProviderInterface
1717
{
1818
/**
19-
* @var array<string> Schema files
19+
* @var array<non-empty-string> Schema files
2020
*/
2121
private array $files = [];
2222

@@ -36,11 +36,24 @@ final class FromFilesSchemaProvider implements SchemaProviderInterface
3636
*/
3737
public function __construct(?callable $pathResolver = null)
3838
{
39+
/** @psalm-suppress PropertyTypeCoercion */
3940
$this->pathResolver = $pathResolver === null
4041
? static fn (string $path): string => $path
4142
: \Closure::fromCallable($pathResolver);
4243
}
4344

45+
/**
46+
* Create a configuration array for the {@see self::withConfig()} method.
47+
* @param array<non-empty-string> $files
48+
*/
49+
public static function config(array $files, bool $strict = false): array
50+
{
51+
return [
52+
'files' => $files,
53+
'strict' => $strict,
54+
];
55+
}
56+
4457
public function withConfig(array $config): self
4558
{
4659
$files = $config['files'] ?? [];
@@ -57,9 +70,9 @@ public function withConfig(array $config): self
5770
}
5871

5972
$files = \array_map(
60-
function ($file) {
61-
if (!\is_string($file)) {
62-
throw new ConfigurationException('The `files` parameter must contain string values.');
73+
function (mixed $file) {
74+
if (!\is_string($file) || $file === '') {
75+
throw new ConfigurationException('The `files` parameter must contain non-empty string values.');
6376
}
6477
return ($this->pathResolver)($file);
6578
},

src/PhpFileSchemaProvider.php

+3-10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ final class PhpFileSchemaProvider implements SchemaProviderInterface
3131
public function __construct(?callable $pathResolver = null, ?FilesInterface $files = null)
3232
{
3333
$this->files = $files ?? new Files();
34+
/** @psalm-suppress PropertyTypeCoercion */
3435
$this->pathResolver = $pathResolver === null
3536
? static fn (string $path): string => $path
3637
: \Closure::fromCallable($pathResolver);
@@ -39,22 +40,14 @@ public function __construct(?callable $pathResolver = null, ?FilesInterface $fil
3940
/**
4041
* Create a configuration array for the {@see self::withConfig()} method.
4142
*/
42-
public static function config(
43-
string $file,
44-
int $mode = self::MODE_READ_AND_WRITE,
45-
): array {
43+
public static function config(string $file, int $mode = self::MODE_READ_AND_WRITE): array
44+
{
4645
return [
4746
'file' => $file,
4847
'mode' => $mode,
4948
];
5049
}
5150

52-
/**
53-
* @param array{
54-
* file: non-empty-string,
55-
* mode?: self::MODE_READ_AND_WRITE|self::MODE_WRITE_ONLY,
56-
* } $config
57-
*/
5851
public function withConfig(array $config): self
5952
{
6053
$new = clone $this;

src/SimpleCacheSchemaProvider.php

+15
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,28 @@
1010
final class SimpleCacheSchemaProvider implements SchemaProviderInterface
1111
{
1212
public const DEFAULT_KEY = 'Cycle-ORM-Schema';
13+
14+
/**
15+
* @var non-empty-string
16+
*/
1317
private string $key = self::DEFAULT_KEY;
1418

1519
public function __construct(
1620
private CacheInterface $cache
1721
) {
1822
}
1923

24+
/**
25+
* Create a configuration array for the {@see self::withConfig()} method.
26+
* @param non-empty-string $key
27+
*/
28+
public static function config(string $key): array
29+
{
30+
return [
31+
'key' => $key,
32+
];
33+
}
34+
2035
public function withConfig(array $config): self
2136
{
2237
$new = clone $this;

tests/Feature/FromFilesSchemaProviderTest.php

+30-24
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,26 @@
1010
use Cycle\Schema\Provider\Exception\SchemaFileNotFoundException;
1111
use Cycle\Schema\Provider\FromFilesSchemaProvider;
1212

13-
class FromFilesSchemaProviderTest extends BaseSchemaProvider
13+
final class FromFilesSchemaProviderTest extends BaseSchemaProvider
1414
{
1515
protected const READ_CONFIG = ['files' => [__DIR__ . '/Stub/FromFilesSchemaProvider/schema1.php']];
1616
protected const READ_CONFIG_SCHEMA = ['user' => []];
1717

18-
public static function EmptyConfigProvider(): array
18+
public function testConfig(): void
1919
{
20-
return [
21-
[
22-
[],
23-
],
24-
[
25-
['files' => []],
26-
],
27-
];
20+
$this->assertSame(
21+
['files' => ['foo', 'bar'], 'strict' => false],
22+
FromFilesSchemaProvider::config(['foo', 'bar'])
23+
);
24+
25+
$this->assertSame(
26+
['files' => ['foo', 'bar'], 'strict' => true],
27+
FromFilesSchemaProvider::config(['foo', 'bar'], true)
28+
);
2829
}
2930

3031
/**
31-
* @dataProvider EmptyConfigProvider
32+
* @dataProvider emptyConfigProvider
3233
*/
3334
public function testWithConfigEmpty(array $config): void
3435
{
@@ -48,26 +49,15 @@ public function testWithConfigInvalidFiles(): void
4849
$schemaProvider->withConfig(['files' => __DIR__ . '/Stub/FromFilesSchemaProvider/schema1.php']);
4950
}
5051

51-
public static function FileListBadValuesProvider(): array
52-
{
53-
return [
54-
[null],
55-
[42],
56-
[STDIN],
57-
[[]],
58-
[new \SplFileInfo(__FILE__)],
59-
];
60-
}
61-
6252
/**
63-
* @dataProvider FileListBadValuesProvider
53+
* @dataProvider fileListBadValuesProvider
6454
*/
6555
public function testWithConfigInvalidValueInFileList($value): void
6656
{
6757
$schemaProvider = $this->createSchemaProvider();
6858

6959
$this->expectException(ConfigurationException::class);
70-
$this->expectExceptionMessage('The `files` parameter must contain string values.');
60+
$this->expectExceptionMessage('The `files` parameter must contain non-empty string values.');
7161
$schemaProvider->withConfig(['files' => [$value]]);
7262
}
7363

@@ -204,6 +194,22 @@ public function testClear(): void
204194
$this->assertFalse($schemaProvider->clear());
205195
}
206196

197+
public static function emptyConfigProvider(): \Traversable
198+
{
199+
yield [[]];
200+
yield [['files' => []]];
201+
}
202+
203+
public static function fileListBadValuesProvider(): \Traversable
204+
{
205+
yield [null];
206+
yield [42];
207+
yield [STDIN];
208+
yield [[]];
209+
yield [['']];
210+
yield [new \SplFileInfo(__FILE__)];
211+
}
212+
207213
protected function createSchemaProvider(array $config = null): FromFilesSchemaProvider
208214
{
209215
$provider = new FromFilesSchemaProvider();

tests/Feature/PhpFileSchemaProviderTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ protected function tearDown(): void
2727
$this->removeTmpFile();
2828
}
2929

30+
public function testConfig(): void
31+
{
32+
$this->assertSame(
33+
['file' => 'foo', 'mode' => PhpFileSchemaProvider::MODE_READ_AND_WRITE],
34+
PhpFileSchemaProvider::config('foo')
35+
);
36+
37+
$this->assertSame(
38+
['file' => 'foo', 'mode' => PhpFileSchemaProvider::MODE_WRITE_ONLY],
39+
PhpFileSchemaProvider::config('foo', PhpFileSchemaProvider::MODE_WRITE_ONLY)
40+
);
41+
}
42+
3043
public function testReadFromNextProvider(): void
3144
{
3245
$provider1 = $this->createSchemaProvider(self::WRITE_CONFIG);

tests/Feature/SimpleCacheSchemaProviderTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ final class SimpleCacheSchemaProviderTest extends BaseSchemaProvider
1818

1919
private SimpleCacheActionLogger $cacheService;
2020

21+
public function testConfig(): void
22+
{
23+
$this->assertSame(
24+
['key' => 'foo'],
25+
SimpleCacheSchemaProvider::config('foo')
26+
);
27+
}
28+
2129
public function testDefaultState(): void
2230
{
2331
$provider = $this->createSchemaProvider();

0 commit comments

Comments
 (0)