|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace tests\specs\Karriere\JsonDecoder\Bindings; |
| 4 | + |
| 5 | +use DateTime; |
| 6 | +use Karriere\JsonDecoder\Bindings\DateTimeBinding; |
| 7 | +use Karriere\JsonDecoder\JsonDecoder; |
| 8 | +use Karriere\JsonDecoder\PropertyAccessor; |
| 9 | +use PhpSpec\ObjectBehavior; |
| 10 | +use Prophecy\Argument; |
| 11 | + |
| 12 | +class DateTimeBindingSpec extends ObjectBehavior |
| 13 | +{ |
| 14 | + public function let() |
| 15 | + { |
| 16 | + $this->beConstructedWith('property', 'field'); |
| 17 | + } |
| 18 | + |
| 19 | + public function it_is_initializable() |
| 20 | + { |
| 21 | + $this->shouldHaveType(DateTimeBinding::class); |
| 22 | + } |
| 23 | + |
| 24 | + public function it_should_return_the_property_name() |
| 25 | + { |
| 26 | + $this->property()->shouldReturn('property'); |
| 27 | + } |
| 28 | + |
| 29 | + public function it_should_succeed_on_binding_validation() |
| 30 | + { |
| 31 | + $this->validate(['field' => '2018-02-01T12:00:00+00:00'])->shouldReturn(true); |
| 32 | + } |
| 33 | + |
| 34 | + public function it_should_fail_on_binding_validation_for_a_required_property_with_invalid_format() |
| 35 | + { |
| 36 | + $this->beConstructedWith('property', 'field', true); |
| 37 | + $this->validate(['field' => 'value'])->shouldReturn(false); |
| 38 | + } |
| 39 | + |
| 40 | + public function it_should_fail_on_binding_validation_for_a_not_required_property_with_invalid_format() |
| 41 | + { |
| 42 | + $this->validate(['field' => 'value'])->shouldReturn(false); |
| 43 | + } |
| 44 | + |
| 45 | + public function it_should_be_able_to_parse_an_atom_datetime(JsonDecoder $jsonDecoder, PropertyAccessor $propertyAccessor) |
| 46 | + { |
| 47 | + $expectedDateTime = DateTime::createFromFormat(DateTime::ATOM, '2020-01-01T12:00:00+00:00'); |
| 48 | + $propertyAccessor->set($expectedDateTime)->shouldBeCalled(); |
| 49 | + |
| 50 | + $this->bind($jsonDecoder, ['field' => '2020-01-01T12:00:00+00:00'], $propertyAccessor); |
| 51 | + } |
| 52 | + |
| 53 | + public function it_should_not_set_a_value_for_an_empty_datetime_string(JsonDecoder $jsonDecoder, PropertyAccessor $propertyAccessor) |
| 54 | + { |
| 55 | + $propertyAccessor->set(Argument::any())->shouldNotBeCalled(); |
| 56 | + |
| 57 | + $this->bind($jsonDecoder, ['field' => ''], $propertyAccessor); |
| 58 | + } |
| 59 | + |
| 60 | + public function it_should_not_set_a_value_for_an_invalid_formatted_datetime_string(JsonDecoder $jsonDecoder, PropertyAccessor $propertyAccessor) |
| 61 | + { |
| 62 | + $propertyAccessor->set(Argument::any())->shouldNotBeCalled(); |
| 63 | + |
| 64 | + $this->bind($jsonDecoder, ['field' => 'invalid'], $propertyAccessor); |
| 65 | + } |
| 66 | +} |
0 commit comments