Skip to content

Commit 8190168

Browse files
committed
test: Add more tests for a more complete coverage sample
1 parent af67fad commit 8190168

File tree

11 files changed

+332
-35
lines changed

11 files changed

+332
-35
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Codeception_Basic\Tests\unit\Covered;
4+
5+
final class CalculatorProvider
6+
{
7+
public static function provideAdditions(): iterable
8+
{
9+
yield [2, 3, 5];
10+
yield [-5, 5, 0];
11+
yield 'with a key' => [-3, -7, -10];
12+
yield 'with a key with (\'"#::&) special characters' => [-5, -5, -10];
13+
}
14+
}

tests/e2e/Codeception_With_Suite_Overridings/expected-output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ Escaped: 6
88
Timed Out: 0
99
Skipped: 0
1010
Ignored: 0
11-
Not Covered: 0
11+
Not Covered: 0
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Codeception_With_Suite_Overridings;
4+
5+
/**
6+
* Inherited Methods
7+
* @method void wantToTest($text)
8+
* @method void wantTo($text)
9+
* @method void execute($callable)
10+
* @method void expectTo($prediction)
11+
* @method void expect($prediction)
12+
* @method void amGoingTo($argumentation)
13+
* @method void am($role)
14+
* @method void lookForwardTo($achieveValue)
15+
* @method void comment($description)
16+
* @method void pause()
17+
*
18+
* @SuppressWarnings(PHPMD)
19+
*/
20+
class FunctionalTester extends \Codeception\Actor
21+
{
22+
use _generated\FunctionalTesterActions;
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Codeception_With_Suite_Overridings\Helper;
4+
5+
class Functional extends \Codeception\Module
6+
{
7+
}

tests/e2e/Codeception_With_Suite_Overridings/tests/acceptance/CalculatorContext.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public function iCheckIfIsPositive(string $number): void
6262
$this->result = $this->calculator->isPositive((int) $number);
6363
}
6464

65+
#[When('/^I compute the absolute value of (-?\d+)$/')]
66+
public function iComputeTheAbsoluteValueOf(string $number): void
67+
{
68+
$this->result = $this->calculator->absolute((int) $number);
69+
}
70+
6571
#[Then('the result should be :expected')]
6672
public function theResultShouldBe(string $expected): void
6773
{

tests/e2e/Codeception_With_Suite_Overridings/tests/acceptance/calculator.feature

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,17 @@ Feature: Calculator
3838
| 5 | true |
3939
| 0 | true |
4040
| -5 | false |
41+
42+
Scenario Outline: Computing absolute value with label "<label>"
43+
Given I have a calculator
44+
When I compute the absolute value of <number>
45+
Then the result should be <expected>
46+
47+
Examples:
48+
| label | number | expected |
49+
| 42 | 5 | 5 |
50+
| positive number | 10 | 10 |
51+
| negative number | -7 | 7 |
52+
| zero | 0 | 0 |
53+
| with special chars ('"#::&) | -15 | 15 |
54+
| another "quoted" value | -1 | 1 |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Codeception Test Suite Configuration
2+
#
3+
# Suite for functional tests using Cest format.
4+
5+
actor: FunctionalTester
6+
modules:
7+
enabled:
8+
- Asserts
9+
- \Codeception_With_Suite_Overridings\Helper\Functional
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace Codeception_With_Suite_Overridings\Tests\functional;
4+
5+
use Codeception\Attribute\Examples;
6+
use Codeception\Example;
7+
use Codeception_With_Suite_Overridings\Covered\Calculator;
8+
use Codeception_With_Suite_Overridings\FunctionalTester;
9+
10+
class CalculatorCest
11+
{
12+
private Calculator $calculator;
13+
14+
public function _before(FunctionalTester $I): void
15+
{
16+
$this->calculator = new Calculator();
17+
}
18+
19+
/**
20+
* @dataProvider additionProvider
21+
*/
22+
public function testAddition(FunctionalTester $I, Example $example): void
23+
{
24+
$I->wantTo('verify addition works correctly');
25+
26+
$I->assertEquals($example['expected'], $this->calculator->add($example['a'], $example['b']));
27+
}
28+
29+
protected function additionProvider(): array
30+
{
31+
return [
32+
['a' => 2, 'b' => 3, 'expected' => 5],
33+
['a' => -5, 'b' => 5, 'expected' => 0],
34+
'with a key' => ['a' => -3, 'b' => -7, 'expected' => -10],
35+
'with a key with (\'"#::&|) special characters' => ['a' => -5, 'b' => -5, 'expected' => -10],
36+
];
37+
}
38+
39+
#[Examples(3, 2, 1)]
40+
#[Examples(-5, 5, -10)]
41+
#[Examples('with a key', -3, -7, 4)]
42+
#[Examples('with a key with (\'"#::&|) special characters', 5, 5, 0)]
43+
public function testSubtraction(FunctionalTester $I, Example $example): void
44+
{
45+
$I->wantTo('verify subtraction works correctly');
46+
47+
if (count($example) === 3) {
48+
$I->assertEquals($example[2], $this->calculator->subtract($example[0], $example[1]));
49+
} else {
50+
// Named example: first element is the label
51+
$I->assertEquals($example[3], $this->calculator->subtract($example[1], $example[2]));
52+
}
53+
}
54+
55+
public function testMultiplication(FunctionalTester $I): void
56+
{
57+
$I->wantTo('verify multiplication works correctly');
58+
59+
$I->assertEquals(6, $this->calculator->multiply(2, 3));
60+
$I->assertEquals(-25, $this->calculator->multiply(-5, 5));
61+
$I->assertEquals(21, $this->calculator->multiply(-3, -7));
62+
$I->assertEquals(0, $this->calculator->multiply(5, 0));
63+
}
64+
65+
public function testDivision(FunctionalTester $I): void
66+
{
67+
$I->wantTo('verify division works correctly');
68+
69+
$I->assertEquals(2.0, $this->calculator->divide(6, 3));
70+
$I->assertEquals(-1.0, $this->calculator->divide(-5, 5));
71+
$I->assertEquals(2.5, $this->calculator->divide(5, 2));
72+
}
73+
74+
public function testDivisionByZeroThrowsException(FunctionalTester $I): void
75+
{
76+
$I->wantTo('verify division by zero throws an exception');
77+
78+
$I->expectThrowable(
79+
new \InvalidArgumentException('Division by zero'),
80+
fn() => $this->calculator->divide(5, 0)
81+
);
82+
}
83+
84+
public function testIsPositive(FunctionalTester $I): void
85+
{
86+
$I->wantTo('verify isPositive identifies positive numbers correctly');
87+
88+
$I->assertTrue($this->calculator->isPositive(5));
89+
$I->assertTrue($this->calculator->isPositive(0));
90+
$I->assertFalse($this->calculator->isPositive(-5));
91+
}
92+
93+
public function testAbsolute(FunctionalTester $I): void
94+
{
95+
$I->wantTo('verify absolute value works correctly');
96+
97+
$I->assertEquals(5, $this->calculator->absolute(5));
98+
$I->assertEquals(5, $this->calculator->absolute(-5));
99+
$I->assertEquals(0, $this->calculator->absolute(0));
100+
}
101+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Codeception_With_Suite_Overridings\Tests\functional;
4+
5+
use Codeception_With_Suite_Overridings\Covered\UserService;
6+
use Codeception_With_Suite_Overridings\FunctionalTester;
7+
8+
class UserServiceCest
9+
{
10+
private UserService $service;
11+
12+
public function _before(FunctionalTester $I): void
13+
{
14+
$this->service = new UserService();
15+
}
16+
17+
public function testAddUser(FunctionalTester $I): void
18+
{
19+
$I->wantTo('add a user successfully');
20+
21+
$I->assertTrue($this->service->addUser('John Doe', 'john@example.com'));
22+
$I->assertEquals(1, $this->service->getUserCount());
23+
$I->assertTrue($this->service->hasLogs());
24+
}
25+
26+
public function testAddUserWithEmptyNameFails(FunctionalTester $I): void
27+
{
28+
$I->wantTo('verify adding user with empty name fails');
29+
30+
$I->assertFalse($this->service->addUser('', 'john@example.com'));
31+
$I->assertEquals(0, $this->service->getUserCount());
32+
}
33+
34+
public function testAddUserWithEmptyEmailFails(FunctionalTester $I): void
35+
{
36+
$I->wantTo('verify adding user with empty email fails');
37+
38+
$I->assertFalse($this->service->addUser('John Doe', ''));
39+
$I->assertEquals(0, $this->service->getUserCount());
40+
}
41+
42+
public function testAddDuplicateUserFails(FunctionalTester $I): void
43+
{
44+
$I->wantTo('verify adding duplicate user fails');
45+
46+
$this->service->addUser('John Doe', 'john@example.com');
47+
$I->assertFalse($this->service->addUser('Jane Doe', 'john@example.com'));
48+
$I->assertEquals(1, $this->service->getUserCount());
49+
}
50+
51+
public function testRemoveUser(FunctionalTester $I): void
52+
{
53+
$I->wantTo('remove a user successfully');
54+
55+
$this->service->addUser('John Doe', 'john@example.com');
56+
$I->assertTrue($this->service->removeUser('john@example.com'));
57+
$I->assertEquals(0, $this->service->getUserCount());
58+
}
59+
60+
public function testRemoveNonExistentUserFails(FunctionalTester $I): void
61+
{
62+
$I->wantTo('verify removing non-existent user fails');
63+
64+
$I->assertFalse($this->service->removeUser('john@example.com'));
65+
}
66+
67+
public function testGetUser(FunctionalTester $I): void
68+
{
69+
$I->wantTo('retrieve a user by email');
70+
71+
$this->service->addUser('John Doe', 'john@example.com');
72+
$user = $this->service->getUser('john@example.com');
73+
74+
$I->assertIsArray($user);
75+
$I->assertEquals('John Doe', $user['name']);
76+
$I->assertEquals('john@example.com', $user['email']);
77+
}
78+
79+
public function testGetNonExistentUserReturnsNull(FunctionalTester $I): void
80+
{
81+
$I->wantTo('verify getting non-existent user returns null');
82+
83+
$I->assertNull($this->service->getUser('john@example.com'));
84+
}
85+
86+
public function testUserExists(FunctionalTester $I): void
87+
{
88+
$I->wantTo('check if user exists');
89+
90+
$this->service->addUser('John Doe', 'john@example.com');
91+
$I->assertTrue($this->service->userExists('john@example.com'));
92+
$I->assertFalse($this->service->userExists('jane@example.com'));
93+
}
94+
95+
public function testClearLogs(FunctionalTester $I): void
96+
{
97+
$I->wantTo('clear logs');
98+
99+
$this->service->addUser('John Doe', 'john@example.com');
100+
$this->service->clearLogs();
101+
$I->assertFalse($this->service->hasLogs());
102+
}
103+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Codeception_With_Suite_Overridings\Tests\unit\Covered;
4+
5+
final class CalculatorProvider
6+
{
7+
public static function provideAdditions(): iterable
8+
{
9+
yield [2, 3, 5];
10+
yield [-5, 5, 0];
11+
yield 'with a key' => [-3, -7, -10];
12+
yield 'with a key with (\'"#::&|) special characters' => [-5, -5, -10];
13+
}
14+
}

0 commit comments

Comments
 (0)