-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: name changes and new resolver for date format
- Loading branch information
1 parent
86bcefa
commit eaf617b
Showing
17 changed files
with
181 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Municipio\PostObject\Date; | ||
|
||
use Municipio\PostObject\PostObjectInterface; | ||
use WpService\Contracts\GetThemeMod; | ||
|
||
class ArchiveDateFormatResolver implements ArchiveDateFormatResolverInterface | ||
{ | ||
public function __construct( | ||
private PostObjectInterface $postObject, | ||
private GetThemeMod $wpService | ||
) { | ||
} | ||
|
||
public function resolve(): string | ||
{ | ||
$dateFormat = $this->wpService->getThemeMod('archive_' . $this->postObject->getPostType() . '_date_format', 'date-time'); | ||
|
||
return $dateFormat; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
library/PostObject/Date/ArchiveDateFormatResolver.test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Municipio\PostObject\Date; | ||
|
||
use Municipio\PostObject\PostObjectInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use WpService\Implementations\FakeWpService; | ||
|
||
class ArchiveDateFormatResolverTest extends TestCase | ||
{ | ||
/** | ||
* @testdox class can be instantiated | ||
* @runInSeparateProcess | ||
*/ | ||
public function testClassCanBeInstantiated() | ||
{ | ||
$this->assertInstanceOf( | ||
ArchiveDateFormatResolver::class, | ||
new ArchiveDateFormatResolver($this->createMock(PostObjectInterface::class), new FakeWpService()) | ||
); | ||
} | ||
|
||
/** | ||
* @testdox resolve() returns archive post date setting | ||
* @runInSeparateProcess | ||
*/ | ||
public function testResolveReturnsFoundArchiveSetting() | ||
{ | ||
$postObject = $this->createMock(PostObjectInterface::class); | ||
$wpService = new FakeWpService(['getThemeMod' => 'date-time']); | ||
|
||
$resolver = new ArchiveDateSourceResolver($postObject, $wpService); | ||
|
||
$result = $resolver->resolve(); | ||
|
||
$this->assertEquals('date-time', $result); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
library/PostObject/Date/ArchiveDateFormatResolverInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Municipio\PostObject\Date; | ||
|
||
interface ArchiveDateFormatResolverInterface | ||
{ | ||
/** | ||
* Resolve the archive date format. | ||
*/ | ||
public function resolve(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
library/PostObject/Date/CachedArchiveDateFormatResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Municipio\PostObject\Date; | ||
|
||
use Municipio\PostObject\PostObjectInterface; | ||
use Municipio\PostObject\Date\ArchiveDateFormatResolverInterface; | ||
|
||
class CachedArchiveDateFormatResolver | ||
{ | ||
private static array $cache = []; | ||
|
||
public function __construct( | ||
private PostObjectInterface $postObject, | ||
private ArchiveDateFormatResolverInterface $innerResolver | ||
) { | ||
} | ||
|
||
public function resolve(): string | ||
{ | ||
$cacheKey = $this->postObject->getBlogId() . '_' . $this->postObject->getPostType(); | ||
|
||
if (array_key_exists($cacheKey, self::$cache)) { | ||
return self::$cache[$cacheKey]; | ||
} | ||
|
||
return self::$cache[$cacheKey] = $this->innerResolver->resolve(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
library/PostObject/Date/CachedArchiveDateFormatResolver.test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Municipio\PostObject\Date; | ||
|
||
use Municipio\PostObject\PostObjectInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use WpService\Implementations\FakeWpService; | ||
|
||
class CachedArchiveDateFormatResolverTest extends TestCase | ||
{ | ||
/** | ||
* @testdox class can be instantiated | ||
* @runInSeparateProcess | ||
*/ | ||
public function testClassCanBeInstantiated() | ||
{ | ||
$this->assertInstanceOf( | ||
CachedArchiveDateFormatResolver::class, | ||
new CachedArchiveDateFormatResolver( | ||
$this->createMock(PostObjectInterface::class), | ||
$this->createMock(ArchiveDateFormatResolverInterface::class) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @testdox resolve() caches result from inner resolver | ||
* @runInSeparateProcess | ||
*/ | ||
public function testResolveCachesResultFromInnerResolver() | ||
{ | ||
$postObject = $this->createMock(PostObjectInterface::class); | ||
$postObject->method('getPostType')->willReturn('post_type'); | ||
$postObject->method('getBlogId')->willReturn(1); | ||
$innerResolver = $this->createMock(ArchiveDateFormatResolverInterface::class); | ||
$innerResolver->expects($this->exactly(1))->method('resolve')->willReturn('date'); | ||
|
||
$resolver = new CachedArchiveDateFormatResolver($postObject, $innerResolver); | ||
|
||
$result = $resolver->resolve(); | ||
$result = $resolver->resolve(); | ||
|
||
$this->assertEquals('date', $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.