diff --git a/src/DateFormatter.php b/src/DateFormatter.php index 24e9af0..c3dfc4b 100644 --- a/src/DateFormatter.php +++ b/src/DateFormatter.php @@ -1,4 +1,5 @@ -format('G'); if ($currentHour >= 0 && $currentHour < 6) diff --git a/tests/CalculatorTest.php b/tests/CalculatorTest.php new file mode 100644 index 0000000..c96ef8b --- /dev/null +++ b/tests/CalculatorTest.php @@ -0,0 +1,38 @@ +assertEquals( + 3, + Calculator::add(1, 2) + ); + + // navic i zaporne hodnoty + $this->assertEquals( + -3, + Calculator::add(-1, -2) + ); + } + + public function testDivideException(): void + { + $this->expectException(InvalidArgumentException::class); + + // zavolat metodu, ktera zpusobi exception + Calculator::divide(1, 0); + } + + public function testDivideOne(): void + { + $this->assertEquals( + 2, + Calculator::divide(4, 2) + ); + } +} diff --git a/tests/DateFormatterTest.php b/tests/DateFormatterTest.php new file mode 100644 index 0000000..bde1fb3 --- /dev/null +++ b/tests/DateFormatterTest.php @@ -0,0 +1,24 @@ +assertEquals("Night", DateFormatter::getPartOfDay(new DateTime("2018-05-27 0:00:01"))); + $this->assertEquals("Night", DateFormatter::getPartOfDay(new DateTime("2018-05-27 5:59:01"))); + + $this->assertEquals("Morning", DateFormatter::getPartOfDay(new DateTime("2018-05-27 6:00:01"))); + $this->assertEquals("Morning", DateFormatter::getPartOfDay(new DateTime("2018-05-27 11:59:01"))); + + $this->assertEquals("Afternoon", DateFormatter::getPartOfDay(new DateTime("2018-05-27 12:00:01"))); + $this->assertEquals("Afternoon", DateFormatter::getPartOfDay(new DateTime("2018-05-27 17:59:01"))); + + // evening je od 18 do 24h? OK. + $this->assertEquals("Evening", DateFormatter::getPartOfDay(new DateTime("2018-05-27 18:01:01"))); + $this->assertEquals("Evening", DateFormatter::getPartOfDay(new DateTime("2018-05-27 23:59:01"))); + } +} \ No newline at end of file diff --git a/tests/PostServiceTest.php b/tests/PostServiceTest.php new file mode 100644 index 0000000..dd5752a --- /dev/null +++ b/tests/PostServiceTest.php @@ -0,0 +1,82 @@ +createMock(Curl::class); + + // ID noveho vytvoreneho clanku + $post_data = json_encode(array("id" => 901)); + + // konfigurace klienta + $client->method('post')->willReturn(array(201, $post_data)); // ok a vratit ID clanku na 901 + + // vytvorit postService s Mock clientem + $postService = new PostService($client); + + // test vysledku jednotlivych volani + $this->assertEquals(901, $postService->createPost(array("fdsaf", "fdafad"))); + } + + + /** + * Testuji runtime exception z jineho kodu nez 201. + * + * @throws ReflectionException + */ + public function testCreatePostRuntimeException():void + { + $this->expectException(RuntimeException::class); + + // udelam moc clienta + $client = $this->createMock(Curl::class); + + // ID noveho vytvoreneho clanku + $post_data = json_encode(array("id" => 901)); + + // konfigurace klienta + $client->method('post')->willReturn(array(404, $post_data)); // chyba - vratit cokoliv nez 201 + + // vytvorit postService s Mock clientem + $postService = new PostService($client); + + // test vysledku jednotlivych volani + $postService->createPost(array("fdsaf", "fdafad")); + } + + + /** + * Prazdny vstup by nemel vadit. + * + * @throws ReflectionException + */ + public function testCreatePostEmptyInput():void + { + // udelam moc clienta + $client = $this->createMock(Curl::class); + + // ID noveho vytvoreneho clanku + $post_data = json_encode(array("id" => 901)); + + // konfigurace klienta + $client->method('post')->willReturn(array(201, $post_data)); // ok + + // vytvorit postService s Mock clientem + $postService = new PostService($client); + + // test vysledku jednotlivych volani + $this->assertEquals(901, $postService->createPost(array())); + } +} +