-
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.
Merge pull request #10 from IlyasAkbergen/feature/2-entities
#2 add Entities, abstract ArticlesProvider and tests
- Loading branch information
Showing
22 changed files
with
412 additions
and
7 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,18 @@ | ||
name: PHPStan | ||
|
||
on: | ||
push: | ||
branches: [ "master" ] | ||
pull_request: | ||
branches: [ "master" ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: PHPStan | ||
run: make phpstan |
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,18 @@ | ||
name: PHPUnit | ||
|
||
on: | ||
push: | ||
branches: [ "master" ] | ||
pull_request: | ||
branches: [ "master" ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Test | ||
run: make test |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Jobs; | ||
|
||
use Domain\Service\ArticlesProvider; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
|
||
class FetchArticles implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
public function __construct( | ||
readonly private ArticlesProvider $articlesProvider, | ||
) { | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
$this->articlesProvider->fetchArticles(); | ||
} | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Repository; | ||
|
||
use Domain\Entity\Article; | ||
use Domain\Repository\ArticleRepositoryInterface; | ||
|
||
class ArticleRepositoryEloquent implements ArticleRepositoryInterface | ||
{ | ||
public function find(int $id): ?Article | ||
{ | ||
// TODO: Implement find() method. | ||
return null; | ||
} | ||
|
||
public function save(Article ...$articles): void | ||
{ | ||
// TODO: Implement save() method. | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\Entity; | ||
|
||
use DateTimeImmutable; | ||
use Domain\ValueObject\KeywordsCollection; | ||
|
||
readonly class Article | ||
{ | ||
public function __construct( | ||
public ?int $id = null, | ||
public string $title, | ||
public string $content, | ||
public Author $author, | ||
public Source $source, | ||
public Category $category, | ||
public DateTimeImmutable $publishedAt, | ||
public KeywordsCollection $keywords, | ||
) { | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\Entity; | ||
|
||
use Domain\ValueObject\FullName; | ||
|
||
readonly class Author | ||
{ | ||
public function __construct( | ||
public int $id, | ||
public FullName $fullName, | ||
) { | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\Entity; | ||
|
||
readonly class Category | ||
{ | ||
public function __construct( | ||
public int $id, | ||
public string $name, | ||
) { | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\Entity; | ||
|
||
use Domain\ValueObject\Url; | ||
|
||
readonly class Source | ||
{ | ||
public function __construct( | ||
public int $id, | ||
public string $name, | ||
public Url $url, | ||
) { | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\Exception; | ||
|
||
class DomainException extends \Exception | ||
{ | ||
} |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\Repository; | ||
|
||
use Domain\Entity\Article; | ||
|
||
interface ArticleRepositoryInterface | ||
{ | ||
public function find(int $id): ?Article; | ||
|
||
public function save(Article ...$articles): void; | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\Service; | ||
|
||
use Domain\Entity\Article; | ||
use Domain\Repository\ArticleRepositoryInterface; | ||
|
||
abstract class ArticlesProvider | ||
{ | ||
public function __construct( | ||
protected ArticleRepositoryInterface $articleRepository, | ||
) { | ||
} | ||
|
||
public function fetchArticles(): void | ||
{ | ||
$this->articleRepository->save(...$this->getNewArticles()); | ||
} | ||
|
||
/** | ||
* @return Article[] | ||
*/ | ||
abstract protected function getNewArticles(): array; | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\ValueObject; | ||
|
||
use Domain\Exception\DomainException; | ||
|
||
readonly class FullName | ||
{ | ||
/** | ||
* @throws DomainException | ||
*/ | ||
public function __construct( | ||
public string $firstName, | ||
public string $lastName, | ||
) { | ||
if (empty($this->firstName) || empty($this->lastName)) { | ||
throw new DomainException('First name and last name cannot be empty'); | ||
} | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->firstName . ' ' . $this->lastName; | ||
} | ||
} | ||
|
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\ValueObject; | ||
|
||
use Domain\Exception\DomainException; | ||
|
||
readonly class Keyword | ||
{ | ||
/** | ||
* @throws DomainException | ||
*/ | ||
public function __construct( | ||
public string $value, | ||
) { | ||
if (empty($this->value)) { | ||
throw new DomainException('Keyword cannot be empty'); | ||
} | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\ValueObject; | ||
|
||
readonly class KeywordsCollection | ||
{ | ||
public array $keywords; | ||
|
||
public function __construct( | ||
Keyword ...$keywords, | ||
) { | ||
$this->keywords = $keywords; | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Domain\ValueObject; | ||
|
||
readonly class Url | ||
{ | ||
public function __construct( | ||
public string $value, | ||
) { | ||
if (!filter_var($this->value, FILTER_VALIDATE_URL)) { | ||
throw new \InvalidArgumentException('Invalid URL'); | ||
} | ||
} | ||
} |
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,8 +1,9 @@ | ||
<?php | ||
|
||
use Illuminate\Foundation\Inspiring; | ||
use Illuminate\Support\Facades\Artisan; | ||
declare(strict_types=1); | ||
|
||
Artisan::command('inspire', function () { | ||
$this->comment(Inspiring::quote()); | ||
})->purpose('Display an inspiring quote')->hourly(); | ||
use App\Jobs\FetchArticles; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\Facades\Schedule; | ||
|
||
//Schedule::job(new FetchArticles(App::get(NewsAPIArticlesProvider::class)))->everyFiveMinutes(); |
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Unit\Domain\Service; | ||
|
||
use Domain\Entity\Article; | ||
use Domain\Repository\ArticleRepositoryInterface; | ||
use Domain\Service\ArticlesProvider; | ||
use PHPUnit\Framework\MockObject\Exception; | ||
use Tests\TestCase; | ||
|
||
class ArticlesProviderTest extends TestCase | ||
{ | ||
/** | ||
* @throws Exception | ||
*/ | ||
public function testFetchArticles(): void | ||
{ | ||
$articles = [ | ||
self::createMock(Article::class), | ||
self::createMock(Article::class), | ||
]; | ||
$articleRepository = self::createMock(ArticleRepositoryInterface::class); | ||
$articleRepository->expects(self::once()) | ||
->method('save') | ||
->with(...$articles); | ||
|
||
$articlesProvider = new class($articles, $articleRepository) extends ArticlesProvider { | ||
/** | ||
* @param Article[] $articles | ||
*/ | ||
public function __construct( | ||
private $articles, | ||
ArticleRepositoryInterface $articleRepository, | ||
) { | ||
parent::__construct($articleRepository); | ||
} | ||
|
||
protected function getNewArticles(): array | ||
{ | ||
return $this->articles; | ||
} | ||
}; | ||
|
||
$articlesProvider->fetchArticles(); | ||
} | ||
} |
Oops, something went wrong.