createMock(\medoo::class); $mockDb->expects($this->once()) ->method('get') ->with('pp_banners', '*', ['id' => 1]) ->willReturn(['id' => 1, 'name' => 'Baner testowy', 'status' => 1]); $mockDb->expects($this->once()) ->method('select') ->with('pp_banners_langs', '*', ['id_banner' => 1]) ->willReturn([ ['id_lang' => 'pl', 'src' => 'banner.jpg', 'url' => '/promo'], ['id_lang' => 'en', 'src' => 'banner-en.jpg', 'url' => '/promo-en'], ]); $repository = new BannerRepository($mockDb); // Act $banner = $repository->find(1); // Assert $this->assertIsArray($banner); $this->assertEquals('Baner testowy', $banner['name']); $this->assertArrayHasKey('languages', $banner); $this->assertCount(2, $banner['languages']); $this->assertEquals('banner.jpg', $banner['languages']['pl']['src']); } /** * Test pobierania banera - nie istnieje */ public function testFindReturnsNullWhenNotFound() { // Arrange $mockDb = $this->createMock(\medoo::class); $mockDb->method('get')->willReturn(false); $repository = new BannerRepository($mockDb); // Act $banner = $repository->find(999); // Assert $this->assertNull($banner); } /** * Test usuwania banera - sukces */ public function testDeleteReturnsTrue() { // Arrange $mockDb = $this->createMock(\medoo::class); $mockDb->expects($this->once()) ->method('delete') ->with('pp_banners', ['id' => 5]) ->willReturn($this->createMock(\PDOStatement::class)); $repository = new BannerRepository($mockDb); // Act $result = $repository->delete(5); // Assert $this->assertTrue($result); } /** * Test zapisywania nowego banera */ public function testSaveInsertsNewBanner() { // Arrange $mockDb = $this->createMock(\medoo::class); // insert() wywoływane 2x: raz dla banera, raz dla tłumaczenia $mockDb->expects($this->exactly(2)) ->method('insert'); $mockDb->expects($this->once()) ->method('id') ->willReturn(10); // get() for checking existing translations - returns false (no existing) $mockDb->method('get')->willReturn(false); $repository = new BannerRepository($mockDb); // Act $result = $repository->save([ 'name' => 'Nowy baner', 'status' => 'on', 'date_start' => '', 'date_end' => '', 'home_page' => 'on', 'src' => ['pl' => 'banner.jpg'], 'url' => ['pl' => '/promo'], 'html' => ['pl' => ''], 'text' => ['pl' => 'Tekst'], ]); // Assert $this->assertEquals(10, $result); } }