Laravel Mockery phpunit error: Unresolvable dependency resolving

79 views Asked by At

Fairly new to testing but can't this test with a facade to work correctly. The code itself is working correctly. Just want the test to work also :')

The serviceprovider:

<?php

class ServiceProvider extends AbstractServiceProvider implements DeferrableProvider
{
    public function register(): void
    {
        $this->app->singleton(RedisHashManager::class, $this->hashManagerSingleton(...));
    }

    /**
     * @return array<int, string>
     */
    public function provides(): array
    {
        return [RedisHashManager::class];
    }

    private function hashManagerSingleton(Application $app): RedisHashManager
    {
        return new RedisHashManager($app->make(Factory::class), 'cache');
    }
}

The facade

<?php

class RedisHashCache extends Facade
{
    protected static function getFacadeAccessor(): string
    {
        return RedisHashManager::class;
    }
}

The code to test:

<?php

trait HashTrait
{
    protected function getHashes(array $keys, array $fields): Collection
    {
        $keys = collect($keys)->mapWithKeys(static function ($key) use ($fields) {
            return [$key => $fields];
        })->toArray();

        $fieldCount = count($fields);

        return RedisHashCache::getHashes($keys)
            ->reject(static function ($data) use ($fieldCount) {
                return count($data) !== $fieldCount;
            });
    }
}

The test itself:

<?php

class HashTraitTest extends TestCase
{
    use HashTrait;

    public function testGetHashes(): void
    {
        RedisHashCache::shouldReceive('getHashes')
            ->once();

        $this->getHashes(['canidae:canis:lupus:lycaon'], ['color', 'food']);
    }
}

(I removed the namespaces and uses, they are in the real code)

When I run the test, I got the following error: Illuminate\Contracts\Container\BindingResolutionException: Unresolvable dependency resolving [Parameter #1 [ <required> string $connectionName ]] in class AbstractRedisManager

The AbstractRedisManager is extended by the RedisHashManager and requires 2 parameters in the construct: a RedisFactory as the first one which is resolving correctly and a simple string which is giving the error.

The second parameter hasn't got a default value, when I set a default value it's working correctly. But I don't know if that is the correct/only fix I've got for the problem.

Hopefully someone could help me out?

0

There are 0 answers