Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/DateFormatter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace IW\Workshop;

Expand All @@ -9,11 +10,16 @@ class DateFormatter
/**
* Get current part of the day
*
* @param $dateTime
* @return string
*/
public function getPartOfDay() : string
public function getPartOfDay($dateTime = null) : string
{
$dateTime = new DateTime();
// default param, v parametru nemuze byt volani fce
if (!isset($dateTime)) {
$dateTime = new DateTime();
}

$currentHour = $dateTime->format('G');

if ($currentHour >= 0 && $currentHour < 6)
Expand Down
38 changes: 38 additions & 0 deletions tests/CalculatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use PHPUnit\Framework\TestCase;
use IW\Workshop\Calculator;

class CalculatorTest extends TestCase
{

public function testAdd(): void
{
$this->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)
);
}
}
24 changes: 24 additions & 0 deletions tests/DateFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use PHPUnit\Framework\TestCase;
use IW\Workshop\DateFormatter;

class DateFormatterTest extends TestCase
{

public function testGetPartOfDay(): void
{
$this->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")));
}
}
82 changes: 82 additions & 0 deletions tests/PostServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

use PHPUnit\Framework\TestCase;
use IW\Workshop\Client\Curl;
use IW\Workshop\PostService;

class PostServiceTest extends TestCase
{

/**
* Happy day test, kdy vse dopadne dobre.
* @throws ReflectionException
*/
public function testCreatePostHappyDay():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 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()));
}
}