Skip to content

Commit

Permalink
refactor: simplify JsonFileSourceReader by consolidating file system …
Browse files Browse the repository at this point in the history
…dependencies
  • Loading branch information
thorbrink committed Feb 25, 2025
1 parent e7a546d commit c02fc9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
namespace Municipio\ExternalContent\SourceReaders;

use Municipio\ExternalContent\JsonToSchemaObjects\JsonToSchemaObjects;
use WpService\FileSystem\FileExists;
use WpService\FileSystem\GetFileContent;
use WpService\FileSystem\FileSystem;

class JsonFileSourceReader implements SourceReaderInterface
{
public function __construct(
private string $filePath,
private GetFileContent $fileSystem,
private FileExists $fileExists,
private FileSystem $fileSystem,
private JsonToSchemaObjects $jsonToSchemaObjects,
)
{
Expand All @@ -22,7 +20,7 @@ public function __construct(
*/
public function getSourceData(): array
{
if( ! $this->fileExists->fileExists($this->filePath) ) {
if( ! $this->fileSystem->fileExists($this->filePath) ) {
throw new \InvalidArgumentException('File does not exist');
}

Expand Down
34 changes: 14 additions & 20 deletions library/ExternalContent/SourceReaders/JsonFileSourceReader.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,61 @@
use Municipio\ExternalContent\JsonToSchemaObjects\JsonToSchemaObjects;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use WpService\FileSystem\FileExists;
use WpService\FileSystem\GetFileContent;
use WpService\FileSystem\FileSystem;

class JsonFileSourceReaderTest extends TestCase {

/**
* @testdox class can be instantiated
*/
public function testCanBeInstantiated() {
$jsonFileSourceReader = new JsonFileSourceReader('', $this->getGetFileContentMock(), $this->getFileExistsMock(), $this->getJsonToSchemaObjectsMock());
$jsonFileSourceReader = new JsonFileSourceReader('', $this->getFileSystemMock(), $this->getJsonToSchemaObjectsMock());
$this->assertInstanceOf(JsonFileSourceReader::class, $jsonFileSourceReader);
}

/**
* @testdox getSourceData() returns an array
*/
public function testGetSourceDataReturnsArrayOfSchemaObjects() {
$fileExists = $this->getFileExistsMock();
$fileExists->method('fileExists')->willReturn(true);
$jsonFileSourceReader = new JsonFileSourceReader('', $this->getGetFileContentMock(), $fileExists, $this->getJsonToSchemaObjectsMock());
$fileSystem = $this->getFileSystemMock();
$fileSystem->method('fileExists')->willReturn(true);
$jsonFileSourceReader = new JsonFileSourceReader('', $fileSystem, $this->getJsonToSchemaObjectsMock());
$this->assertIsArray($jsonFileSourceReader->getSourceData());
}

/**
* @testdox getSourceData() transforms json to schema objects
*/
public function testGetSourceDataReturnsArrayOfSchemaObjectsFoundInJsonFile() {
$fileExists = $this->getFileExistsMock();
$fileExists->method('fileExists')->willReturn(true);
$fileSystem = $this->getFileSystemMock();
$fileSystem->method('fileExists')->willReturn(true);
$json = '[{ "@context": "https://schema.org", "@type": "Organization", "name": "Helsingborgs stad"}]';
$getFileContent = $this->getGetFileContentMock();
$jsonToSchemaObjects = $this->getJsonToSchemaObjectsMock();

$getFileContent->expects($this->once())->method('getFileContent')->willReturn($json);
$fileSystem->expects($this->once())->method('getFileContent')->willReturn($json);
$jsonToSchemaObjects->method('transform')->with($json)->willReturn([]);

$jsonFileSourceReader = new JsonFileSourceReader('', $getFileContent, $fileExists, $jsonToSchemaObjects);
$jsonFileSourceReader = new JsonFileSourceReader('', $fileSystem, $jsonToSchemaObjects);
$jsonFileSourceReader->getSourceData();
}

/**
* @testdox getSourceData() throws if file does not exist
*/
public function testGetSourceDataThrowsIfFileDoesNotExist() {
$fileExists = $this->getFileExistsMock();
$fileExists->method('fileExists')->willReturn(false);
$fileSystem = $this->getFileSystemMock();
$fileSystem->method('fileExists')->willReturn(false);

$this->expectException(\InvalidArgumentException::class);
$jsonFileSourceReader = new JsonFileSourceReader('invalid filePath', $this->getGetFileContentMock(), $fileExists, $this->getJsonToSchemaObjectsMock());
$jsonFileSourceReader = new JsonFileSourceReader('invalid filePath', $fileSystem, $this->getJsonToSchemaObjectsMock());
$jsonFileSourceReader->getSourceData();
}

private function getJsonToSchemaObjectsMock(): JsonToSchemaObjects|MockObject {
return $this->createMock(JsonToSchemaObjects::class);
}

private function getGetFileContentMock(): GetFileContent|MockObject {
return $this->createMock(GetFileContent::class);
}

private function getFileExistsMock(): FileExists|MockObject {
return $this->createMock(FileExists::class);
private function getFileSystemMock(): FileSystem|MockObject {
return $this->createMock(FileSystem::class);
}
}

0 comments on commit c02fc9e

Please sign in to comment.