From 83805bbc2aa9ffb565a78c3f4faf9cd410173b84 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 15 Sep 2020 12:20:32 +0200 Subject: [PATCH 1/5] allow to suppress URL suffix for structured properties --- src/StructuredProperties/Audio.php | 14 +------- src/StructuredProperties/Image.php | 14 +------- .../StructuredProperty.php | 23 +++++++++++++ src/StructuredProperties/Video.php | 14 +------- tests/GlobalTypesTest.php | 33 +++++++++++++++++-- ...website_tags_with_structured_image__1.html | 12 +++++++ ...tructured_image_without_url_suffix__1.html | 12 +++++++ 7 files changed, 81 insertions(+), 41 deletions(-) create mode 100644 src/StructuredProperties/StructuredProperty.php create mode 100644 tests/__snapshots__/GlobalTypesTest__it_can_generate_website_tags_with_structured_image__1.html create mode 100644 tests/__snapshots__/GlobalTypesTest__it_can_generate_website_tags_with_structured_image_without_url_suffix__1.html diff --git a/src/StructuredProperties/Audio.php b/src/StructuredProperties/Audio.php index 6b64ea4..2e16bbe 100644 --- a/src/StructuredProperties/Audio.php +++ b/src/StructuredProperties/Audio.php @@ -2,22 +2,10 @@ namespace Astrotomic\OpenGraph\StructuredProperties; -use Astrotomic\OpenGraph\BaseObject; - -class Audio extends BaseObject +class Audio extends StructuredProperty { protected const PREFIX = 'og:audio'; - public function __construct(string $url) - { - $this->setProperty(self::PREFIX, 'url', $url); - } - - public static function make(string $url) - { - return new static($url); - } - public function secureUrl(string $url) { $this->setProperty(self::PREFIX, 'secure_url', $url); diff --git a/src/StructuredProperties/Image.php b/src/StructuredProperties/Image.php index e2ab1e1..b2f86d9 100644 --- a/src/StructuredProperties/Image.php +++ b/src/StructuredProperties/Image.php @@ -2,22 +2,10 @@ namespace Astrotomic\OpenGraph\StructuredProperties; -use Astrotomic\OpenGraph\BaseObject; - -class Image extends BaseObject +class Image extends StructuredProperty { protected const PREFIX = 'og:image'; - public function __construct(string $url) - { - $this->setProperty(self::PREFIX, 'url', $url); - } - - public static function make(string $url) - { - return new static($url); - } - public function secureUrl(string $url) { $this->setProperty(self::PREFIX, 'secure_url', $url); diff --git a/src/StructuredProperties/StructuredProperty.php b/src/StructuredProperties/StructuredProperty.php new file mode 100644 index 0000000..6f16ab3 --- /dev/null +++ b/src/StructuredProperties/StructuredProperty.php @@ -0,0 +1,23 @@ +setProperty(static::PREFIX, 'url', $url); + } else { + $prefix = explode(':', static::PREFIX, 2); + $this->setProperty($prefix[0], $prefix[1], $url); + } + } + + public static function make(string $url, bool $withUrlSuffix = true) + { + return new static($url, $withUrlSuffix); + } +} diff --git a/src/StructuredProperties/Video.php b/src/StructuredProperties/Video.php index 9a5e7df..55405d0 100644 --- a/src/StructuredProperties/Video.php +++ b/src/StructuredProperties/Video.php @@ -2,22 +2,10 @@ namespace Astrotomic\OpenGraph\StructuredProperties; -use Astrotomic\OpenGraph\BaseObject; - -class Video extends BaseObject +class Video extends StructuredProperty { protected const PREFIX = 'og:video'; - public function __construct(string $url) - { - $this->setProperty(self::PREFIX, 'url', $url); - } - - public static function make(string $url) - { - return new static($url); - } - public function secureUrl(string $url) { $this->setProperty(self::PREFIX, 'secure_url', $url); diff --git a/tests/GlobalTypesTest.php b/tests/GlobalTypesTest.php index 80a2590..1bd64ea 100644 --- a/tests/GlobalTypesTest.php +++ b/tests/GlobalTypesTest.php @@ -1,6 +1,7 @@ group('global', 'website'); +it('can generate website tags with structured image', function () { + $og = Website::make('Title | Example') + ->url('http://www.example.com') + ->description('Description') + ->locale('en_US') + ->alternateLocale('en_GB') + ->siteName('Example') + ->image(Image::make('http://www.example.com/image1.jpg')->mimeType('image/jpeg')); + + assertMatchesHtmlSnapshot((string) $og); +})->group('global', 'website'); + +it('can generate website tags with structured image without url suffix', function () { + $og = Website::make('Title | Example') + ->url('http://www.example.com') + ->description('Description') + ->locale('en_US') + ->alternateLocale('en_GB') + ->siteName('Example') + ->image(Image::make('http://www.example.com/image1.jpg', false)->mimeType('image/jpeg')); + + assertMatchesHtmlSnapshot((string) $og); +})->group('global', 'website'); + it('can generate website tags with conditional callbacks', function () { $og = Website::make('Title | Example') ->url('http://www.example.com') ->description('Description') ->locale('en_US') - ->when(false, fn (Website $og) => $og->alternateLocale('de_DE')) - ->when(true, fn (Website $og) => $og->alternateLocale('en_GB')) + ->when(false, function (Website $og) { + $og->alternateLocale('de_DE'); + }) + ->when(true, function (Website $og) { + $og->alternateLocale('en_GB'); + }) ->siteName('Example') ->image('http://www.example.com/image1.jpg'); diff --git a/tests/__snapshots__/GlobalTypesTest__it_can_generate_website_tags_with_structured_image__1.html b/tests/__snapshots__/GlobalTypesTest__it_can_generate_website_tags_with_structured_image__1.html new file mode 100644 index 0000000..eed52c9 --- /dev/null +++ b/tests/__snapshots__/GlobalTypesTest__it_can_generate_website_tags_with_structured_image__1.html @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/tests/__snapshots__/GlobalTypesTest__it_can_generate_website_tags_with_structured_image_without_url_suffix__1.html b/tests/__snapshots__/GlobalTypesTest__it_can_generate_website_tags_with_structured_image_without_url_suffix__1.html new file mode 100644 index 0000000..51087eb --- /dev/null +++ b/tests/__snapshots__/GlobalTypesTest__it_can_generate_website_tags_with_structured_image_without_url_suffix__1.html @@ -0,0 +1,12 @@ + + + + + + + + + + + + From fcbe9cc9933a4efd20c1a1608ba08346435d5ee1 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 15 Sep 2020 12:25:41 +0200 Subject: [PATCH 2/5] only test PHP 7.4 and suggest to use PHP 7.4 --- .github/workflows/run-tests.yml | 2 +- composer.json | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index ca5f45b..2dd8873 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -15,7 +15,7 @@ jobs: strategy: fail-fast: true matrix: - php: [7.1, 7.2, 7.3, 7.4] + php: [7.4] dependency-version: [prefer-lowest, prefer-stable] name: P${{ matrix.php }} - ${{ matrix.dependency-version }} diff --git a/composer.json b/composer.json index 5a7e3a0..e4d5cdd 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,11 @@ "astrotomic/php-conditional-proxy": "^0.2.0" }, "require-dev": { - "pestphp/pest": "^0.1.5", - "spatie/pest-plugin-snapshots": "^0.1.0" + "pestphp/pest": "^0.3.0", + "spatie/pest-plugin-snapshots": "^0.3.0" + }, + "suggest": { + "php": "^7.4" }, "config": { "sort-packages": true From d5d6367653bb629fb647a3d3e488db945a184127 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 15 Sep 2020 12:28:08 +0200 Subject: [PATCH 3/5] fi namespaced function --- tests/GlobalTypesTest.php | 1 + tests/MusicTypesTest.php | 1 + tests/TwitterTypesTest.php | 1 + tests/VideoTypesTest.php | 1 + 4 files changed, 4 insertions(+) diff --git a/tests/GlobalTypesTest.php b/tests/GlobalTypesTest.php index 1bd64ea..fcfab4d 100644 --- a/tests/GlobalTypesTest.php +++ b/tests/GlobalTypesTest.php @@ -6,6 +6,7 @@ use Astrotomic\OpenGraph\Types\Book; use Astrotomic\OpenGraph\Types\Profile; use Astrotomic\OpenGraph\Types\Website; +use function Spatie\Snapshots\{assertMatchesHtmlSnapshot}; it('can generate website tags', function () { $og = Website::make('Title | Example') diff --git a/tests/MusicTypesTest.php b/tests/MusicTypesTest.php index 16b90c7..4119631 100644 --- a/tests/MusicTypesTest.php +++ b/tests/MusicTypesTest.php @@ -4,6 +4,7 @@ use Astrotomic\OpenGraph\Types\Music\Playlist; use Astrotomic\OpenGraph\Types\Music\RadioStation; use Astrotomic\OpenGraph\Types\Music\Song; +use function Spatie\Snapshots\{assertMatchesHtmlSnapshot}; it('can generate song tags', function () { $og = Song::make('Title | Example') diff --git a/tests/TwitterTypesTest.php b/tests/TwitterTypesTest.php index 1ae00d6..3521d8c 100644 --- a/tests/TwitterTypesTest.php +++ b/tests/TwitterTypesTest.php @@ -3,6 +3,7 @@ use Astrotomic\OpenGraph\Types\Twitter\Player; use Astrotomic\OpenGraph\Types\Twitter\Summary; use Astrotomic\OpenGraph\Types\Twitter\SummaryLargeImage; +use function Spatie\Snapshots\{assertMatchesHtmlSnapshot}; it('can generate summary tags', function () { $og = Summary::make('Title | Example') diff --git a/tests/VideoTypesTest.php b/tests/VideoTypesTest.php index 29c66f2..abdda3e 100644 --- a/tests/VideoTypesTest.php +++ b/tests/VideoTypesTest.php @@ -6,6 +6,7 @@ use Astrotomic\OpenGraph\Types\Video\Movie; use Astrotomic\OpenGraph\Types\Video\Other; use Astrotomic\OpenGraph\Types\Video\TvShow; +use function Spatie\Snapshots\{assertMatchesHtmlSnapshot}; it('can generate movie tags', function () { $og = Movie::make('Title | Example') From f6a4e1cdb36a80509778dc485a7d8f512842a582 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 15 Sep 2020 12:31:19 +0200 Subject: [PATCH 4/5] switch back to PHP7.4 short closures --- composer.json | 1 + tests/GlobalTypesTest.php | 8 ++------ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index e4d5cdd..3c67779 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "astrotomic/php-conditional-proxy": "^0.2.0" }, "require-dev": { + "php": "^7.4", "pestphp/pest": "^0.3.0", "spatie/pest-plugin-snapshots": "^0.3.0" }, diff --git a/tests/GlobalTypesTest.php b/tests/GlobalTypesTest.php index fcfab4d..7346ae7 100644 --- a/tests/GlobalTypesTest.php +++ b/tests/GlobalTypesTest.php @@ -66,12 +66,8 @@ public function __toString() ->url('http://www.example.com') ->description('Description') ->locale('en_US') - ->when(false, function (Website $og) { - $og->alternateLocale('de_DE'); - }) - ->when(true, function (Website $og) { - $og->alternateLocale('en_GB'); - }) + ->when(false, fn (Website $og) => $og->alternateLocale('de_DE')) + ->when(true, fn (Website $og) => $og->alternateLocale('en_GB')) ->siteName('Example') ->image('http://www.example.com/image1.jpg'); From 4dfd1a3309c49e527ff471ac3168935e18f49b25 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 15 Sep 2020 12:32:23 +0200 Subject: [PATCH 5/5] fix duplicated php dependency --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 3c67779..e4d5cdd 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,6 @@ "astrotomic/php-conditional-proxy": "^0.2.0" }, "require-dev": { - "php": "^7.4", "pestphp/pest": "^0.3.0", "spatie/pest-plugin-snapshots": "^0.3.0" },