Skip to content

Commit

Permalink
MetaLoader: preload excludes, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed May 22, 2023
1 parent 0f687b9 commit d8b2715
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
11 changes: 11 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,17 @@ $metaLoader->preloadFromPaths([
]);
```

You may as well exclude paths

```php
$metaLoader->preloadFromPaths([
__DIR__ . '/path1',
__DIR__ . '/path2',
], [
__DIR__ . '/path1/excluded',
]);
```

## Tracking input values

Track input values of every mapped object in hierarchy, before they were processed by rules.
Expand Down
15 changes: 13 additions & 2 deletions src/Meta/MetaLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use function array_values;
use function assert;
use function class_exists;
use function interface_exists;
use function is_subclass_of;
use function trait_exists;

final class MetaLoader
{
Expand Down Expand Up @@ -147,18 +149,27 @@ private function createRuntimeMeta(ReflectionClass $class): array

/**
* @param list<string> $paths
* @param list<string> $excludePaths
*/
public function preloadFromPaths(array $paths): void
public function preloadFromPaths(array $paths, array $excludePaths = []): void
{
$loader = new RobotLoader();

foreach ($paths as $path) {
$loader->addDirectory($path);
}

foreach ($excludePaths as $excludePath) {
$loader->excludeDirectory($excludePath);
}

$loader->rebuild();

foreach ($loader->getIndexedClasses() as $class => $file) {
assert(class_exists($class));
require_once $file;

assert(class_exists($class) || interface_exists($class) || trait_exists($class));

$classRef = new ReflectionClass($class);

if (!$classRef->isSubclassOf(MappedObject::class)) {
Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/Meta/MetaLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Tests\Orisai\ObjectMapper\Doubles\FieldNames\FieldNameIdenticalWithAnotherPropertyNameVO;
use Tests\Orisai\ObjectMapper\Doubles\FieldNames\MultipleIdenticalFieldNamesVO;
use Tests\Orisai\ObjectMapper\Toolkit\ProcessingTestCase;
use const PHP_VERSION_ID;

final class MetaLoaderTest extends ProcessingTestCase
{
Expand Down Expand Up @@ -69,4 +70,42 @@ public function testMultipleIdenticalPropertyNames(): void
$this->metaLoader->load(ChildCollidingFieldVO::class);
}

/**
* @runInSeparateProcess
*/
public function testPreload(): void
{
$excludes = [];
$excludes[] = __DIR__ . '/../../Doubles/FieldNames/FieldNameIdenticalWithAnotherPropertyNameVO.php';
$excludes[] = __DIR__ . '/../../Doubles/FieldNames/MultipleIdenticalFieldNamesVO.php';
$excludes[] = __DIR__ . '/../../Doubles/FieldNames/ChildCollidingFieldVO.php';
$excludes[] = __DIR__ . '/../../Doubles/Constructing/DependentVO.php';

if (PHP_VERSION_ID < 8_00_00) {
$excludes[] = __DIR__ . '/../../Doubles/PhpVersionSpecific/AttributesVO.php';
}

if (PHP_VERSION_ID < 8_01_00) {
$excludes[] = __DIR__ . '/../../Doubles/Callbacks/ObjectInitializingVoPhp81.php';
$excludes[] = __DIR__ . '/../../Doubles/Enums';
$excludes[] = __DIR__ . '/../../Doubles/PhpVersionSpecific/ConstructorPromotedVO.php';
$excludes[] = __DIR__ . '/../../Doubles/PhpVersionSpecific/ObjectDefaultVO.php';
$excludes[] = __DIR__ . '/../../Doubles/PhpVersionSpecific/ReadonlyPropertiesVO.php';
}

if (PHP_VERSION_ID < 8_02_00) {
$excludes[] = __DIR__ . '/../../Doubles/PhpVersionSpecific/ReadonlyClassVO.php';
}

$this->metaLoader->preloadFromPaths(
[
__DIR__ . '/../../Doubles',
],
$excludes,
);

// Makes PHPUnit happy
self::assertTrue(true);
}

}
5 changes: 5 additions & 0 deletions tools/phpstan.baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ parameters:
count: 1
path: ../tests/Doubles/Inheritance/trait-callback.php

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertTrue\\(\\) with true will always evaluate to true\\.$#"
count: 1
path: ../tests/Unit/Meta/MetaLoaderTest.php

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertSame\\(\\) with ReflectionClass\\<Tests\\\\Orisai\\\\ObjectMapper\\\\Unit\\\\Meta\\\\Runtime\\\\CallbackRuntimeMetaTest\\> and ReflectionClass\\<Orisai\\\\ObjectMapper\\\\MappedObject\\> will always evaluate to false\\.$#"
count: 1
Expand Down

0 comments on commit d8b2715

Please sign in to comment.