From 52436e65498cde24375c96057c19a6c52199acb8 Mon Sep 17 00:00:00 2001 From: lebaongan Date: Wed, 19 Aug 2020 15:53:05 +0700 Subject: [PATCH] Issue #5 Test C2 --- tests/C2/DumpExampleTest.php | 29 +++++++ tests/C2/ProcessRunnerTest.php | 121 ++++++++++++++++++++++++++++ tests/C2/UsernameValidationTest.php | 68 ++++++++++++++++ 3 files changed, 218 insertions(+) create mode 100644 tests/C2/DumpExampleTest.php create mode 100644 tests/C2/ProcessRunnerTest.php create mode 100644 tests/C2/UsernameValidationTest.php diff --git a/tests/C2/DumpExampleTest.php b/tests/C2/DumpExampleTest.php new file mode 100644 index 0000000..c76a561 --- /dev/null +++ b/tests/C2/DumpExampleTest.php @@ -0,0 +1,29 @@ +examine(true); + + $expectedResult = 1; + $this->assertEquals($expectedResult, $actualResult); + } + + public function test_return_0_when_input_false() + { + $sut = new DumpExample; + + // Cause by division 0 + $this->expectError(); + + $sut->examine(false); + } +} diff --git a/tests/C2/ProcessRunnerTest.php b/tests/C2/ProcessRunnerTest.php new file mode 100644 index 0000000..1b0afba --- /dev/null +++ b/tests/C2/ProcessRunnerTest.php @@ -0,0 +1,121 @@ +run(12, 3); + + $expectedResult = 2; + $this->assertEquals($expectedResult, $actualResult); + $this->assertTrue($sut->wasProcess1Called()); + $this->assertFalse($sut->wasProcess2Called()); + $this->assertTrue($sut->wasProcess3Called()); + $this->assertFalse($sut->wasProcess4Called()); + } + + public function test_return_2_WHEN_input1_gt_10_input2_div_by_2_input3_not_div_by_3() + { + $sut = new ProcessRunner; + + $actualResult = $sut->run(12, 2); + + $expectedResult = 2; + $this->assertEquals($expectedResult, $actualResult); + $this->assertTrue($sut->wasProcess1Called()); + $this->assertFalse($sut->wasProcess2Called()); + $this->assertTrue($sut->wasProcess3Called()); + $this->assertFalse($sut->wasProcess4Called()); + } + + public function test_return_2_WHEN_input1_gt_10_input2_not_div_by_2_input3_div_by_3() + { + $sut = new ProcessRunner; + + $actualResult = $sut->run(11, 3); + + $expectedResult = 2; + $this->assertEquals($expectedResult, $actualResult); + $this->assertTrue($sut->wasProcess1Called()); + $this->assertFalse($sut->wasProcess2Called()); + $this->assertTrue($sut->wasProcess3Called()); + $this->assertFalse($sut->wasProcess4Called()); + } + + public function test_return_2_WHEN_input1_gt_10_input2_not_div_by_2_input3_not_div_by_3() + { + $sut = new ProcessRunner; + + $actualResult = $sut->run(11, 2); + + $expectedResult = 2; + $this->assertEquals($expectedResult, $actualResult); + $this->assertTrue($sut->wasProcess1Called()); + $this->assertFalse($sut->wasProcess2Called()); + $this->assertFalse($sut->wasProcess3Called()); + $this->assertTrue($sut->wasProcess4Called()); + } + + public function test_return_2_WHEN_input1_lt_10_input2_div_by_2_input3_div_by_3() + { + $sut = new ProcessRunner; + + $actualResult = $sut->run(2, 3); + + $expectedResult = 2; + $this->assertEquals($expectedResult, $actualResult); + $this->assertFalse($sut->wasProcess1Called()); + $this->assertTrue($sut->wasProcess2Called()); + $this->assertTrue($sut->wasProcess3Called()); + $this->assertFalse($sut->wasProcess4Called()); + } + + public function test_return_2_WHEN_input1_lt_10_input2_div_by_2_input3_not_div_by_3() + { + $sut = new ProcessRunner; + + $actualResult = $sut->run(2, 2); + + $expectedResult = 2; + $this->assertEquals($expectedResult, $actualResult); + $this->assertFalse($sut->wasProcess1Called()); + $this->assertTrue($sut->wasProcess2Called()); + $this->assertTrue($sut->wasProcess3Called()); + $this->assertFalse($sut->wasProcess4Called()); + } + + public function test_return_2_WHEN_input1_lt_10_input2_not_div_by_2_input3_div_by_3() + { + $sut = new ProcessRunner; + + $actualResult = $sut->run(3, 3); + + $expectedResult = 2; + $this->assertEquals($expectedResult, $actualResult); + $this->assertFalse($sut->wasProcess1Called()); + $this->assertTrue($sut->wasProcess2Called()); + $this->assertTrue($sut->wasProcess3Called()); + $this->assertFalse($sut->wasProcess4Called()); + } + + public function test_return_2_WHEN_input1_lt_10_input2_not_div_by_2_input3_not_div_by_3() + { + $sut = new ProcessRunner; + + $actualResult = $sut->run(3, 2); + + $expectedResult = 2; + $this->assertEquals($expectedResult, $actualResult); + $this->assertFalse($sut->wasProcess1Called()); + $this->assertTrue($sut->wasProcess2Called()); + $this->assertFalse($sut->wasProcess3Called()); + $this->assertTrue($sut->wasProcess4Called()); + } +} diff --git a/tests/C2/UsernameValidationTest.php b/tests/C2/UsernameValidationTest.php new file mode 100644 index 0000000..e21176f --- /dev/null +++ b/tests/C2/UsernameValidationTest.php @@ -0,0 +1,68 @@ +isValid(''); + + $this->assertFalse($actualResult); + $this->assertEquals('Minimum length is 1', $sut->getMessage()); + } + + public function test_return_false_when_user_name_gt_max_length() + { + $sut = new UsernameValidation; + + $actualResult = $sut->isValid('012345678901234567890'); + + $this->assertFalse($actualResult); + $this->assertEquals('Maximum length is 20', $sut->getMessage()); + } + + public function test_return_false_when_user_name_not_allow_dash_begin_or_end() + { + $sut = new UsernameValidation; + + $actualResult = $sut->isValid('-1234'); + + $this->assertFalse($actualResult); + $this->assertEquals('- cannot appear at begin or end of name', $sut->getMessage()); + } + + public function test_return_false_when_user_name_not_allow_double_dash() + { + $sut = new UsernameValidation; + + $actualResult = $sut->isValid('1--2'); + + $this->assertFalse($actualResult); + $this->assertEquals('Only single - is allowed', $sut->getMessage()); + } + + public function test_return_false_when_user_name_not_allow_invalid_character() + { + $sut = new UsernameValidation; + + $actualResult = $sut->isValid('@'); + + $this->assertFalse($actualResult); + $this->assertEquals('Invalid character. Use only letters, digits and -', $sut->getMessage()); + } + + public function test_return_true_when_user_name_valid() + { + $sut = new UsernameValidation; + + $actualResult = $sut->isValid('lethibaongan'); + + $this->assertTrue($actualResult); + } +}