-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
speckmaier
committed
Aug 26, 2024
1 parent
f72924d
commit 2e02adf
Showing
3 changed files
with
91 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace VAF\WP\Framework\Permalink; | ||
|
||
class Permalink | ||
{ | ||
|
||
static ?PermalinkResolver $permalinkResolver = null; | ||
private int $postId; | ||
|
||
public static function fake(PermalinkResolver $permalinkResolver) | ||
{ | ||
self::$permalinkResolver = $permalinkResolver; | ||
} | ||
|
||
public static function fromPostId(int $postId): self | ||
{ | ||
$permalink = new static(); | ||
|
||
$permalink->postId = $postId; | ||
|
||
return $permalink; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return self::resolver()->permalinkForPostId($this->postId); | ||
} | ||
|
||
private static function resolver(): PermalinkResolver | ||
{ | ||
if(self::$permalinkResolver === null) { | ||
self::$permalinkResolver = new PermalinkResolver(); | ||
} | ||
|
||
return self::$permalinkResolver; | ||
} | ||
|
||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace VAF\WP\Framework\Permalink; | ||
|
||
class PermalinkResolver | ||
{ | ||
|
||
public function permalinkForPostId(string $postId): string | ||
{ | ||
return get_permalink($postId); | ||
} | ||
|
||
} |
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,39 @@ | ||
<?php | ||
|
||
namespace VAF\WP\FrameworkTests\Unit; | ||
|
||
use VAF\WP\Framework\Permalink\Permalink; | ||
use VAF\WP\Framework\Permalink\PermalinkResolver; | ||
use VAF\WP\FrameworkTests\TestCase; | ||
|
||
class PermalinkTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function should_be_able_to_fake_permalink_before_creating_permalinnk() | ||
{ | ||
$resolver = \Mockery::mock(PermalinkResolver::class); | ||
$resolver->shouldReceive('permalinkForPostId')->with(15)->andReturn('expected permalink'); | ||
|
||
Permalink::fake($resolver); | ||
$permalink = Permalink::fromPostId(15); | ||
|
||
$this->assertEquals('expected permalink', (string)$permalink); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function should_be_able_to_fake_permalink_after_creating_permalinnk() | ||
{ | ||
$resolver = \Mockery::mock(PermalinkResolver::class); | ||
$resolver->shouldReceive('permalinkForPostId')->with(15)->andReturn('expected permalink'); | ||
|
||
$permalink = Permalink::fromPostId(15); | ||
Permalink::fake($resolver); | ||
|
||
$this->assertEquals('expected permalink', (string)$permalink); | ||
} | ||
} |