Summer Sale! All accounts are 50% off this week.

jmurphy1267's avatar

Laravel Package Testing with Singleton

I am trying to develop a package for an api and I have a config singleton that all the clients use to get the config options for the endpoints

final class ConfigRepository { private $baseUrl;

public function __construct(
    string $baseUrl,
)
{
    $this->baseUrl = $baseUrl;
   
}

/**
 * @return string
 */
public function getBaseUrl(): string
{
    return $this->baseUrl;
}

}

In one of the client constructors I have

public function __construct(
    HttpClientAdapterInterface $httpClientAdapter
)
{
    $this->oktaConfigRepository = App::make(OktaConfigRepository::class);
    $this->httpClientAdapter = $httpClientAdapter;
}

In the package service provider I have this singleton like so

$this->app->singleton(ConfigRepository::class, function (Application $app) {
        return new ConfigRepository(
            $app['config']->get('laravel-api.base_url')
        );
    });

When I make an instance of the client in a test class and run one of my tests I get this error Unresolvable dependency resolving [Parameter #0 [ string $baseUrl ]] in class ConfigRepository. My test is like below

 protected function setUp(): void
 {
     parent::setUp(); // TODO: Change the autogenerated stub
     $this->userClient = new UserClient(
         new LaravelHttpFacadeAdapter()
     );
   
 }

public function testGetUser()
{
    Http::fake();
    $this->userClient->getUser('testid');

    Http::assertSent(function (Request $request){
        return $request->hasHeader('Accept') &&
            $request->hasHeader('Content-Type') &&
            $request->hasHeader('Authorization');
    });

}

How would I resolve the dependency in the testing class I am extending the \Orchestra\Testbench\TestCase

0 likes
0 replies

Please or to participate in this conversation.