I'm working on a package that interacts with the current request to retrieve information. To retrieve that information, I use $request = Container::getInstance()->make('request');.
It works perfect when I use it in a Laravel application. However, I'm trying to get full code coverage in my tests. I believe I need to create a new Container object and pass it a request for my test, but it's not working out so great. Here's what I'm doing...
/**
* Create a mock request
*
* @return Illuminate\Http\Request
*/
public function createDummyRequest($headers = [], $payload = null): Request
{
return new Request([], [], [], [], [], $headers, $payload);
}
/**
* Create a dummy IOC container
*
* @return Illuminate\Container\Container
*/
public function createDummyContainer(): Container
{
return new Container;
$reflectionClass = new Reflect(Container::class);
return $reflectionClass->newInstance();
}
/** @test */
public function response_preferred_format_returns_xml()
{
$container = $this->createDummyContainer();
$request = $this->createDummyRequest(['Accept' => 'application/json'], json_encode($this->testArray));
$container->instance('request', $request);
$response = $this->createDummyResponse()->preferredFormat($this->testArray, 200, ['Accept' => 'application/xml']);
$this->assertEquals($this->testXml, $response->getContent());
}
The error I get is:
ReflectionException: Class request does not exist
Am I on the right track, or am I missing something obvious? Why can't I instantiate the Container class and bind an instance of Request so my code can utilize it?
Aucun commentaire:
Enregistrer un commentaire