Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Category {}
- interface CategoryRepository {
- /**
- * @return Category[]
- */
- public function all(): array;
- public function save(Category $category);
- }
- class CategoryService {
- private $categoryRepository;
- public function __construct(CategoryRepository $categoryRepository) {
- $this->categoryRepository = $categoryRepository;
- }
- public function hardDomainLogic(Category $category) {
- $availableCategories = $this->categoryRepository->all();
- // ...
- }
- }
- class TestCategoryService {
- public function testHardDomainLogic() {
- $categoryA = new Category();
- $categoryB = new Category();
- $categoryC = new Category();
- $categoryRepository = $this->createCategoryRepository();
- $categoryRepository->save($categoryA);
- $categoryRepository->save($categoryB);
- $service = new CategoryService($categoryRepository);
- $service->hardDomainLogic($categoryC);
- }
- private function createCategoryRepository(): CategoryRepository {
- return new class implements CategoryRepository {
- private $categories = [];
- public function save(Category $category) {
- $this->categories[] = $category;
- }
- public function all(): array {
- return $this->categories;
- }
- };
- }
- }
- $test = new TestCategoryService();
- $test->testHardDomainLogic();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement