Skip to content

Commit

Permalink
Merge pull request #57 from spatie/expectation-testing
Browse files Browse the repository at this point in the history
Add expectation testing
  • Loading branch information
rubenvanassche authored Aug 20, 2021
2 parents 08a829f + 0d2e753 commit 34e2a34
Show file tree
Hide file tree
Showing 24 changed files with 688 additions and 416 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to `icalendar-generator` will be documented in this file

## 2.3.0 - 2020-08-20

- add support for attachments on events
- add a new expectation testing mechanism for internal tests

## 2.2.2 - 2020-08-19

- fix date timezones on all day events (#55)
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ Event::create()
...
```

You can add an attachment as such:

```php
Event::create()
->attachment('https://spatie.be/logo.svg')
->attachment('https://spatie.be/feed.xml', 'application/json')
...
```

After creating your event, it should be added to a calendar. There are multiple options to do this:

``` php
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"larapack/dd" : "^1.0",
"phpunit/phpunit" : "^8.2",
"spatie/phpunit-snapshot-assertions" : "^4.2",
"vimeo/psalm" : "^4.3"
"vimeo/psalm" : "^4.3",
"ext-json" : "*"
},
"autoload" : {
"psr-4" : {
Expand Down
14 changes: 12 additions & 2 deletions src/ComponentPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ public function getProperties(): array
return $this->properties;
}

public function getProperty(string $name): Property
/**
* @param string $name
*
* @return Property[]|Property
* @throws \Exception
*/
public function getProperty(string $name)
{
$filteredProperties = array_filter(
$this->properties,
Expand All @@ -88,7 +94,11 @@ function (Property $property) use ($name) {
throw new Exception("Property `{$name}` does not exist in the payload");
}

return $properties[0];
if (count($properties) === 1) {
return $properties[0];
}

return $properties;
}

public function getSubComponents(): array
Expand Down
25 changes: 23 additions & 2 deletions src/Components/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class Event extends Component implements HasTimezones

private ?string $url = null;

/** @var array[] */
private array $attachments = [];

public static function create(string $name = null): Event
{
return new self($name);
Expand Down Expand Up @@ -309,6 +312,16 @@ public function url(string $url): Event
return $this;
}

public function attachment(string $url, ?string $mediaType = null): Event
{
$this->attachments[] = [
'url' => $url,
'type' => $mediaType,
];

return $this;
}

public function getTimezoneRangeCollection(): TimezoneRangeCollection
{
if ($this->withoutTimezone) {
Expand Down Expand Up @@ -382,12 +395,20 @@ private function resolveProperties(ComponentPayload $payload): self
->optional(
$this->url,
fn () => UriProperty::create('URL', $this->url)
)->multiple(
)
->multiple(
$this->recurrence_dates,
fn (DateTimeValue $dateTime) => self::dateTimePropertyWithSpecifiedType('RDATE', $dateTime)
)->multiple(
)
->multiple(
$this->excluded_recurrence_dates,
fn (DateTimeValue $dateTime) => self::dateTimePropertyWithSpecifiedType('EXDATE', $dateTime)
)
->multiple(
$this->attachments,
fn (array $attachment) => $attachment['type'] !== null
? UriProperty::create('ATTACH', $attachment['url'])->addParameter(Parameter::create('FMTTYPE', $attachment['type']))
: UriProperty::create('ATTACH', $attachment['url'])
);

return $this;
Expand Down
10 changes: 8 additions & 2 deletions tests/ComponentPayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public function an_optional_will_only_be_added_when_the_condition_is_true()
$payload->optional(false, fn () => TextProperty::create('text', 'Some text here'));
$payload->optional(true, fn () => TextProperty::create('text', 'Other text here'));

$this->assertPropertyEqualsInPayload('text', 'Other text here', $payload);
PayloadExpectation::create($payload)->expectPropertyValue(
'text',
'Other text here'
);
}

/** @test */
Expand All @@ -79,7 +82,10 @@ public function an_optional_will_only_be_added_when_it_has_a_value()
$payload->optional(null, fn () => TextProperty::create('text', 'Some text here'));
$payload->optional('something', fn () => TextProperty::create('text', 'Other text here'));

$this->assertPropertyEqualsInPayload('text', 'Other text here', $payload);
PayloadExpectation::create($payload)->expectPropertyValue(
'text',
'Other text here'
);
}

/** @test */
Expand Down
79 changes: 46 additions & 33 deletions tests/Components/AlertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use DateInterval;
use DateTime;
use Spatie\IcalendarGenerator\Components\Alert;
use Spatie\IcalendarGenerator\Tests\PayloadExpectation;
use Spatie\IcalendarGenerator\Tests\PropertyExpectation;
use Spatie\IcalendarGenerator\Tests\TestCase;

class AlertTest extends TestCase
Expand All @@ -16,14 +18,17 @@ public function it_can_create_an_alert_at_a_date()

$payload = (new Alert('It is time'))->triggerDate($trigger)->resolvePayload();

$this->assertEquals('VALARM', $payload->getType());
$this->assertCount(3, $payload->getProperties());

$this->assertPropertyEqualsInPayload('ACTION', 'DISPLAY', $payload);
$this->assertPropertyEqualsInPayload('DESCRIPTION', 'It is time', $payload);
$this->assertPropertyEqualsInPayload('TRIGGER', $trigger, $payload);
$this->assertParameterCountInProperty(1, $payload->getProperty('TRIGGER'));
$this->assertParameterEqualsInProperty('VALUE', 'DATE-TIME', $payload->getProperty('TRIGGER'));
PayloadExpectation::create($payload)
->expectType('VALARM')
->expectPropertyCount(3)
->expectPropertyValue('ACTION', 'DISPLAY')
->expectPropertyValue('DESCRIPTION', 'It is time')
->expectProperty('TRIGGER', function (PropertyExpectation $expectation) use ($trigger) {
$expectation
->expectValue($trigger)
->expectParameterCount(1)
->expectParameterValue('VALUE', 'DATE-TIME');
});
}

/** @test */
Expand All @@ -36,8 +41,9 @@ public function it_can_create_an_alert_without_timezone_at_a_date()
->triggerDate($trigger)
->resolvePayload();

$this->assertParameterCountInProperty(1, $payload->getProperty('TRIGGER'));
$this->assertParameterEqualsInProperty('VALUE', 'DATE-TIME', $payload->getProperty('TRIGGER'));
PropertyExpectation::create($payload, 'TRIGGER')
->expectParameterCount(1)
->expectParameterValue('VALUE', 'DATE-TIME');
}

/** @test */
Expand All @@ -49,12 +55,13 @@ public function it_can_create_an_alert_at_the_start_of_an_event()
->triggerAtStart($trigger)
->resolvePayload();

$this->assertEquals('VALARM', $payload->getType());
$this->assertCount(2, $payload->getProperties());

$this->assertPropertyEqualsInPayload('ACTION', 'DISPLAY', $payload);
$this->assertPropertyEqualsInPayload('TRIGGER', $trigger, $payload);
$this->assertParameterCountInProperty(0, $payload->getProperty('TRIGGER'));
PayloadExpectation::create($payload)
->expectType('VALARM')
->expectPropertyCount(2)
->expectPropertyValue('ACTION', 'DISPLAY')
->expectProperty('TRIGGER', function (PropertyExpectation $expectation) use ($trigger) {
$expectation->expectValue($trigger)->expectParameterCount(0);
});
}

/** @test */
Expand All @@ -66,13 +73,15 @@ public function it_can_create_an_alert_at_the_end_of_an_event()
->triggerAtEnd($trigger)
->resolvePayload();

$this->assertEquals('VALARM', $payload->getType());
$this->assertCount(2, $payload->getProperties());

$this->assertPropertyEqualsInPayload('ACTION', 'DISPLAY', $payload);
$this->assertPropertyEqualsInPayload('TRIGGER', $trigger, $payload);
$this->assertParameterCountInProperty(1, $payload->getProperty('TRIGGER'));
$this->assertParameterEqualsInProperty('RELATED', 'END', $payload->getProperty('TRIGGER'));
PayloadExpectation::create($payload)
->expectType('VALARM')
->expectPropertyCount(2)
->expectPropertyValue('ACTION', 'DISPLAY')
->expectProperty('TRIGGER', function (PropertyExpectation $expectation) use ($trigger) {
$expectation->expectValue($trigger)
->expectParameterCount(1)
->expectParameterValue('RELATED', 'END');
});
}

/** @test */
Expand All @@ -83,27 +92,31 @@ public function it_can_be_constructed_as_static_before_or_after()
$payload = Alert::minutesBeforeStart(5)->resolvePayload();
$interval->invert = 1;

$this->assertPropertyEqualsInPayload('TRIGGER', $interval, $payload);
$this->assertParameterCountInProperty(0, $payload->getProperty('TRIGGER'));
PropertyExpectation::create($payload, 'TRIGGER')
->expectValue($interval)
->expectParameterCount(0);

$payload = Alert::minutesAfterStart(5)->resolvePayload();
$interval->invert = 0;

$this->assertPropertyEqualsInPayload('TRIGGER', $interval, $payload);
$this->assertParameterCountInProperty(0, $payload->getProperty('TRIGGER'));
PropertyExpectation::create($payload, 'TRIGGER')
->expectValue($interval)
->expectParameterCount(0);

$payload = Alert::minutesBeforeEnd(5)->resolvePayload();
$interval->invert = 1;

$this->assertPropertyEqualsInPayload('TRIGGER', $interval, $payload);
$this->assertParameterCountInProperty(1, $payload->getProperty('TRIGGER'));
$this->assertParameterEqualsInProperty('RELATED', 'END', $payload->getProperty('TRIGGER'));
PropertyExpectation::create($payload, 'TRIGGER')
->expectValue($interval)
->expectParameterCount(1)
->expectParameterValue('RELATED', 'END');

$payload = Alert::minutesAfterEnd(5)->resolvePayload();
$interval->invert = 0;

$this->assertPropertyEqualsInPayload('TRIGGER', $interval, $payload);
$this->assertParameterCountInProperty(1, $payload->getProperty('TRIGGER'));
$this->assertParameterEqualsInProperty('RELATED', 'END', $payload->getProperty('TRIGGER'));
PropertyExpectation::create($payload, 'TRIGGER')
->expectValue($interval)
->expectParameterCount(1)
->expectParameterValue('RELATED', 'END');
}
}
Loading

0 comments on commit 34e2a34

Please sign in to comment.