From 48be030a003debecba6bee88637ccacf29194e75 Mon Sep 17 00:00:00 2001 From: Thor Brink Date: Fri, 28 Feb 2025 09:33:38 +0000 Subject: [PATCH] refactor: rename create method to transform in WpPostArgsFromSchemaObjectInterface and its implementations --- .../AddChecksum.php | 6 ++-- .../AddChecksum.test.php | 4 +-- .../DateDecorator.php | 4 +-- .../DateDecorator.test.php | 4 +-- .../Factory/Factory.php | 15 ++++++++++ .../Factory/FactoryInterface.php | 15 ++++++++++ .../IdDecorator.php | 4 +-- .../IdDecorator.test.php | 20 +++++++++++++ .../JobPostingDecorator.php | 4 +-- .../JobPostingDecorator.test.php | 4 +-- .../MetaPropertyValueDecorator.php | 4 +-- .../MetaPropertyValueDecorator.test.php | 6 ++-- .../OriginIdDecorator.php | 4 +-- .../OriginIdDecorator.test.php | 4 +-- .../PostTypeDecorator.php | 4 +-- .../PostTypeDecorator.test.php | 8 ++---- .../SchemaDataDecorator.php | 4 +-- .../SchemaDataDecorator.test.php | 9 +++--- .../SourceIdDecorator.php | 4 +-- .../SourceIdDecorator.test.php | 7 ++--- .../TermsDecorator.php | 4 +-- .../TermsDecorator.test.php | 28 +++++++++---------- .../ThumbnailDecorator.php | 4 +-- .../ThumbnailDecorator.test.php | 22 +++++++-------- .../VerifyChecksum.php | 4 +-- .../VerifyChecksum.test.php | 16 +++++------ ...ory.php => WpPostArgsFromSchemaObject.php} | 6 ++-- ...hp => WpPostArgsFromSchemaObject.test.php} | 12 ++++---- .../WpPostArgsFromSchemaObjectInterface.php | 2 +- 29 files changed, 136 insertions(+), 96 deletions(-) create mode 100644 library/ExternalContent/WpPostArgsFromSchemaObject/Factory/Factory.php create mode 100644 library/ExternalContent/WpPostArgsFromSchemaObject/Factory/FactoryInterface.php create mode 100644 library/ExternalContent/WpPostArgsFromSchemaObject/IdDecorator.test.php rename library/ExternalContent/WpPostArgsFromSchemaObject/{WpPostFactory.php => WpPostArgsFromSchemaObject.php} (82%) rename library/ExternalContent/WpPostArgsFromSchemaObject/{WpPostFactory.test.php => WpPostArgsFromSchemaObject.test.php} (73%) diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/AddChecksum.php b/library/ExternalContent/WpPostArgsFromSchemaObject/AddChecksum.php index 829a97742..14c807fe1 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/AddChecksum.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/AddChecksum.php @@ -21,10 +21,10 @@ public function __construct(private WpPostArgsFromSchemaObjectInterface $inner) /** * @inheritDoc */ - public function create(BaseType $schemaObject): array + public function transform(BaseType $schemaObject): array { - $postArgs = $this->inner->create($schemaObject); - $checksum = md5(json_encode($this->inner->create($schemaObject))); + $postArgs = $this->inner->transform($schemaObject); + $checksum = md5(json_encode($this->inner->transform($schemaObject))); if (!isset($postArgs['meta_input'])) { $postArgs['meta_input'] = []; diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/AddChecksum.test.php b/library/ExternalContent/WpPostArgsFromSchemaObject/AddChecksum.test.php index 3e94163f0..ecaa2afdc 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/AddChecksum.test.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/AddChecksum.test.php @@ -15,7 +15,7 @@ public function testAppliesChecksum() $schemaObject = new Thing(); $factory = new AddChecksum($this->getInnerFactory()); - $result = $factory->create($schemaObject); + $result = $factory->transform($schemaObject); $this->assertEquals('8f11aed0a9fa79ac707b8a4846a74f27', $result['meta_input']['checksum']); } @@ -23,7 +23,7 @@ public function testAppliesChecksum() private function getInnerFactory(): WpPostArgsFromSchemaObjectInterface { return new class implements WpPostArgsFromSchemaObjectInterface { - public function create(\Spatie\SchemaOrg\BaseType $schemaObject): array + public function transform(\Spatie\SchemaOrg\BaseType $schemaObject): array { return [ 'post_title' => '', diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/DateDecorator.php b/library/ExternalContent/WpPostArgsFromSchemaObject/DateDecorator.php index 0134e2308..0692fecc0 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/DateDecorator.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/DateDecorator.php @@ -10,10 +10,10 @@ public function __construct(private WpPostArgsFromSchemaObjectInterface $inner) { } - public function create(BaseType $schemaObject): array + public function transform(BaseType $schemaObject): array { return array_merge( - $this->inner->create($schemaObject), + $this->inner->transform($schemaObject), [ 'post_date' => $schemaObject['datePublished'] ?? null, 'post_modified' => $schemaObject['dateModified'] ?? null, diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/DateDecorator.test.php b/library/ExternalContent/WpPostArgsFromSchemaObject/DateDecorator.test.php index b604bc3f1..4caa4e93f 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/DateDecorator.test.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/DateDecorator.test.php @@ -19,10 +19,10 @@ public function testCreate() ]); $inner = $this->createMock(WpPostArgsFromSchemaObjectInterface::class); - $inner->method('create')->willReturn([]); + $inner->method('transform')->willReturn([]); $wpPostFactory = new DateDecorator($inner); - $wpPost = $wpPostFactory->create($schemaObject, new Source('', '')); + $wpPost = $wpPostFactory->transform($schemaObject, new Source('', '')); $this->assertEquals('2021-01-01', $wpPost['post_date']); $this->assertEquals('2021-01-02', $wpPost['post_modified']); diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/Factory/Factory.php b/library/ExternalContent/WpPostArgsFromSchemaObject/Factory/Factory.php new file mode 100644 index 000000000..22b13ba76 --- /dev/null +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/Factory/Factory.php @@ -0,0 +1,15 @@ +inner->create($schemaObject); + $post = $this->inner->transform($schemaObject); if (!empty($schemaObject['@id'])) { $postWithSameOriginId = $this->wpService->getPosts([ diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/IdDecorator.test.php b/library/ExternalContent/WpPostArgsFromSchemaObject/IdDecorator.test.php new file mode 100644 index 000000000..c8fe81999 --- /dev/null +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/IdDecorator.test.php @@ -0,0 +1,20 @@ +assertInstanceOf(IdDecorator::class, $factory); + } +} diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/JobPostingDecorator.php b/library/ExternalContent/WpPostArgsFromSchemaObject/JobPostingDecorator.php index 79b367ff1..55c918753 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/JobPostingDecorator.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/JobPostingDecorator.php @@ -22,9 +22,9 @@ public function __construct(private WpPostArgsFromSchemaObjectInterface $inner) /** * @inheritDoc */ - public function create(BaseType $schemaObject): array + public function transform(BaseType $schemaObject): array { - $post = $this->inner->create($schemaObject); + $post = $this->inner->transform($schemaObject); if ($schemaObject instanceof JobPosting) { $post = $this->applyPropertiesFromJobPosting($post, $schemaObject); diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/JobPostingDecorator.test.php b/library/ExternalContent/WpPostArgsFromSchemaObject/JobPostingDecorator.test.php index 48cbc3ff2..c02e7da43 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/JobPostingDecorator.test.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/JobPostingDecorator.test.php @@ -13,11 +13,11 @@ class JobPostingDecoratorTest extends TestCase */ public function testSetsTitleFromNameIfTitleIsMissing() { - $factory = new JobPostingDecorator(new WpPostFactory()); + $factory = new JobPostingDecorator(new WpPostArgsFromSchemaObject()); $schemaObject = new JobPosting(); $schemaObject->title('Job title'); - $post = $factory->create($schemaObject, new Source('', '')); + $post = $factory->transform($schemaObject, new Source('', '')); $this->assertEquals('Job title', $post['post_title']); } diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/MetaPropertyValueDecorator.php b/library/ExternalContent/WpPostArgsFromSchemaObject/MetaPropertyValueDecorator.php index 3d0800840..0199dcfc4 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/MetaPropertyValueDecorator.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/MetaPropertyValueDecorator.php @@ -21,13 +21,13 @@ public function __construct(private ?WpPostArgsFromSchemaObjectInterface $inner /** * @inheritDoc */ - public function create(BaseType $schemaObject): array + public function transform(BaseType $schemaObject): array { $postArgs = []; $propertyName = '@meta'; if ($this->inner !== null) { - $postArgs = $this->inner->create($schemaObject); + $postArgs = $this->inner->transform($schemaObject); } if ($schemaObject->hasProperty($propertyName) && is_array($schemaObject->getProperty($propertyName))) { diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/MetaPropertyValueDecorator.test.php b/library/ExternalContent/WpPostArgsFromSchemaObject/MetaPropertyValueDecorator.test.php index f1bc4998c..09da7d1fc 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/MetaPropertyValueDecorator.test.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/MetaPropertyValueDecorator.test.php @@ -18,7 +18,7 @@ public function testCreate() $schemaObject->setProperty('@meta', [Schema::propertyValue()->name('foo')->value('bar')]); $factory = new MetaPropertyValueDecorator(); - $postArgs = $factory->create($schemaObject, new Source('', '')); + $postArgs = $factory->transform($schemaObject, new Source('', '')); $this->assertEquals('bar', $postArgs['meta_input']['foo']); } @@ -35,7 +35,7 @@ public function testCreateEmptyName() ]); $factory = new MetaPropertyValueDecorator(); - $postArgs = $factory->create($schemaObject, new Source('', '')); + $postArgs = $factory->transform($schemaObject, new Source('', '')); $this->assertEmpty($postArgs['meta_input']); } @@ -52,7 +52,7 @@ public function testCreateNonPropertyValue() ]); $factory = new MetaPropertyValueDecorator(); - $postArgs = $factory->create($schemaObject, new Source('', '')); + $postArgs = $factory->transform($schemaObject, new Source('', '')); $this->assertEmpty($postArgs['meta_input']); } diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/OriginIdDecorator.php b/library/ExternalContent/WpPostArgsFromSchemaObject/OriginIdDecorator.php index db51e3f12..cefef7d61 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/OriginIdDecorator.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/OriginIdDecorator.php @@ -11,9 +11,9 @@ public function __construct(private WpPostArgsFromSchemaObjectInterface $inner) { } - public function create(BaseType $schemaObject): array + public function transform(BaseType $schemaObject): array { - $post = $this->inner->create($schemaObject); + $post = $this->inner->transform($schemaObject); $post['meta_input']['originId'] = $schemaObject['@id']; return $post; diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/OriginIdDecorator.test.php b/library/ExternalContent/WpPostArgsFromSchemaObject/OriginIdDecorator.test.php index 8c43d2fa5..83d3a4d4e 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/OriginIdDecorator.test.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/OriginIdDecorator.test.php @@ -15,9 +15,9 @@ public function testCreate() { $schemaObject = $this->getSchemaObject(); $schemaObject->setProperty('@id', 'foo'); - $factory = new OriginIdDecorator(new WpPostFactory()); + $factory = new OriginIdDecorator(new WpPostArgsFromSchemaObject()); - $result = $factory->create($schemaObject, new Source('', '')); + $result = $factory->transform($schemaObject, new Source('', '')); $this->assertEquals('foo', $result['meta_input']['originId']); } diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/PostTypeDecorator.php b/library/ExternalContent/WpPostArgsFromSchemaObject/PostTypeDecorator.php index 2ded62371..2d7db5b92 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/PostTypeDecorator.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/PostTypeDecorator.php @@ -23,8 +23,8 @@ public function __construct(private string $postType, private WpPostArgsFromSche /** * @inheritDoc */ - public function create(BaseType $schemaObject): array + public function transform(BaseType $schemaObject): array { - return [...$this->inner->create($schemaObject), 'post_type' => $this->postType]; + return [...$this->inner->transform($schemaObject), 'post_type' => $this->postType]; } } diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/PostTypeDecorator.test.php b/library/ExternalContent/WpPostArgsFromSchemaObject/PostTypeDecorator.test.php index 4798fcb36..466762f7a 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/PostTypeDecorator.test.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/PostTypeDecorator.test.php @@ -2,9 +2,7 @@ namespace Municipio\ExternalContent\WpPostArgsFromSchemaObject; -use Municipio\ExternalContent\Sources\Source; use PHPUnit\Framework\TestCase; -use Spatie\SchemaOrg\BaseType; use Spatie\SchemaOrg\Schema; class PostTypeDecoratorTest extends TestCase @@ -14,7 +12,7 @@ class PostTypeDecoratorTest extends TestCase */ public function testCanBeInstantiated() { - $factory = new PostTypeDecorator('test_post_type', new WpPostFactory()); + $factory = new PostTypeDecorator('test_post_type', new WpPostArgsFromSchemaObject()); $this->assertInstanceOf(PostTypeDecorator::class, $factory); } @@ -24,8 +22,8 @@ public function testCanBeInstantiated() public function testCreate() { $schemaObject = Schema::thing(); - $factory = new PostTypeDecorator('test_post_type', new WpPostFactory()); - $postArgs = $factory->create($schemaObject); + $factory = new PostTypeDecorator('test_post_type', new WpPostArgsFromSchemaObject()); + $postArgs = $factory->transform($schemaObject); $this->assertEquals('test_post_type', $postArgs['post_type']); } diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/SchemaDataDecorator.php b/library/ExternalContent/WpPostArgsFromSchemaObject/SchemaDataDecorator.php index 67940470a..b7c1bf13c 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/SchemaDataDecorator.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/SchemaDataDecorator.php @@ -24,9 +24,9 @@ public function __construct(private WpPostArgsFromSchemaObjectInterface $inner) /** * @inheritDoc */ - public function create(BaseType $schemaObject): array + public function transform(BaseType $schemaObject): array { - $post = $this->inner->create($schemaObject); + $post = $this->inner->transform($schemaObject); $post['meta_input']['schemaData'] = $schemaObject->toArray(); if (isset($post['meta_input']['schemaData']['id'])) { diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/SchemaDataDecorator.test.php b/library/ExternalContent/WpPostArgsFromSchemaObject/SchemaDataDecorator.test.php index 5faceebe2..5c6ff5114 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/SchemaDataDecorator.test.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/SchemaDataDecorator.test.php @@ -6,7 +6,6 @@ use PHPUnit\Framework\TestCase; use Spatie\SchemaOrg\BaseType; use Spatie\SchemaOrg\Schema; -use Spatie\SchemaOrg\Thing; class SchemaDataDecoratorTest extends TestCase { @@ -16,8 +15,8 @@ class SchemaDataDecoratorTest extends TestCase public function testCreate() { $schemaObject = Schema::thing()->setProperty('foo', 'bar'); - $factory = new SchemaDataDecorator(new WpPostFactory()); - $result = $factory->create($schemaObject, new Source('', '')); + $factory = new SchemaDataDecorator(new WpPostArgsFromSchemaObject()); + $result = $factory->transform($schemaObject, new Source('', '')); $this->assertEquals($schemaObject->toArray(), $result['meta_input']['schemaData']); } @@ -28,8 +27,8 @@ public function testCreate() public function testCreateWithId() { $schemaObject = Schema::thing()->setProperty('foo', 'bar')->setProperty('id', '123'); - $factory = new SchemaDataDecorator(new WpPostFactory()); - $result = $factory->create($schemaObject, new Source('', '')); + $factory = new SchemaDataDecorator(new WpPostArgsFromSchemaObject()); + $result = $factory->transform($schemaObject, new Source('', '')); $this->assertArrayNotHasKey('id', $result['meta_input']['schemaData']); } diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/SourceIdDecorator.php b/library/ExternalContent/WpPostArgsFromSchemaObject/SourceIdDecorator.php index af17a74a4..f30fae19c 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/SourceIdDecorator.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/SourceIdDecorator.php @@ -10,9 +10,9 @@ public function __construct(private string $sourceId, private WpPostArgsFromSche { } - public function create(BaseType $schemaObject): array + public function transform(BaseType $schemaObject): array { - $post = $this->inner->create($schemaObject); + $post = $this->inner->transform($schemaObject); $post['meta_input']['sourceId'] = $this->sourceId; return $post; diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/SourceIdDecorator.test.php b/library/ExternalContent/WpPostArgsFromSchemaObject/SourceIdDecorator.test.php index 96902ed4d..fd3b9208f 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/SourceIdDecorator.test.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/SourceIdDecorator.test.php @@ -2,11 +2,8 @@ namespace Municipio\ExternalContent\WpPostArgsFromSchemaObject; -use Municipio\ExternalContent\Sources\SourceInterface; use PHPUnit\Framework\TestCase; use Spatie\SchemaOrg\BaseType; -use Spatie\SchemaOrg\Thing; -use WP_Query; class SourceIdDecoratorTest extends TestCase { @@ -16,9 +13,9 @@ class SourceIdDecoratorTest extends TestCase public function testAppliesSourceId() { $schemaObject = $this->getSchemaObject(); - $factory = new SourceIdDecorator('foo', new WpPostFactory()); + $factory = new SourceIdDecorator('foo', new WpPostArgsFromSchemaObject()); - $result = $factory->create($schemaObject); + $result = $factory->transform($schemaObject); $this->assertEquals('foo', $result['meta_input']['sourceId']); } diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/TermsDecorator.php b/library/ExternalContent/WpPostArgsFromSchemaObject/TermsDecorator.php index e7457ad6a..6eb93c86e 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/TermsDecorator.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/TermsDecorator.php @@ -32,9 +32,9 @@ public function __construct( /** * @inheritDoc */ - public function create(BaseType $schemaObject): array + public function transform(BaseType $schemaObject): array { - $post = $this->inner->create($schemaObject); + $post = $this->inner->transform($schemaObject); $matchingTaxonomyItems = $this->tryGetMatchingTaxonomyItems($schemaObject); if (!isset($post['tax_input'])) { diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/TermsDecorator.test.php b/library/ExternalContent/WpPostArgsFromSchemaObject/TermsDecorator.test.php index b9a370d39..94ad1a350 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/TermsDecorator.test.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/TermsDecorator.test.php @@ -29,10 +29,10 @@ public function testAddsTermsToPost(): void [$this->getTaxonomyItem()], $this->getWpTermFactory(), $wpService, - new WpPostFactory() + new WpPostArgsFromSchemaObject() ); - $postData = $termsDecorator->create($schemaObject, $this->getSource()); + $postData = $termsDecorator->transform($schemaObject, $this->getSource()); $this->assertEquals([1], $postData['tax_input']['test_taxonomy']); } @@ -49,10 +49,10 @@ public function testAddsExistingTerms(): void [$this->getTaxonomyItem()], $this->getWpTermFactory(), $wpService, - new WpPostFactory() + new WpPostArgsFromSchemaObject() ); - $postData = $termsDecorator->create($schemaObject, $this->getSource()); + $postData = $termsDecorator->transform($schemaObject, $this->getSource()); $this->assertEquals([3], $postData['tax_input']['test_taxonomy']); } @@ -68,8 +68,8 @@ public function testCanCreateTermsFromSchemaPropertyThatContainsOtherSchemaTypes $termFactory = $this->getWpTermFactory(); $taxonomyItem = $this->getTaxonomyItem('Event', 'actor', 'test_taxonomy'); - $termsDecorator = new TermsDecorator([$taxonomyItem], $termFactory, $wpService, new WpPostFactory()); - $termsDecorator->create($schemaObject, $this->getSource()); + $termsDecorator = new TermsDecorator([$taxonomyItem], $termFactory, $wpService, new WpPostArgsFromSchemaObject()); + $termsDecorator->transform($schemaObject, $this->getSource()); $this->assertEquals('testPerson', $termFactory->calls[0][0]); } @@ -85,8 +85,8 @@ public function testCanCreateTermsFromNestedSchemaProperty(): void $termFactory = $this->getWpTermFactory(); $taxonomyItem = $this->getTaxonomyItem('Event', 'actor.callSign', 'test_taxonomy'); - $termsDecorator = new TermsDecorator([$taxonomyItem], $termFactory, $wpService, new WpPostFactory()); - $termsDecorator->create($schemaObject, $this->getSource()); + $termsDecorator = new TermsDecorator([$taxonomyItem], $termFactory, $wpService, new WpPostArgsFromSchemaObject()); + $termsDecorator->transform($schemaObject, $this->getSource()); $this->assertEquals('The Joker', $termFactory->calls[0][0]); } @@ -102,8 +102,8 @@ public function testCanCreateTermsFromMetaPropertyValueArray(): void $termFactory = $this->getWpTermFactory(); $taxonomyItem = $this->getTaxonomyItem('Event', '@meta.illness', 'test_taxonomy'); - $termsDecorator = new TermsDecorator([$taxonomyItem], $termFactory, $wpService, new WpPostFactory()); - $termsDecorator->create($schemaObject, $this->getSource()); + $termsDecorator = new TermsDecorator([$taxonomyItem], $termFactory, $wpService, new WpPostArgsFromSchemaObject()); + $termsDecorator->transform($schemaObject, $this->getSource()); $this->assertEquals('Mental', $termFactory->calls[0][0]); } @@ -119,8 +119,8 @@ public function testCanCreateTermsFromMetaPropertyValue(): void $termFactory = $this->getWpTermFactory(); $taxonomyItem = $this->getTaxonomyItem('Event', '@meta.illness', 'test_taxonomy'); - $termsDecorator = new TermsDecorator([$taxonomyItem], $termFactory, $wpService, new WpPostFactory()); - $termsDecorator->create($schemaObject, $this->getSource()); + $termsDecorator = new TermsDecorator([$taxonomyItem], $termFactory, $wpService, new WpPostArgsFromSchemaObject()); + $termsDecorator->transform($schemaObject, $this->getSource()); $this->assertEquals('Mental', $termFactory->calls[0][0]); } @@ -140,8 +140,8 @@ public function testDoesNotCreateFromPropertyValueIfNameIsNotThePropertyName(): $termFactory = $this->getWpTermFactory(); $taxonomyItem = $this->getTaxonomyItem('Event', '@meta.foo', 'test_taxonomy'); - $termsDecorator = new TermsDecorator([$taxonomyItem], $termFactory, $wpService, new WpPostFactory()); - $termsDecorator->create($schemaObject, $this->getSource()); + $termsDecorator = new TermsDecorator([$taxonomyItem], $termFactory, $wpService, new WpPostArgsFromSchemaObject()); + $termsDecorator->transform($schemaObject, $this->getSource()); $this->assertCount(1, $termFactory->calls); $this->assertEquals('Bar', $termFactory->calls[0][0]); diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/ThumbnailDecorator.php b/library/ExternalContent/WpPostArgsFromSchemaObject/ThumbnailDecorator.php index 0f73673a6..3671bb4e0 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/ThumbnailDecorator.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/ThumbnailDecorator.php @@ -27,9 +27,9 @@ public function __construct( /** * @inheritDoc */ - public function create(BaseType $schemaObject): array + public function transform(BaseType $schemaObject): array { - $post = $this->inner->create($schemaObject); + $post = $this->inner->transform($schemaObject); $imageUrl = $this->getImageUrl($schemaObject); if (!empty($imageUrl)) { diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/ThumbnailDecorator.test.php b/library/ExternalContent/WpPostArgsFromSchemaObject/ThumbnailDecorator.test.php index 0cd054c51..c2b3581ee 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/ThumbnailDecorator.test.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/ThumbnailDecorator.test.php @@ -2,8 +2,6 @@ namespace Municipio\ExternalContent\WpPostArgsFromSchemaObject; -use Municipio\ExternalContent\Sources\Source; -use Municipio\ExternalContent\WpPostArgsFromSchemaObject\WpPostFactory; use Municipio\TestUtils\WpMockFactory; use PHPUnit\Framework\TestCase; use Spatie\SchemaOrg\BaseType; @@ -21,9 +19,9 @@ public function testSideloadImageFromUrl(): void $wpService = new FakeWpService(['mediaSideloadImage' => 1, 'getPosts' => [], 'updatePostMeta' => true]); $schemaObject = $this->getSchemaObject(); $schemaObject->setProperty('image', $url); - $decorator = new ThumbnailDecorator(new WpPostFactory('test_post_type'), $wpService); + $decorator = new ThumbnailDecorator(new WpPostArgsFromSchemaObject('test_post_type'), $wpService); - $post = $decorator->create($schemaObject); + $post = $decorator->transform($schemaObject); $this->assertEquals($url, $wpService->methodCalls['mediaSideloadImage'][0][0]); $this->assertEquals(1, $post['meta_input']['_thumbnail_id']); @@ -40,9 +38,9 @@ public function testSideloadImageFromImageObject(): void $wpService = new FakeWpService(['mediaSideloadImage' => 1, 'getPosts' => [], 'updatePostMeta' => true]); $schemaObject = $this->getSchemaObject(); $schemaObject->setProperty('image', $imageProperty); - $decorator = new ThumbnailDecorator(new WpPostFactory('test_post_type'), $wpService); + $decorator = new ThumbnailDecorator(new WpPostArgsFromSchemaObject('test_post_type'), $wpService); - $post = $decorator->create($schemaObject); + $post = $decorator->transform($schemaObject); $this->assertEquals($url, $wpService->methodCalls['mediaSideloadImage'][0][0]); $this->assertEquals(1, $post['meta_input']['_thumbnail_id']); @@ -57,9 +55,9 @@ public function testUsesAlreadyUploadedImageIfFound(): void $wpService = new FakeWpService(['getPosts' => [(object)['ID' => 123]]]); $schemaObject = $this->getSchemaObject(); $schemaObject->setProperty('image', $url); - $decorator = new ThumbnailDecorator(new WpPostFactory('test_post_type'), $wpService); + $decorator = new ThumbnailDecorator(new WpPostArgsFromSchemaObject('test_post_type'), $wpService); - $post = $decorator->create($schemaObject); + $post = $decorator->transform($schemaObject); $this->assertArrayNotHasKey('mediaSideloadImage', $wpService->methodCalls); $this->assertEquals(123, $post['meta_input']['_thumbnail_id']); @@ -75,9 +73,9 @@ public function testDoesNotConnectToPostIfSideloadFails(): void $schemaObject = $this->getSchemaObject(); $schemaObject->setProperty('image', $url); - $decorator = new ThumbnailDecorator(new WpPostFactory('test_post_type'), $wpService); + $decorator = new ThumbnailDecorator(new WpPostArgsFromSchemaObject('test_post_type'), $wpService); - $post = $decorator->create($schemaObject); + $post = $decorator->transform($schemaObject); $this->assertArrayNotHasKey('_thumbnail_id', $post['meta_input']); } @@ -89,11 +87,11 @@ public function testSetsMetaForAttachment(): void { $url = 'https://example.com/image.jpg'; $wpService = new FakeWpService(['mediaSideloadImage' => 1, 'getPosts' => [], 'updatePostMeta' => true]); - $decorator = new ThumbnailDecorator(new WpPostFactory('test_post_type'), $wpService); + $decorator = new ThumbnailDecorator(new WpPostArgsFromSchemaObject('test_post_type'), $wpService); $schemaObject = $this->getSchemaObject(); $schemaObject->setProperty('image', $url); - $decorator->create($schemaObject); + $decorator->transform($schemaObject); $this->assertEquals(1, $wpService->methodCalls['updatePostMeta'][0][0]); $this->assertEquals('synced_from_external_source', $wpService->methodCalls['updatePostMeta'][0][1]); diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/VerifyChecksum.php b/library/ExternalContent/WpPostArgsFromSchemaObject/VerifyChecksum.php index 6aee16233..7746fc1db 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/VerifyChecksum.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/VerifyChecksum.php @@ -27,9 +27,9 @@ public function __construct( /** * @inheritDoc */ - public function create(BaseType $schemaObject): array + public function transform(BaseType $schemaObject): array { - $postArgs = $this->inner->create($schemaObject); + $postArgs = $this->inner->transform($schemaObject); if (!isset($postArgs['meta_input']['checksum']) || !isset($postArgs['ID'])) { return $postArgs; diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/VerifyChecksum.test.php b/library/ExternalContent/WpPostArgsFromSchemaObject/VerifyChecksum.test.php index ae6a05836..f9f6abc0f 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/VerifyChecksum.test.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/VerifyChecksum.test.php @@ -19,10 +19,10 @@ public function testCreate() * @var WpPostArgsFromSchemaObjectInterface|\PHPUnit\Framework\MockObject\MockObject $inner */ $inner = $this->createMock(WpPostArgsFromSchemaObjectInterface::class); - $inner->method('create')->willReturn(['ID' => 123, 'meta_input' => ['checksum' => '321']]); + $inner->method('transform')->willReturn(['ID' => 123, 'meta_input' => ['checksum' => '321']]); $verifyChecksum = new VerifyChecksum($inner, new FakeWpService(['getPostMeta' => '321'])); - $postArgs = $verifyChecksum->create(new Thing(), $this->getSourceMock()); + $postArgs = $verifyChecksum->transform(new Thing(), $this->getSourceMock()); $this->assertEquals(-1, $postArgs['ID']); } @@ -36,10 +36,10 @@ public function testCreateDifferentChecksum() * @var WpPostArgsFromSchemaObjectInterface|\PHPUnit\Framework\MockObject\MockObject $inner */ $inner = $this->createMock(WpPostArgsFromSchemaObjectInterface::class); - $inner->method('create')->willReturn(['ID' => 123, 'meta_input' => ['checksum' => '321']]); + $inner->method('transform')->willReturn(['ID' => 123, 'meta_input' => ['checksum' => '321']]); $verifyChecksum = new VerifyChecksum($inner, new FakeWpService(['getPostMeta' => '123'])); - $postArgs = $verifyChecksum->create(new Thing(), $this->getSourceMock()); + $postArgs = $verifyChecksum->transform(new Thing(), $this->getSourceMock()); $this->assertEquals(123, $postArgs['ID']); } @@ -53,10 +53,10 @@ public function testCreateNoChecksum() * @var WpPostArgsFromSchemaObjectInterface|\PHPUnit\Framework\MockObject\MockObject $inner */ $inner = $this->createMock(WpPostArgsFromSchemaObjectInterface::class); - $inner->method('create')->willReturn(['ID' => 123]); + $inner->method('transform')->willReturn(['ID' => 123]); $verifyChecksum = new VerifyChecksum($inner, new FakeWpService(['getPostMeta' => '123'])); - $postArgs = $verifyChecksum->create(new Thing(), $this->getSourceMock()); + $postArgs = $verifyChecksum->transform(new Thing(), $this->getSourceMock()); $this->assertEquals(123, $postArgs['ID']); } @@ -70,10 +70,10 @@ public function testCreateNoId() * @var WpPostArgsFromSchemaObjectInterface|\PHPUnit\Framework\MockObject\MockObject $inner */ $inner = $this->createMock(WpPostArgsFromSchemaObjectInterface::class); - $inner->method('create')->willReturn(['meta_input' => ['checksum' => '321']]); + $inner->method('transform')->willReturn(['meta_input' => ['checksum' => '321']]); $verifyChecksum = new VerifyChecksum($inner, new FakeWpService(['getPostMeta' => '321'])); - $postArgs = $verifyChecksum->create(new Thing(), $this->getSourceMock()); + $postArgs = $verifyChecksum->transform(new Thing(), $this->getSourceMock()); $this->assertArrayNotHasKey('ID', $postArgs); } diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/WpPostFactory.php b/library/ExternalContent/WpPostArgsFromSchemaObject/WpPostArgsFromSchemaObject.php similarity index 82% rename from library/ExternalContent/WpPostArgsFromSchemaObject/WpPostFactory.php rename to library/ExternalContent/WpPostArgsFromSchemaObject/WpPostArgsFromSchemaObject.php index 53ed972ba..c67d734de 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/WpPostFactory.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/WpPostArgsFromSchemaObject.php @@ -5,11 +5,11 @@ use Spatie\SchemaOrg\BaseType; /** - * Class WpPostFactory + * Class WpArgsFromSchemaObject * * This class is responsible for creating WordPress post arguments from a schema object. */ -class WpPostFactory implements WpPostArgsFromSchemaObjectInterface +class WpPostArgsFromSchemaObject implements WpPostArgsFromSchemaObjectInterface { /** * Creates WordPress post arguments from a schema object. @@ -17,7 +17,7 @@ class WpPostFactory implements WpPostArgsFromSchemaObjectInterface * @param BaseType $schemaObject The schema object containing post data. * @return array The array of WordPress post arguments. */ - public function create(BaseType $schemaObject): array + public function transform(BaseType $schemaObject): array { $title = html_entity_decode($schemaObject['name'] ?? ''); $title = urldecode($title); diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/WpPostFactory.test.php b/library/ExternalContent/WpPostArgsFromSchemaObject/WpPostArgsFromSchemaObject.test.php similarity index 73% rename from library/ExternalContent/WpPostArgsFromSchemaObject/WpPostFactory.test.php rename to library/ExternalContent/WpPostArgsFromSchemaObject/WpPostArgsFromSchemaObject.test.php index 8cc665aa1..ff65df5b9 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/WpPostFactory.test.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/WpPostArgsFromSchemaObject.test.php @@ -2,12 +2,10 @@ namespace Municipio\ExternalContent\WpPostArgsFromSchemaObject; -use Municipio\ExternalContent\Sources\SourceInterface; -use Municipio\ExternalContent\Sources\Source; use PHPUnit\Framework\TestCase; use Spatie\SchemaOrg\BaseType; -class WpPostFactoryTest extends TestCase +class WpArgsFromSchemaObjectTest extends TestCase { /** * @testdox array is created with title and content @@ -15,9 +13,9 @@ class WpPostFactoryTest extends TestCase public function testCreate() { $schemaObject = $this->getBaseTypeInstance([ 'name' => 'Title', 'description' => 'Content', ]); - $wpPostFactory = new WpPostFactory('test_post_type'); + $wpPostFactory = new WpPostArgsFromSchemaObject('test_post_type'); - $wpPost = $wpPostFactory->create($schemaObject); + $wpPost = $wpPostFactory->transform($schemaObject); $this->assertEquals('Title', $wpPost['post_title']); $this->assertEquals('Content', $wpPost['post_content']); @@ -29,9 +27,9 @@ public function testCreate() public function testCreateWithPublishStatus() { $schemaObject = $this->getBaseTypeInstance(); - $wpPostFactory = new WpPostFactory('test_post_type'); + $wpPostFactory = new WpPostArgsFromSchemaObject('test_post_type'); - $wpPost = $wpPostFactory->create($schemaObject,); + $wpPost = $wpPostFactory->transform($schemaObject,); $this->assertEquals('publish', $wpPost['post_status']); } diff --git a/library/ExternalContent/WpPostArgsFromSchemaObject/WpPostArgsFromSchemaObjectInterface.php b/library/ExternalContent/WpPostArgsFromSchemaObject/WpPostArgsFromSchemaObjectInterface.php index 10d2fd096..08f37439a 100644 --- a/library/ExternalContent/WpPostArgsFromSchemaObject/WpPostArgsFromSchemaObjectInterface.php +++ b/library/ExternalContent/WpPostArgsFromSchemaObject/WpPostArgsFromSchemaObjectInterface.php @@ -9,5 +9,5 @@ interface WpPostArgsFromSchemaObjectInterface /** * Create a array from a schema object to be used to insert/update a WP_Post. */ - public function create(BaseType $schemaObject): array; + public function transform(BaseType $schemaObject): array; }