Skip to content

Commit

Permalink
Add support for immutable timestamps (#43)
Browse files Browse the repository at this point in the history
* Test if SEOData class can handle immutable timestamps

* Add CarbonInterface as typehint to allow both immutable and mutable objects

---------

Co-authored-by: Stan Daniëls <[email protected]>
  • Loading branch information
standaniels and standaniels authored Aug 12, 2023
1 parent 2788725 commit e1769c1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Schema/ArticleSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace RalphJSmit\Laravel\SEO\Schema;

use Illuminate\Support\Carbon;
use Carbon\CarbonInterface;
use Illuminate\Support\Collection;
use RalphJSmit\Laravel\SEO\Support\SEOData;

class ArticleSchema extends Schema
{
public array $authors = [];

public ?Carbon $datePublished = null;
public ?CarbonInterface $datePublished = null;

public ?Carbon $dateModified = null;
public ?CarbonInterface $dateModified = null;

public ?string $description = null;

Expand Down
6 changes: 3 additions & 3 deletions src/Support/SEOData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RalphJSmit\Laravel\SEO\Support;

use Carbon\Carbon;
use Carbon\CarbonInterface;
use Illuminate\Support\Str;
use RalphJSmit\Helpers\Laravel\Pipe\Pipeable;
use RalphJSmit\Laravel\SEO\SchemaCollection;
Expand All @@ -19,8 +19,8 @@ public function __construct(
public ?string $url = null,
public bool $enableTitleSuffix = true,
public ?ImageMeta $imageMeta = null,
public ?Carbon $published_time = null,
public ?Carbon $modified_time = null,
public ?CarbonInterface $published_time = null,
public ?CarbonInterface $modified_time = null,
public ?string $articleBody = null,
public ?string $section = null,
public ?array $tags = null,
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Models/SEOTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Date;
use RalphJSmit\Laravel\SEO\Models\SEO;
use RalphJSmit\Laravel\SEO\Support\SEOData;
use RalphJSmit\Laravel\SEO\Tests\Fixtures\Page;
Expand All @@ -22,6 +24,16 @@
expect($export)->toBeInstanceOf(SEOData::class);
});

it('can have immutable timestamps', function () {
Date::useClass(CarbonImmutable::class);

$seo = Page::create()->seo;

$export = $seo->prepareForUsage();

expect($export)->toBeInstanceOf(SEOData::class);
});

it('can add properties to a SEO model', function (string $property, string $input) {
$page = Page::create();

Expand Down

0 comments on commit e1769c1

Please sign in to comment.