Skip to content

Commit

Permalink
refactor: rename create method to transform in WpPostArgsFromSchemaOb…
Browse files Browse the repository at this point in the history
…jectInterface and its implementations
  • Loading branch information
thorbrink committed Feb 28, 2025
1 parent dd5ea54 commit 48be030
Show file tree
Hide file tree
Showing 29 changed files with 136 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ 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']);
}

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' => '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Municipio\ExternalContent\WpPostArgsFromSchemaObject\Factory;

use Municipio\ExternalContent\WpPostArgsFromSchemaObject\WpPostArgsFromSchemaObjectInterface;

class Factory implements FactoryInterface
{
/**
* @inheritDoc
*/
public function create(): WpPostArgsFromSchemaObjectInterface
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Municipio\ExternalContent\WpPostArgsFromSchemaObject\Factory;

use Municipio\ExternalContent\WpPostArgsFromSchemaObject\WpPostArgsFromSchemaObjectInterface;

interface FactoryInterface
{
/**
* Create a new instance of the WpPostArgsFromSchemaObjectInterface.
*
* @return WpPostArgsFromSchemaObjectInterface
*/
public function create(): WpPostArgsFromSchemaObjectInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public function __construct(
) {
}

public function create(BaseType $schemaObject): array
public function transform(BaseType $schemaObject): array
{
$post = $this->inner->create($schemaObject);
$post = $this->inner->transform($schemaObject);

if (!empty($schemaObject['@id'])) {
$postWithSameOriginId = $this->wpService->getPosts([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Municipio\ExternalContent\WpPostArgsFromSchemaObject;

use PHPUnit\Framework\TestCase;
use Spatie\SchemaOrg\BaseType;
use WpService\Contracts\GetPosts;
use WpService\Implementations\FakeWpService;

class IdDecoratorTest extends TestCase
{
/**
* @testdox class can be instantiated
*/
public function testClassCanBeInstantiated()
{
$factory = new IdDecorator('', '', new WpPostArgsFromSchemaObject(), new FakeWpService());
$this->assertInstanceOf(IdDecorator::class, $factory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand All @@ -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']);
}
Expand All @@ -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']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand All @@ -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']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPUnit\Framework\TestCase;
use Spatie\SchemaOrg\BaseType;
use Spatie\SchemaOrg\Schema;
use Spatie\SchemaOrg\Thing;

class SchemaDataDecoratorTest extends TestCase
{
Expand All @@ -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']);
}
Expand All @@ -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']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
Loading

0 comments on commit 48be030

Please sign in to comment.