-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
Showing
8 changed files
with
225 additions
and
36 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 |
---|---|---|
@@ -1,38 +1,50 @@ | ||
<?php | ||
|
||
namespace Coyote\Services\Widgets; | ||
|
||
use Coyote\Repositories\Contracts\MicroblogRepositoryInterface as MicroblogRepository; | ||
use Coyote\Repositories\Contracts\UserRepositoryInterface as UserRepository; | ||
use Coyote\Microblog; | ||
use Coyote\Repositories\Criteria\Microblog\OnlyMine; | ||
use Coyote\Repositories\Criteria\Microblog\WithTag; | ||
use Illuminate\Contracts\Cache\Repository as Cache; | ||
use Coyote\Repositories\Eloquent\MicroblogRepository; | ||
use Coyote\User; | ||
use Illuminate\Contracts\Cache; | ||
use Illuminate\Contracts\View\View; | ||
|
||
class WhatsNew | ||
{ | ||
protected MicroblogRepository $microblog; | ||
protected UserRepository $user; | ||
protected Cache $cache; | ||
|
||
public function __construct(MicroblogRepository $microblog, UserRepository $user, Cache $cache) | ||
public function __construct( | ||
private MicroblogRepository $microblog, | ||
private Cache\Repository $cache) | ||
{ | ||
$this->microblog = $microblog; | ||
$this->user = $user; | ||
$this->cache = $cache; | ||
} | ||
|
||
public function render(): string | ||
{ | ||
return $this->cache->remember('widget:whats-new', now()->addHour(), function () { | ||
$this->user->resetCriteria(); | ||
|
||
$user = $this->user->findBy('name', '4programmers.net', ['id']); | ||
return $this->cache->remember( | ||
'widget:whats-new', | ||
now()->addHour(), | ||
fn() => $this->widgetView()->render()); | ||
} | ||
|
||
$this->microblog->resetCriteria(); | ||
$this->microblog->pushCriteria(new WithTag('4programmers.net')); | ||
$this->microblog->pushCriteria(new OnlyMine($user->id ?? null)); | ||
private function widgetView(): View | ||
{ | ||
$this->microblog->resetCriteria(); | ||
$this->microblog->pushCriteria(new WithTag('4programmers.net')); | ||
$this->microblog->pushCriteria(new OnlyMine($this->userIdByName('4programmers.net'))); | ||
|
||
return view('homepage.whats-new', [ | ||
'href' => route('microblog.tag', ['4programmers.net']), | ||
'microblogs' => $this->microblog->recent()->map(fn(Microblog $microblog) => [ | ||
'id' => $microblog->id, | ||
'summary' => excerpt($microblog->html), | ||
'href' => route('microblog.view', [$microblog->id]), | ||
'date' => $microblog->created_at->formatLocalized('%d %b %y'), | ||
]), | ||
]); | ||
} | ||
|
||
return view('homepage.whats-new', ['microblogs' => $this->microblog->recent()])->render(); | ||
}); | ||
private function userIdByName(string $name): ?int | ||
{ | ||
$user = User::query()->where('name', '=', $name)->first(['id']); | ||
return $user->id ?? null; | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
namespace Tests\Unit\BaseFixture; | ||
|
||
use Illuminate\Contracts\Cache; | ||
use Tests\Unit\BaseFixture\Server\Laravel; | ||
|
||
trait ClearedCache | ||
{ | ||
use Laravel\Application; | ||
|
||
/** | ||
* @before | ||
*/ | ||
function clearCache(): void | ||
{ | ||
/** @var Cache\Repository $cache */ | ||
$cache = $this->laravel->app[Cache\Repository::class]; | ||
$cache->clear(); | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
namespace Tests\Unit\WhatsNew\Fixture; | ||
|
||
use Carbon\Carbon; | ||
use Coyote\Microblog; | ||
use Coyote\Tag; | ||
use Coyote\User; | ||
use Tests\Unit\BaseFixture\Server\Laravel; | ||
use Tests\Unit\Seo; | ||
|
||
trait Models | ||
{ | ||
use Laravel\Transactional; | ||
|
||
function newWhatsNewItem(string $text, string $dateTime): int | ||
{ | ||
return $this->newMicroblog( | ||
$text, | ||
new Carbon($dateTime), | ||
$this->newTag('4programmers.net')); | ||
} | ||
|
||
function newMicroblog(string $text, Carbon $date, Tag $tag): int | ||
{ | ||
$microblog = new Microblog([ | ||
'user_id' => $this->systemUser()->id, | ||
'text' => $text, | ||
]); | ||
$microblog->created_at = $date; | ||
$microblog->save(); | ||
$microblog->tags()->save($tag); | ||
return $microblog->id; | ||
} | ||
|
||
function newTag(string $name): Tag | ||
{ | ||
$tag = new Tag(['name' => $name]); | ||
$tag->save(); | ||
return $tag; | ||
} | ||
|
||
function systemUser(): User | ||
{ | ||
$user = new User(); | ||
$user->name = '4programmers.net'; | ||
$user->email = 'irrelevant'; | ||
$user->save(); | ||
return $user; | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
namespace Tests\Unit\WhatsNew\Fixture; | ||
|
||
use Tests\Unit\BaseFixture\View; | ||
use Tests\Unit\BaseFixture\View\ViewDom; | ||
|
||
trait NewsItems | ||
{ | ||
use View\HtmlView; | ||
|
||
function newsItem(): array | ||
{ | ||
$dom = new ViewDom($this->htmlView('/')); | ||
/** @var \DOMElement $listItem */ | ||
foreach ($dom->elements(xPath:'//aside//div[@class="card bg-dark"]//ul/li') as $listItem) { | ||
return $this->listItem($listItem); | ||
} | ||
throw new \AssertionError("Failed finding news item."); | ||
} | ||
|
||
function listItem(\DOMElement $listItem): array | ||
{ | ||
$item = []; | ||
/** @var \DOMElement $child */ | ||
foreach ($listItem->childNodes as $child) { | ||
if ($child->nodeName === 'a') { | ||
$item['text'] = $child->textContent; | ||
$item['href'] = $child->getAttribute('href'); | ||
} | ||
if ($child->nodeName === 'div') { | ||
$item['date'] = $child->textContent; | ||
} | ||
} | ||
return $item; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
namespace Tests\Unit\WhatsNew\Fixture; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
use SebastianBergmann\Comparator\ComparisonFailure; | ||
|
||
class TrimmedString extends Constraint | ||
{ | ||
public function __construct(private string $value) | ||
{ | ||
} | ||
|
||
public function evaluate($other, string $description = '', bool $returnResult = false): ?bool | ||
{ | ||
$success = $this->value === \trim($other); | ||
if ($returnResult) { | ||
return $success; | ||
} | ||
if (!$success) { | ||
$this->fail($other, $description, new ComparisonFailure( | ||
$this->value, | ||
$other, | ||
"'$this->value'", | ||
"'$other'", | ||
)); | ||
} | ||
return null; | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'is identical to trimmed ' . $this->exporter()->export($this->value); | ||
} | ||
|
||
protected function failureDescription($other): string | ||
{ | ||
if (\is_string($other)) { | ||
return 'two strings are identical'; | ||
} | ||
return parent::failureDescription($other); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
namespace Tests\Unit\WhatsNew; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Tests\Unit\BaseFixture; | ||
use Tests\Unit\BaseFixture\Constraint\ArrayKey; | ||
use Tests\Unit\Seo; | ||
use Tests\Unit\WhatsNew; | ||
use Tests\Unit\WhatsNew\Fixture\TrimmedString; | ||
|
||
class Test extends TestCase | ||
{ | ||
use BaseFixture\ClearedCache; | ||
use BaseFixture\Server\RelativeUri; | ||
use WhatsNew\Fixture\Models; | ||
use WhatsNew\Fixture\NewsItems; | ||
|
||
public function test() | ||
{ | ||
$id = $this->newWhatsNewItem('Valar morghulis.', '2005-04-02 21:37:13'); | ||
$this->assertThat( | ||
$this->newsItem(), | ||
$this->logicalAnd( | ||
new ArrayKey('text', new TrimmedString('Valar morghulis.')), | ||
new ArrayKey('href', $this->relativeUri("/Mikroblogi/View/$id")), | ||
new ArrayKey('date', new TrimmedString('— 02 kwi 05')), | ||
)); | ||
} | ||
} |