Skip to content
Open
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
51 changes: 50 additions & 1 deletion tests/C0/ProcessRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,58 @@

namespace Tests\C0;

use App\ProcessRunner;
use PHPUnit\Framework\TestCase;

class ProcessRunnerTest extends TestCase
{
// TODO: write 2 tests to coverage 100% code
private $processRunner;

public function __construct()
{
parent::__construct();
$this->processRunner = new ProcessRunner();
}

public function testRunWhenIf1TrueAndIf2True()
Copy link
Copy Markdown
Member

@tuanpt-0634 tuanpt-0634 Jun 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nên coi class là Unit cần test, tức class là SUT (System Under Test). Vì thế test case sẽ là test hành vi,không nên quá quan tâm đến tên method là gì, bên trong nó implement như thế nào, khi đó có thể thay đổi cách implement nhưng hành vi không thay đổi nên không phải update lại unit test.

Tên method unit test không cần phải ngắn gọn, nên viết theo snake_case để dễ đọc hơn (có thể config phpcs để bỏ qua naming convention với các method test, xem phpcs.xml)

Có 1 cách đặt tên như thế này: Test It <expected result> When <input are ...>

=> Test It called process1 and process3 When input1 is 12 and input2 can be whatever

Suggested change
public function testRunWhenIf1TrueAndIf2True()
public function test_IT_called_process1_and_process3_WHEN_input1_is_12_input2_is_whatever()

Tên test case cho thấy rõ ràng expect là gì.

{
$result = $this->processRunner->run(12, 3);

$this->assertEquals(2, $result);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chưa đủ assert, vì không ở test case dưới https://github.com/sun7pro/phpunit-training-coverage/pull/6/files#diff-2ccad413df4fa96d3b36fd1b9389b09fR29 kết quả trả về vẫn là 2, làm sao để phân biệt?

}

public function testRunWhenIf1FalseAndIf2Fasle()
{
$result = $this->processRunner->run(7, 5);

$this->assertEquals(2, $result);
}

public function testWasProcess1Called()
{
$result = $this->processRunner->wasProcess1Called();

$this->assertFalse($result);
}

public function testWasProcess2Called()
{
$result = $this->processRunner->wasProcess2Called();

$this->assertFalse($result);
}

public function testWasProcess3Called()
{
$result = $this->processRunner->wasProcess3Called();

$this->assertFalse($result);
}

public function testWasProcess4Called()
{
$result = $this->processRunner->wasProcess4Called();

$this->assertFalse($result);
}
}