diff --git a/src/DateTimeFactory.php b/src/DateTimeFactory.php new file mode 100644 index 00000000..894fc7cb --- /dev/null +++ b/src/DateTimeFactory.php @@ -0,0 +1,24 @@ + $availableTo) { diff --git a/src/ElasticSearch/JsonDocument/Properties/CalendarTransformer.php b/src/ElasticSearch/JsonDocument/Properties/CalendarTransformer.php index 6a3f7db0..cb0dd88b 100644 --- a/src/ElasticSearch/JsonDocument/Properties/CalendarTransformer.php +++ b/src/ElasticSearch/JsonDocument/Properties/CalendarTransformer.php @@ -5,6 +5,7 @@ namespace CultuurNet\UDB3\Search\ElasticSearch\JsonDocument\Properties; use Cake\Chronos\Chronos; +use CultuurNet\UDB3\Search\DateTimeFactory; use CultuurNet\UDB3\Search\JsonDocument\JsonTransformer; use CultuurNet\UDB3\Search\JsonDocument\JsonTransformerLogger; use DateInterval; @@ -474,13 +475,13 @@ private function convertSubEventToLocalTimeRanges(array $subEvent, DateTimeZone // When converting the dates to times it's important we set the right timezone, because sometimes the dates are // in UTC for example and then the time info is not what we'd expect to be in Belgium. if (isset($subEvent['startDate'])) { - $startDate = DateTimeImmutable::createFromFormat(DateTime::ATOM, $subEvent['startDate']); + $startDate = DateTimeFactory::fromAtom($subEvent['startDate']); $startDate = $startDate->setTimezone($timezone); $startTime = $startDate->format('Hi'); } if (isset($subEvent['endDate'])) { - $endDate = DateTimeImmutable::createFromFormat(DateTime::ATOM, $subEvent['endDate']); + $endDate = DateTimeFactory::fromAtom($subEvent['endDate']); $endDate = $endDate->setTimezone($timezone); $endTime = $endDate->format('Hi'); } diff --git a/src/Http/Offer/RequestParser/AvailabilityOfferRequestParser.php b/src/Http/Offer/RequestParser/AvailabilityOfferRequestParser.php index 2cc9a49a..83001fda 100644 --- a/src/Http/Offer/RequestParser/AvailabilityOfferRequestParser.php +++ b/src/Http/Offer/RequestParser/AvailabilityOfferRequestParser.php @@ -7,6 +7,7 @@ use CultuurNet\UDB3\Search\Http\ApiRequestInterface; use CultuurNet\UDB3\Search\Offer\OfferQueryBuilderInterface; use DateTimeImmutable; +use InvalidArgumentException; final class AvailabilityOfferRequestParser implements OfferRequestParserInterface { @@ -14,8 +15,11 @@ public function parse(ApiRequestInterface $request, OfferQueryBuilderInterface $ { $parameterBagReader = $request->getQueryParameterBag(); - $default = DateTimeImmutable::createFromFormat('U', (string) $request->getServerParam('REQUEST_TIME', 0)) - ->format(DATE_ATOM); + $default = DateTimeImmutable::createFromFormat('U', (string) $request->getServerParam('REQUEST_TIME', 0)); + if (!$default) { + throw new InvalidArgumentException('Invalid timestamp provided'); + } + $default = $default->format(DATE_ATOM); $availableFrom = $parameterBagReader->getDateTimeFromParameter('availableFrom', $default); $availableTo = $parameterBagReader->getDateTimeFromParameter('availableTo', $default); diff --git a/tests/ElasticSearch/Offer/ElasticSearchOfferQueryBuilderTest.php b/tests/ElasticSearch/Offer/ElasticSearchOfferQueryBuilderTest.php index 2fcf7d70..5974ad04 100644 --- a/tests/ElasticSearch/Offer/ElasticSearchOfferQueryBuilderTest.php +++ b/tests/ElasticSearch/Offer/ElasticSearchOfferQueryBuilderTest.php @@ -7,6 +7,7 @@ use CultuurNet\UDB3\Search\Address\PostalCode; use CultuurNet\UDB3\Search\Country; use CultuurNet\UDB3\Search\Creator; +use CultuurNet\UDB3\Search\DateTimeFactory; use CultuurNet\UDB3\Search\ElasticSearch\AbstractElasticSearchQueryBuilderTest; use CultuurNet\UDB3\Search\ElasticSearch\ElasticSearchDistance; use CultuurNet\UDB3\Search\ElasticSearch\LuceneQueryString; @@ -34,8 +35,6 @@ use CultuurNet\UDB3\Search\SortOrder; use CultuurNet\UDB3\Search\Start; use CultuurNet\UDB3\Search\UnsupportedParameterValue; -use DateTime; -use DateTimeImmutable; use InvalidArgumentException; final class ElasticSearchOfferQueryBuilderTest extends AbstractElasticSearchQueryBuilderTest @@ -437,8 +436,7 @@ public function it_should_build_a_query_with_a_date_range_filter_without_upper_b $builder = (new ElasticSearchOfferQueryBuilder()) ->withStartAndLimit(new Start(30), new Limit(10)) ->withDateRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'), - null + DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00') ); $expectedQueryArray = [ @@ -479,7 +477,7 @@ public function it_should_build_a_query_with_a_date_range_filter_without_lower_b ->withStartAndLimit(new Start(30), new Limit(10)) ->withDateRangeFilter( null, - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00') + DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00') ); $expectedQueryArray = [ @@ -519,8 +517,8 @@ public function it_should_build_a_query_with_a_complete_date_range_filter(): voi $builder = (new ElasticSearchOfferQueryBuilder()) ->withStartAndLimit(new Start(30), new Limit(10)) ->withDateRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'), - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00') + DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'), + DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00') ); $expectedQueryArray = [ @@ -562,8 +560,8 @@ public function it_should_build_query_with_a_complete_date_range_and_time_range_ ->withStartAndLimit(new Start(30), new Limit(10)) ->withSubEventFilter( (new SubEventQueryParameters()) - ->withDateFrom(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00')) - ->withDateTo(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00')) + ->withDateFrom(DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00')) + ->withDateTo(DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00')) ->withLocalTimeFrom(800) ->withLocalTimeTo(1600) ->withStatuses([Status::temporarilyUnavailable(), Status::unavailable()]) @@ -647,8 +645,8 @@ public function it_should_build_query_with_a_complete_date_range_and_time_range_ ->withStartAndLimit(new Start(30), new Limit(10)) ->withSubEventFilter( (new SubEventQueryParameters()) - ->withDateFrom(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00')) - ->withDateTo(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00')) + ->withDateFrom(DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00')) + ->withDateTo(DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00')) ->withLocalTimeFrom(800) ->withLocalTimeTo(1600) ); @@ -711,8 +709,8 @@ public function it_should_build_query_with_a_complete_date_range_filter_for_mult ->withStartAndLimit(new Start(30), new Limit(10)) ->withSubEventFilter( (new SubEventQueryParameters()) - ->withDateFrom(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00')) - ->withDateTo(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00')) + ->withDateFrom(DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00')) + ->withDateTo(DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00')) ->withStatuses([Status::temporarilyUnavailable(), Status::unavailable()]) ); @@ -1225,8 +1223,7 @@ public function it_should_build_a_query_with_an_available_range_filter_without_u $builder = (new ElasticSearchOfferQueryBuilder()) ->withStartAndLimit(new Start(30), new Limit(10)) ->withAvailableRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'), - null + DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'), ); $expectedQueryArray = [ @@ -1267,7 +1264,7 @@ public function it_should_build_a_query_with_an_available_range_filter_without_l ->withStartAndLimit(new Start(30), new Limit(10)) ->withAvailableRangeFilter( null, - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00') + DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00') ); $expectedQueryArray = [ @@ -1307,8 +1304,8 @@ public function it_should_build_a_query_with_a_complete_available_range_filter() $builder = (new ElasticSearchOfferQueryBuilder()) ->withStartAndLimit(new Start(30), new Limit(10)) ->withAvailableRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'), - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00') + DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'), + DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00') ); $expectedQueryArray = [ @@ -1353,8 +1350,8 @@ public function it_should_throw_an_exception_for_an_invalid_available_range(): v (new ElasticSearchOfferQueryBuilder()) ->withAvailableRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00'), - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00') + DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00'), + DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00') ); } @@ -2852,8 +2849,7 @@ public function it_should_build_a_query_with_a_created_range_filter_without_uppe $builder = (new ElasticSearchOfferQueryBuilder()) ->withStartAndLimit(new Start(30), new Limit(10)) ->withCreatedRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'), - null + DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'), ); $expectedQueryArray = [ @@ -2894,7 +2890,7 @@ public function it_should_build_a_query_with_a_created_range_filter_without_lowe ->withStartAndLimit(new Start(30), new Limit(10)) ->withCreatedRangeFilter( null, - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00') + DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00') ); $expectedQueryArray = [ @@ -2934,8 +2930,8 @@ public function it_should_build_a_query_with_a_complete_created_range_filter(): $builder = (new ElasticSearchOfferQueryBuilder()) ->withStartAndLimit(new Start(30), new Limit(10)) ->withCreatedRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'), - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00') + DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'), + DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00') ); $expectedQueryArray = [ @@ -2976,7 +2972,7 @@ public function it_should_build_a_query_with_a_modified_range_filter_without_upp $builder = (new ElasticSearchOfferQueryBuilder()) ->withStartAndLimit(new Start(30), new Limit(10)) ->withModifiedRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'), + DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'), null ); @@ -3018,7 +3014,7 @@ public function it_should_build_a_query_with_a_modified_range_filter_without_low ->withStartAndLimit(new Start(30), new Limit(10)) ->withModifiedRangeFilter( null, - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00') + DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00') ); $expectedQueryArray = [ @@ -3058,8 +3054,8 @@ public function it_should_build_a_query_with_a_complete_modified_range_filter(): $builder = (new ElasticSearchOfferQueryBuilder()) ->withStartAndLimit(new Start(30), new Limit(10)) ->withModifiedRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'), - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00') + DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'), + DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00') ); $expectedQueryArray = [ diff --git a/tests/Http/OfferSearchControllerTest.php b/tests/Http/OfferSearchControllerTest.php index 4525e6c7..705cac1a 100644 --- a/tests/Http/OfferSearchControllerTest.php +++ b/tests/Http/OfferSearchControllerTest.php @@ -4,6 +4,7 @@ namespace CultuurNet\UDB3\Search\Http; +use CultuurNet\UDB3\Search\DateTimeFactory; use InvalidArgumentException; use CultuurNet\UDB3\Search\Address\PostalCode; use CultuurNet\UDB3\Search\Country; @@ -52,8 +53,6 @@ use CultuurNet\UDB3\Search\Region\RegionId; use CultuurNet\UDB3\Search\SortOrder; use CultuurNet\UDB3\Search\Start; -use DateTime; -use DateTimeImmutable; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; @@ -249,8 +248,8 @@ public function it_returns_a_paged_collection_of_search_results_based_on_request new WorkflowStatus('DRAFT') ) ->withAvailableRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-26T00:00:00+01:00'), - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-28T15:30:23+01:00') + DateTimeFactory::fromAtom('2017-04-26T00:00:00+01:00'), + DateTimeFactory::fromAtom('2017-04-28T15:30:23+01:00') ) ->withRegionFilter( $this->regionIndexName, @@ -271,18 +270,18 @@ public function it_returns_a_paged_collection_of_search_results_based_on_request ->withUiTPASFilter(true) ->withCreatorFilter(new Creator('Jane Doe')) ->withCreatedRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T13:33:37+01:00'), - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T13:33:37+01:00') + DateTimeFactory::fromAtom('2017-05-01T13:33:37+01:00'), + DateTimeFactory::fromAtom('2017-05-01T13:33:37+01:00') ) ->withModifiedRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T13:33:37+01:00'), - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T13:33:37+01:00') + DateTimeFactory::fromAtom('2017-05-01T13:33:37+01:00'), + DateTimeFactory::fromAtom('2017-05-01T13:33:37+01:00') ) ->withCalendarTypeFilter(new CalendarType('single')) ->withSubEventFilter( (new SubEventQueryParameters()) - ->withDateFrom(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T00:00:00+01:00')) - ->withDateTo(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+01:00')) + ->withDateFrom(DateTimeFactory::fromAtom('2017-05-01T00:00:00+01:00')) + ->withDateTo(DateTimeFactory::fromAtom('2017-05-01T23:59:59+01:00')) ->withLocalTimeFrom(800) ->withLocalTimeTo(1600) ->withStatuses([Status::unavailable(), Status::temporarilyUnavailable()]) @@ -486,8 +485,8 @@ public function it_uses_default_parameters_when_default_filters_are_not_disabled $expectedQueryBuilder = $this->queryBuilder ->withWorkflowStatusFilter(new WorkflowStatus('APPROVED'), new WorkflowStatus('READY_FOR_VALIDATION')) ->withAvailableRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-26T08:34:21+00:00'), - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-26T08:34:21+00:00') + DateTimeFactory::fromAtom('2017-04-26T08:34:21+00:00'), + DateTimeFactory::fromAtom('2017-04-26T08:34:21+00:00') ) ->withAddressCountryFilter(new Country('BE')) ->withAudienceTypeFilter(new AudienceType('everyone')) @@ -576,12 +575,12 @@ public function it_can_convert_spaces_in_date_parameters_to_plus_signs(): void $expectedQueryBuilder = $this->queryBuilder ->withStartAndLimit(new Start(30), new Limit(10)) ->withAvailableRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-01T00:00:00+01:00'), - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-01T23:59:59+01:00') + DateTimeFactory::fromAtom('2017-04-01T00:00:00+01:00'), + DateTimeFactory::fromAtom('2017-04-01T23:59:59+01:00') ) ->withDateRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-01T00:00:00+01:00'), - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-01T23:59:59+01:00') + DateTimeFactory::fromAtom('2017-04-01T00:00:00+01:00'), + DateTimeFactory::fromAtom('2017-04-01T23:59:59+01:00') ); $expectedResultSet = new PagedResultSet(30, 0, []); @@ -1089,8 +1088,8 @@ public function it_uses_a_date_range_filter_only_if_no_status_or_time_from_or_ti $expectedQueryBuilder = $this->queryBuilder ->withDateRangeFilter( - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T00:00:00+01:00'), - DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+01:00') + DateTimeFactory::fromAtom('2017-05-01T00:00:00+01:00'), + DateTimeFactory::fromAtom('2017-05-01T23:59:59+01:00') ); $expectedResultSet = new PagedResultSet(30, 0, []); diff --git a/tests/Http/Parameters/ArrayParameterBagAdapterTest.php b/tests/Http/Parameters/ArrayParameterBagAdapterTest.php index 63c2039f..d6a72876 100644 --- a/tests/Http/Parameters/ArrayParameterBagAdapterTest.php +++ b/tests/Http/Parameters/ArrayParameterBagAdapterTest.php @@ -4,6 +4,7 @@ namespace CultuurNet\UDB3\Search\Http\Parameters; +use CultuurNet\UDB3\Search\DateTimeFactory; use CultuurNet\UDB3\Search\Label\LabelName; use CultuurNet\UDB3\Search\Offer\WorkflowStatus; use DateTime; @@ -431,7 +432,7 @@ public function it_should_parse_a_datetime_from_a_parameter(): void { $parameterBag = new ArrayParameterBagAdapter(['availableFrom' => '2017-04-26T12:20:05+01:00']); - $expected = DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-26T12:20:05+01:00'); + $expected = DateTimeFactory::fromAtom('2017-04-26T12:20:05+01:00'); $actual = $parameterBag->getDateTimeFromParameter('availableFrom'); $this->assertDateTimeEquals($expected, $actual); @@ -448,7 +449,7 @@ public function it_should_return_a_default_for_a_datetime_parameter_if_it_is_emp $parameterBag = new ArrayParameterBagAdapter([]); $default = '2017-04-26T12:20:05+01:00'; - $expected = DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-26T12:20:05+01:00'); + $expected = DateTimeFactory::fromAtom('2017-04-26T12:20:05+01:00'); $actual = $parameterBag->getDateTimeFromParameter('availableFrom', $default); $this->assertDateTimeEquals($expected, $actual);