Skip to content

Commit

Permalink
GTA-463 permalink with fake feature
Browse files Browse the repository at this point in the history
  • Loading branch information
speckmaier committed Aug 26, 2024
1 parent f72924d commit 2e02adf
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Permalink/Permalink.php
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;
}

}
13 changes: 13 additions & 0 deletions src/Permalink/PermalinkResolver.php
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);
}

}
39 changes: 39 additions & 0 deletions tests/Unit/PermalinkTest.php
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);
}
}

0 comments on commit 2e02adf

Please sign in to comment.