connection = Mockery::mock(ConnectionInterface::class); $this->repository = new DatabaseContentRepository($this->connection); }); it('saves content to database', function () { $content = CmsTestHelpers::createContent(new \App\Framework\DateTime\SystemClock()); $this->connection->shouldReceive('execute') ->once() ->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class)) ->andReturn(1); $this->repository->save($content); }); it('finds content by id', function () { $contentId = ContentId::generate(new \App\Framework\DateTime\SystemClock()); $row = [ 'id' => $contentId->toString(), 'content_type_id' => 'page', 'slug' => 'test-page', 'title' => 'Test Page', 'blocks' => json_encode([['id' => 'hero-1', 'type' => 'hero', 'data' => ['title' => 'Hero']]]), 'meta_data' => null, 'status' => 'draft', 'author_id' => null, 'default_locale' => 'en', 'published_at' => null, 'created_at' => '2025-01-15 10:00:00', 'updated_at' => '2025-01-15 10:00:00', ]; $result = Mockery::mock(ResultInterface::class); $result->shouldReceive('fetch') ->once() ->andReturn($row); $this->connection->shouldReceive('query') ->once() ->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class)) ->andReturn($result); $found = $this->repository->findById($contentId); expect($found)->toBeInstanceOf(Content::class); expect($found->id->equals($contentId))->toBeTrue(); }); it('returns null when content not found by id', function () { $contentId = ContentId::generate(new \App\Framework\DateTime\SystemClock()); $result = Mockery::mock(ResultInterface::class); $result->shouldReceive('fetch') ->once() ->andReturn(null); $this->connection->shouldReceive('query') ->once() ->andReturn($result); $found = $this->repository->findById($contentId); expect($found)->toBeNull(); }); it('finds content by slug', function () { $slug = ContentSlug::fromString('test-page'); $row = [ 'id' => ContentId::generate(new \App\Framework\DateTime\SystemClock())->toString(), 'content_type_id' => 'page', 'slug' => 'test-page', 'title' => 'Test Page', 'blocks' => json_encode([['id' => 'hero-1', 'type' => 'hero', 'data' => ['title' => 'Hero']]]), 'meta_data' => null, 'status' => 'draft', 'author_id' => null, 'default_locale' => 'en', 'published_at' => null, 'created_at' => '2025-01-15 10:00:00', 'updated_at' => '2025-01-15 10:00:00', ]; $result = Mockery::mock(ResultInterface::class); $result->shouldReceive('fetch') ->once() ->andReturn($row); $this->connection->shouldReceive('query') ->once() ->andReturn($result); $found = $this->repository->findBySlug($slug); expect($found)->toBeInstanceOf(Content::class); expect($found->slug->equals($slug))->toBeTrue(); }); it('checks if slug exists', function () { $slug = ContentSlug::fromString('test-page'); $result = Mockery::mock(ResultInterface::class); $result->shouldReceive('fetch') ->once() ->andReturn(['count' => 1]); $this->connection->shouldReceive('query') ->once() ->andReturn($result); expect($this->repository->existsSlug($slug))->toBeTrue(); }); it('finds contents by type', function () { $typeId = ContentTypeId::fromString('page'); $row = [ 'id' => ContentId::generate(new \App\Framework\DateTime\SystemClock())->toString(), 'content_type_id' => 'page', 'slug' => 'test-page', 'title' => 'Test Page', 'blocks' => json_encode([['id' => 'hero-1', 'type' => 'hero', 'data' => ['title' => 'Hero']]]), 'meta_data' => null, 'status' => 'draft', 'author_id' => null, 'default_locale' => 'en', 'published_at' => null, 'created_at' => '2025-01-15 10:00:00', 'updated_at' => '2025-01-15 10:00:00', ]; $result = Mockery::mock(ResultInterface::class); $result->shouldReceive('fetchAll') ->once() ->andReturn([$row]); $this->connection->shouldReceive('query') ->once() ->andReturn($result); $found = $this->repository->findByType($typeId); expect($found)->toBeArray(); expect($found)->toHaveCount(1); }); it('deletes content', function () { $contentId = ContentId::generate(new \App\Framework\DateTime\SystemClock()); $this->connection->shouldReceive('execute') ->once() ->with(Mockery::type(\App\Framework\Database\ValueObjects\SqlQuery::class)) ->andReturn(1); $this->repository->delete($contentId); }); });