Skip to content

Commit

Permalink
Merge pull request #307 from cultuurnet/III-3558-improve-file-get-con…
Browse files Browse the repository at this point in the history
…tents-return-value

III-3558 Improve `file_get_contents` return value
  • Loading branch information
LucWollants authored Aug 22, 2024
2 parents 2ff9630 + d2933b5 commit 04fb563
Show file tree
Hide file tree
Showing 20 changed files with 69 additions and 31 deletions.
3 changes: 2 additions & 1 deletion app/RoutingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use CultureFeed;
use CultureFeed_DefaultOAuthClient;
use CultuurNet\UDB3\Search\FileReader;
use CultuurNet\UDB3\Search\Http\Authentication\Auth0\Auth0TokenGenerator;
use CultuurNet\UDB3\Search\Http\Authentication\Auth0\Auth0MetadataGenerator;
use CultuurNet\UDB3\Search\Http\Authentication\AuthenticateRequest;
Expand Down Expand Up @@ -63,7 +64,7 @@ function (): Router {
new InMemoryDefaultQueryRepository(
file_exists(__DIR__ . '/../default_queries.php') ? require __DIR__ . '/../default_queries.php' : []
),
file_get_contents('file://' . __DIR__ . '/../' . $pemFile)
FileReader::read('file://' . __DIR__ . '/../' . $pemFile)
);

$logger = LoggerFactory::create($this->leagueContainer, LoggerName::forWeb());
Expand Down
3 changes: 2 additions & 1 deletion src/ElasticSearch/Operations/AbstractMappingOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\Operations;

use CultuurNet\UDB3\Search\FileReader;
use CultuurNet\UDB3\Search\Json;

abstract class AbstractMappingOperation extends AbstractElasticSearchOperation
Expand All @@ -15,7 +16,7 @@ protected function updateMapping(string $indexName, string $documentType, string
'index' => $indexName,
'type' => $documentType,
'body' => Json::decodeAssociatively(
file_get_contents($mappingFilePath)
FileReader::read($mappingFilePath)
),
]
);
Expand Down
3 changes: 2 additions & 1 deletion src/ElasticSearch/Operations/CreateAutocompleteAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\Operations;

use CultuurNet\UDB3\Search\FileReader;
use CultuurNet\UDB3\Search\Json;

final class CreateAutocompleteAnalyzer extends AbstractElasticSearchOperation
Expand All @@ -14,7 +15,7 @@ public function run(): void
[
'name' => 'autocomplete_analyzer',
'body' => Json::decodeAssociatively(
file_get_contents(__DIR__ . '/json/analyzer_autocomplete.json')
FileReader::read(__DIR__ . '/json/analyzer_autocomplete.json')
),
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\Operations;

use CultuurNet\UDB3\Search\FileReader;
use CultuurNet\UDB3\Search\Json;

final class CreateLowerCaseExactMatchAnalyzer extends AbstractElasticSearchOperation
Expand All @@ -14,7 +15,7 @@ public function run(): void
[
'name' => 'lowercase_exact_match_analyzer',
'body' => Json::decodeAssociatively(
file_get_contents(__DIR__ . '/json/analyzer_lowercase_exact_match.json')
FileReader::read(__DIR__ . '/json/analyzer_lowercase_exact_match.json')
),
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\Operations;

use CultuurNet\UDB3\Search\FileReader;
use CultuurNet\UDB3\Search\Json;

final class CreateLowerCaseStandardAnalyzer extends AbstractElasticSearchOperation
Expand All @@ -14,7 +15,7 @@ public function run(): void
[
'name' => 'lowercase_standard_analyzer',
'body' => Json::decodeAssociatively(
file_get_contents(__DIR__ . '/json/analyzer_lowercase_standard.json')
FileReader::read(__DIR__ . '/json/analyzer_lowercase_standard.json')
),
]
);
Expand Down
19 changes: 19 additions & 0 deletions src/FileReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace CultuurNet\UDB3\Search;

final class FileReader
{
public static function read(string $filepath): string
{
$content = file_get_contents($filepath);

if ($content === false) {
throw new \RuntimeException('Failed to read file ' . $filepath);
}

return $content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\Http\Authentication\Token;

use CultuurNet\UDB3\Search\FileReader;
use CultuurNet\UDB3\Search\Json;
use DateTimeImmutable;

Expand All @@ -22,7 +23,7 @@ public function get(): ?Token
return null;
}

$tokenAsArray = Json::decodeAssociatively(file_get_contents($this->fullFilePath));
$tokenAsArray = Json::decodeAssociatively(FileReader::read($this->fullFilePath));

return new Token(
$tokenAsArray['token'],
Expand Down
3 changes: 2 additions & 1 deletion tests/AMQP/DomainMessageJSONDeserializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace CultuurNet\UDB3\Search\AMQP;

use Broadway\Serializer\Serializable;
use CultuurNet\UDB3\Search\FileReader;
use InvalidArgumentException;
use Broadway\Domain\DateTime;
use Broadway\Domain\DomainMessage;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function it_throws_an_error_if_payloadclass_does_not_implement_Serializab
*/
public function it_can_deserialize_a_domain_message(): void
{
$jsonData = file_get_contents(__DIR__ . '/Dummies/domain-message-dummy-event.json');
$jsonData = FileReader::read(__DIR__ . '/Dummies/domain-message-dummy-event.json');

$expectedDomainMessage = new DomainMessage(
'message-id-123',
Expand Down
5 changes: 3 additions & 2 deletions tests/ElasticSearch/JsonDocument/EventTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\JsonDocument;

use CultuurNet\UDB3\Search\FileReader;
use DateTimeInterface;
use Cake\Chronos\Chronos;
use CultuurNet\UDB3\Search\ElasticSearch\PathEndIdUrlParser;
Expand Down Expand Up @@ -537,12 +538,12 @@ public function it_transforms_completeness(): void

private function transformAndAssert(string $givenFilePath, string $expectedFilePath, array $expectedLogs = []): void
{
$original = Json::decodeAssociatively(file_get_contents($givenFilePath));
$original = Json::decodeAssociatively(FileReader::read($givenFilePath));

// Compare the expected and actual JSON as objects, not arrays. Some Elasticsearch fields expect an empty object
// specifically instead of an empty array in some scenario's. But if we decode to arrays, empty JSON objects
// become empty arrays in PHP.
$expected = Json::decode(file_get_contents($expectedFilePath));
$expected = Json::decode(FileReader::read($expectedFilePath));
$actual = Json::decode(
Json::encode(
$this->transformer->transform($original, [])
Expand Down
7 changes: 4 additions & 3 deletions tests/ElasticSearch/JsonDocument/OrganizerTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\JsonDocument;

use CultuurNet\UDB3\Search\FileReader;
use DateTimeInterface;
use Cake\Chronos\Chronos;
use CultuurNet\UDB3\Search\ElasticSearch\PathEndIdUrlParser;
Expand Down Expand Up @@ -151,7 +152,7 @@ public function it_indexes_educational_descriptions(): void
public function it_should_log_warnings_if_an_address_translation_is_incomplete(): void
{
$original = Json::decodeAssociatively(
file_get_contents(__DIR__ . '/data/organizer/original_with_incomplete_translated_address.json')
FileReader::read(__DIR__ . '/data/organizer/original_with_incomplete_translated_address.json')
);

$expectedLogs = [
Expand Down Expand Up @@ -199,12 +200,12 @@ public function it_handles_completeness(): void

private function transformAndAssert(string $givenFilePath, string $expectedFilePath, array $expectedLogs = []): void
{
$original = Json::decodeAssociatively(file_get_contents($givenFilePath));
$original = Json::decodeAssociatively(FileReader::read($givenFilePath));

// Compare the expected and actual JSON as objects, not arrays. Some Elasticsearch fields expect an empty object
// specifically instead of an empty array in some scenario's. But if we decode to arrays, empty JSON objects
// become empty arrays in PHP.
$expected = Json::decode(file_get_contents($expectedFilePath));
$expected = Json::decode(FileReader::read($expectedFilePath));
$actual = Json::decode(
Json::encode(
$this->transformer->transform($original, [])
Expand Down
9 changes: 5 additions & 4 deletions tests/ElasticSearch/JsonDocument/PlaceTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\JsonDocument;

use CultuurNet\UDB3\Search\FileReader;
use DateTimeInterface;
use Cake\Chronos\Chronos;
use CultuurNet\UDB3\Search\ElasticSearch\PathEndIdUrlParser;
Expand Down Expand Up @@ -99,7 +100,7 @@ public function it_logs_missing_required_fields(): void
*/
public function it_should_log_a_warning_if_address_is_not_found_in_the_main_language(): void
{
$original = file_get_contents(__DIR__ . '/data/place/original-without-address-in-main-language.json');
$original = FileReader::read(__DIR__ . '/data/place/original-without-address-in-main-language.json');

$expectedLogs = [
['warning', "Missing expected field 'address.nl'.", []],
Expand All @@ -115,7 +116,7 @@ public function it_should_log_a_warning_if_address_is_not_found_in_the_main_lang
*/
public function it_should_log_warnings_if_an_address_translation_is_incomplete(): void
{
$original = file_get_contents(__DIR__ . '/data/place/original-with-incomplete-address-translation.json');
$original = FileReader::read(__DIR__ . '/data/place/original-with-incomplete-address-translation.json');

$expectedLogs = [
['warning', "Missing expected field 'address.fr.addressCountry'.", []],
Expand Down Expand Up @@ -311,12 +312,12 @@ public function it_transforms_completeness(): void

private function transformAndAssert(string $givenFilePath, string $expectedFilePath, array $expectedLogs = []): void
{
$original = Json::decodeAssociatively(file_get_contents($givenFilePath));
$original = Json::decodeAssociatively(FileReader::read($givenFilePath));

// Compare the expected and actual JSON as objects, not arrays. Some Elasticsearch fields expect an empty object
// specifically instead of an empty array in some scenario's. But if we decode to arrays, empty JSON objects
// become empty arrays in PHP.
$expected = Json::decode(file_get_contents($expectedFilePath));
$expected = Json::decode(FileReader::read($expectedFilePath));
$actual = Json::decode(
Json::encode(
$this->transformer->transform($original, [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Broadway\Domain\DomainEventStream;
use Broadway\EventHandling\EventBus;
use CultuurNet\UDB3\Search\Event\EventProjectedToJSONLD;
use CultuurNet\UDB3\Search\FileReader;
use CultuurNet\UDB3\Search\Json;
use CultuurNet\UDB3\Search\Organizer\OrganizerProjectedToJSONLD;
use CultuurNet\UDB3\Search\Place\PlaceProjectedToJSONLD;
Expand Down Expand Up @@ -411,7 +412,7 @@ function (DomainEventStream $stream) use (&$actualEvents): void {

private function getJsonDocumentAsElasticSearchResults(string $filePath): array
{
$contents = file_get_contents($filePath);
$contents = FileReader::read($filePath);
return Json::decodeAssociatively($contents);
}
}
7 changes: 4 additions & 3 deletions tests/ElasticSearch/Operations/IndexRegionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\Operations;

use CultuurNet\UDB3\Search\FileReader;
use CultuurNet\UDB3\Search\Json;
use Elasticsearch\Client;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -41,7 +42,7 @@ public function it_indexes_all_files_located_in_the_given_path_or_subdirectories
'id' => 'gem-antwerpen',
'type' => 'region',
'body' => Json::decodeAssociatively(
file_get_contents(__DIR__ . '/data/regions/municipalities/gem-antwerpen.json')
FileReader::read(__DIR__ . '/data/regions/municipalities/gem-antwerpen.json')
),
],
],
Expand All @@ -51,7 +52,7 @@ public function it_indexes_all_files_located_in_the_given_path_or_subdirectories
'id' => 'gem-leuven',
'type' => 'region',
'body' => Json::decodeAssociatively(
file_get_contents(__DIR__ . '/data/regions/municipalities/gem-leuven.json')
FileReader::read(__DIR__ . '/data/regions/municipalities/gem-leuven.json')
),
],
],
Expand All @@ -61,7 +62,7 @@ public function it_indexes_all_files_located_in_the_given_path_or_subdirectories
'id' => 'prov-vlaams-brabant',
'type' => 'region',
'body' => Json::decodeAssociatively(
file_get_contents(__DIR__ . '/data/regions/provinces/prov-vlaams-brabant.json')
FileReader::read(__DIR__ . '/data/regions/provinces/prov-vlaams-brabant.json')
),
],
]
Expand Down
3 changes: 2 additions & 1 deletion tests/ElasticSearch/Operations/UpdateEventMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\Operations;

use CultuurNet\UDB3\Search\FileReader;
use CultuurNet\UDB3\Search\Json;
use Elasticsearch\Client;
use Psr\Log\LoggerInterface;
Expand All @@ -23,7 +24,7 @@ protected function getDocumentType(): string
protected function getExpectedMappingBody(): array
{
return Json::decodeAssociatively(
file_get_contents(__DIR__ . '/../../../src/ElasticSearch/Operations/json/mapping_event.json')
FileReader::read(__DIR__ . '/../../../src/ElasticSearch/Operations/json/mapping_event.json')
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\Operations;

use CultuurNet\UDB3\Search\FileReader;
use CultuurNet\UDB3\Search\Json;
use Elasticsearch\Client;
use Psr\Log\LoggerInterface;
Expand All @@ -23,7 +24,7 @@ protected function getDocumentType(): string
protected function getExpectedMappingBody(): array
{
return Json::decodeAssociatively(
file_get_contents(__DIR__ . '/../../../src/ElasticSearch/Operations/json/mapping_organizer.json')
FileReader::read(__DIR__ . '/../../../src/ElasticSearch/Operations/json/mapping_organizer.json')
);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/ElasticSearch/Operations/UpdatePlaceMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\Operations;

use CultuurNet\UDB3\Search\FileReader;
use CultuurNet\UDB3\Search\Json;
use Elasticsearch\Client;
use Psr\Log\LoggerInterface;
Expand All @@ -23,7 +24,7 @@ protected function getDocumentType(): string
protected function getExpectedMappingBody(): array
{
return Json::decodeAssociatively(
file_get_contents(__DIR__ . '/../../../src/ElasticSearch/Operations/json/mapping_place.json')
FileReader::read(__DIR__ . '/../../../src/ElasticSearch/Operations/json/mapping_place.json')
);
}

Expand Down
7 changes: 4 additions & 3 deletions tests/ElasticSearch/Region/GeoShapeQueryRegionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\Region;

use CultuurNet\UDB3\Search\FileReader;
use RuntimeException;
use CultuurNet\UDB3\Search\Json;
use CultuurNet\UDB3\Search\Region\RegionId;
Expand Down Expand Up @@ -97,8 +98,8 @@ public function it_uses_a_percolate_query_and_returns_all_region_ids_of_the_matc
]
)
->willReturnOnConsecutiveCalls(
Json::decodeAssociatively(file_get_contents(__DIR__ . '/data/regions_1.json')),
Json::decodeAssociatively(file_get_contents(__DIR__ . '/data/regions_2.json'))
Json::decodeAssociatively(FileReader::read(__DIR__ . '/data/regions_1.json')),
Json::decodeAssociatively(FileReader::read(__DIR__ . '/data/regions_2.json'))
);

$expectedRegionIds = [
Expand Down Expand Up @@ -141,7 +142,7 @@ public function it_throws_an_exception_if_it_gets_an_invalid_response_from_elast
{
$this->client->expects($this->once())
->method('search')
->willReturn(Json::decodeAssociatively(file_get_contents(__DIR__ . '/data/regions_invalid.json')));
->willReturn(Json::decodeAssociatively(FileReader::read(__DIR__ . '/data/regions_invalid.json')));

$this->expectException(RuntimeException::class);

Expand Down
3 changes: 2 additions & 1 deletion tests/Http/Authentication/AuthenticateRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Crell\ApiProblem\ApiProblem;
use CultureFeed_Consumer;
use CultuurNet\UDB3\Search\FileReader;
use CultuurNet\UDB3\Search\Http\Authentication\ApiProblems\BlockedApiKey;
use CultuurNet\UDB3\Search\Http\Authentication\ApiProblems\InvalidApiKey;
use CultuurNet\UDB3\Search\Http\Authentication\ApiProblems\InvalidToken;
Expand Down Expand Up @@ -66,7 +67,7 @@ protected function setUp(): void

$this->cultureFeed = $this->createMock(ICultureFeed::class);

$this->pemFile = file_get_contents(__DIR__ . '/samples/public.pem');
$this->pemFile = FileReader::read(__DIR__ . '/samples/public.pem');

$managementToken = new Token(
'my_auth0_token',
Expand Down
Loading

0 comments on commit 04fb563

Please sign in to comment.