Skip to content

Commit

Permalink
Merge pull request #4 from fabio-ivona/allow-php-native-functions
Browse files Browse the repository at this point in the history
[2.x] native functions in toBeUsedInNothing and toBeUsedIn
  • Loading branch information
nunomaduro authored Aug 21, 2023
2 parents f44834b + ae57d1d commit e3f51ae
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function expectToUse(LayerOptions $options, callable $failure): void
AssertLocker::incrementAndLock();

foreach ($this->target->value as $targetValue) {
$targetLayer = $this->layerFactory->make($options, $targetValue);
$targetLayer = $this->layerFactory->make($options, $targetValue, false);

foreach ($this->dependencies->values as $dependency) {
$dependencyLayer = $this->layerFactory->make($options, $dependency->value);
Expand Down Expand Up @@ -164,7 +164,7 @@ public function expectToOnlyBeUsedIn(LayerOptions $options, callable $failure):
AssertLocker::incrementAndLock();

foreach (Composer::userNamespaces() as $namespace) {
$namespaceLayer = $this->layerFactory->make($options, $namespace);
$namespaceLayer = $this->layerFactory->make($options, $namespace, false);

foreach ($this->dependencies->values as $dependency) {
$namespaceLayer = $namespaceLayer->excludeByNameStart($dependency->value);
Expand Down
4 changes: 2 additions & 2 deletions src/Factories/LayerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(
/**
* Make a new Layer using the given name.
*/
public function make(LayerOptions $options, string $name): Layer
public function make(LayerOptions $options, string $name, bool $onlyUserDefinedUses = true): Layer
{
$objects = array_map(function (ObjectDescription $object) use ($options): ObjectDescription {
if ($object instanceof VendorObjectDescription) {
Expand Down Expand Up @@ -57,7 +57,7 @@ public function make(LayerOptions $options, string $name): Layer
);

return $object;
}, $this->objectsStorage->allByNamespace($name));
}, $this->objectsStorage->allByNamespace($name, $onlyUserDefinedUses));

$layer = Layer::fromBase($objects)->leaveByNameStart($name);

Expand Down
4 changes: 2 additions & 2 deletions src/Factories/ObjectDescriptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class ObjectDescriptionFactory
/**
* Makes a new Object Description instance, is possible.
*/
public static function make(string $filename): ?ObjectDescription
public static function make(string $filename, bool $onlyUserDefinedUses = true): ?ObjectDescription
{
self::ensureServiceContainerIsInitialized();

Expand All @@ -49,7 +49,7 @@ public static function make(string $filename): ?ObjectDescription
$object->uses = new ObjectUses(array_values(
array_filter(
iterator_to_array($object->uses->getIterator()),
static fn (string $use): bool => self::isUserDefined($use) && ! self::isSameLayer($object, $use),
static fn (string $use): bool => (! $onlyUserDefinedUses || self::isUserDefined($use)) && ! self::isSameLayer($object, $use),
)
));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Repositories/ObjectsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function getInstance(): self
*
* @return array<int, ObjectDescription|FunctionDescription>
*/
public function allByNamespace(string $namespace): array
public function allByNamespace(string $namespace, bool $onlyUserDefinedUses = true): array
{
if (function_exists($namespace) && (new ReflectionFunction($namespace))->getName() === $namespace) {
return [
Expand All @@ -91,7 +91,7 @@ public function allByNamespace(string $namespace): array
}

$objectsPerPrefix = array_values(array_filter(array_reduce($directories, fn (array $files, string $fileOrDirectory): array => array_merge($files, array_values(array_map(
static fn (SplFileInfo $file): ?ObjectDescription => ObjectDescriptionFactory::make($file->getPathname()),
static fn (SplFileInfo $file): ?ObjectDescription => ObjectDescriptionFactory::make($file->getPathname(), $onlyUserDefinedUses),
is_dir($fileOrDirectory) ? iterator_to_array(Finder::create()->files()->in($fileOrDirectory)->name('*.php')) : [new SplFileInfo($fileOrDirectory)],
))), [])));

Expand Down
11 changes: 11 additions & 0 deletions tests/Fixtures/Misc/HasSleepFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Tests\Fixtures\Misc;

class HasSleepFunction
{
public function startSleeping(): void
{
sleep(1);
}
}
7 changes: 7 additions & 0 deletions tests/ToBeUsedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
}
});

it('failure with native functions', function () {
expect('sleep')->not->toBeUsedIn('Tests\Fixtures\Misc\HasSleepFunction');
})->throws(
ExpectationFailedException::class,
"Expecting 'sleep' not to be used in 'Tests\Fixtures\Misc\HasSleepFunction'."
);

it('fails 1', function () {
expect([Fooable::class])
->toBeUsedIn(['Tests\Fixtures\Models', 'Tests\Fixtures\Controllers']);
Expand Down
8 changes: 8 additions & 0 deletions tests/ToBeUsedInNothing.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
);
});

it('fails with native functions', function () {
expect(fn () => expect('sleep')->not->toBeUsed())->toThrowArchitectureViolation(
"Expecting 'sleep' not to be used on 'Tests\Fixtures\Misc\HasSleepFunction'.",
'tests/Fixtures/Misc/HasSleepFunction.php',
10
);
});

test('ignoring', function () {
expect(Fooable::class)->toBeUsedInNothing()->ignoring('Tests\Fixtures\Models');
});
Expand Down

0 comments on commit e3f51ae

Please sign in to comment.