Skip to content

New method for getting the content #734

Closed as not planned
Closed as not planned
@marineusde

Description

@marineusde

In the FileGetContents-Class is this code:

$response = file_get_contents($uri);

That do some problems in unit tests, cause you cant mock or fake the schema files. My example idea is this in Laravel, if someone want to test the code later (or extend the file in a seperate file):

$fileGetContents = new class extends FileGetContents
{
  protected function getContents(string $uri): false|string
  {
     if (!File::exists($uri)) {
       throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
     }

     return File::get($uri);
  }
};

$factory = new Factory();
$uriRetriever = $factory->getUriRetriever();
$uriRetriever->setUriRetriever($fileGetContents);

In the unit test, you can mock it:

File::shouldReceive('exists')
  ->with('test.json')
  ->andReturn(true);

  File::shouldReceive('get')
  ->with('file://test.json')
  ->andReturn('{...}');

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions