Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] native functions in toBeUsedInNothing and toBeUsedIn #4

Merged
merged 1 commit into from Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Blueprint.php
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
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
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
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
@@ -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
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
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