Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Codeception_Basic\Tests\unit\Covered;

final class CalculatorProvider
{
public static function provideAdditions(): iterable
{
yield [2, 3, 5];
yield [-5, 5, 0];
yield 'with a key' => [-3, -7, -10];
yield 'with a key with (\'"#::&) special characters' => [-5, -5, -10];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Escaped: 6
Timed Out: 0
Skipped: 0
Ignored: 0
Not Covered: 0
Not Covered: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Codeception_With_Suite_Overridings;

/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method void pause()
*
* @SuppressWarnings(PHPMD)
*/
class FunctionalTester extends \Codeception\Actor
{
use _generated\FunctionalTesterActions;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Codeception_With_Suite_Overridings\Helper;

class Functional extends \Codeception\Module
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public function iCheckIfIsPositive(string $number): void
$this->result = $this->calculator->isPositive((int) $number);
}

#[When('/^I compute the absolute value of (-?\d+)$/')]
public function iComputeTheAbsoluteValueOf(string $number): void
{
$this->result = $this->calculator->absolute((int) $number);
}

#[Then('the result should be :expected')]
public function theResultShouldBe(string $expected): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,17 @@ Feature: Calculator
| 5 | true |
| 0 | true |
| -5 | false |

Scenario Outline: Computing absolute value with label "<label>"
Given I have a calculator
When I compute the absolute value of <number>
Then the result should be <expected>

Examples:
| label | number | expected |
| 42 | 5 | 5 |
| positive number | 10 | 10 |
| negative number | -7 | 7 |
| zero | 0 | 0 |
| with special chars ('"#::&) | -15 | 15 |
| another "quoted" value | -1 | 1 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Codeception Test Suite Configuration
#
# Suite for functional tests using Cest format.

actor: FunctionalTester
modules:
enabled:
- Asserts
- \Codeception_With_Suite_Overridings\Helper\Functional
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Codeception_With_Suite_Overridings\Tests\functional;

use Codeception\Attribute\Examples;
use Codeception\Example;
use Codeception_With_Suite_Overridings\Covered\Calculator;
use Codeception_With_Suite_Overridings\FunctionalTester;

class CalculatorCest
{
private Calculator $calculator;

public function _before(FunctionalTester $I): void
{
$this->calculator = new Calculator();
}

/**
* @dataProvider additionProvider
*/
public function testAddition(FunctionalTester $I, Example $example): void
{
$I->wantTo('verify addition works correctly');

$I->assertEquals($example['expected'], $this->calculator->add($example['a'], $example['b']));
}

protected function additionProvider(): array
{
return [
['a' => 2, 'b' => 3, 'expected' => 5],
['a' => -5, 'b' => 5, 'expected' => 0],
'with a key' => ['a' => -3, 'b' => -7, 'expected' => -10],
'with a key with (\'"#::&|) special characters' => ['a' => -5, 'b' => -5, 'expected' => -10],
];
}

#[Examples(3, 2, 1)]
#[Examples(-5, 5, -10)]
#[Examples('with a key', -3, -7, 4)]
#[Examples('with a key with (\'"#::&|) special characters', 5, 5, 0)]
public function testSubtraction(FunctionalTester $I, Example $example): void
{
$I->wantTo('verify subtraction works correctly');

if (count($example) === 3) {
$I->assertEquals($example[2], $this->calculator->subtract($example[0], $example[1]));
} else {
// Named example: first element is the label
$I->assertEquals($example[3], $this->calculator->subtract($example[1], $example[2]));
}
}

public function testMultiplication(FunctionalTester $I): void
{
$I->wantTo('verify multiplication works correctly');

$I->assertEquals(6, $this->calculator->multiply(2, 3));
$I->assertEquals(-25, $this->calculator->multiply(-5, 5));
$I->assertEquals(21, $this->calculator->multiply(-3, -7));
$I->assertEquals(0, $this->calculator->multiply(5, 0));
}

public function testDivision(FunctionalTester $I): void
{
$I->wantTo('verify division works correctly');

$I->assertEquals(2.0, $this->calculator->divide(6, 3));
$I->assertEquals(-1.0, $this->calculator->divide(-5, 5));
$I->assertEquals(2.5, $this->calculator->divide(5, 2));
}

public function testDivisionByZeroThrowsException(FunctionalTester $I): void
{
$I->wantTo('verify division by zero throws an exception');

$I->expectThrowable(
new \InvalidArgumentException('Division by zero'),
fn() => $this->calculator->divide(5, 0)
);
}

public function testIsPositive(FunctionalTester $I): void
{
$I->wantTo('verify isPositive identifies positive numbers correctly');

$I->assertTrue($this->calculator->isPositive(5));
$I->assertTrue($this->calculator->isPositive(0));
$I->assertFalse($this->calculator->isPositive(-5));
}

public function testAbsolute(FunctionalTester $I): void
{
$I->wantTo('verify absolute value works correctly');

$I->assertEquals(5, $this->calculator->absolute(5));
$I->assertEquals(5, $this->calculator->absolute(-5));
$I->assertEquals(0, $this->calculator->absolute(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Codeception_With_Suite_Overridings\Tests\functional;

use Codeception_With_Suite_Overridings\Covered\UserService;
use Codeception_With_Suite_Overridings\FunctionalTester;

class UserServiceCest
{
private UserService $service;

public function _before(FunctionalTester $I): void
{
$this->service = new UserService();
}

public function testAddUser(FunctionalTester $I): void
{
$I->wantTo('add a user successfully');

$I->assertTrue($this->service->addUser('John Doe', 'john@example.com'));
$I->assertEquals(1, $this->service->getUserCount());
$I->assertTrue($this->service->hasLogs());
}

public function testAddUserWithEmptyNameFails(FunctionalTester $I): void
{
$I->wantTo('verify adding user with empty name fails');

$I->assertFalse($this->service->addUser('', 'john@example.com'));
$I->assertEquals(0, $this->service->getUserCount());
}

public function testAddUserWithEmptyEmailFails(FunctionalTester $I): void
{
$I->wantTo('verify adding user with empty email fails');

$I->assertFalse($this->service->addUser('John Doe', ''));
$I->assertEquals(0, $this->service->getUserCount());
}

public function testAddDuplicateUserFails(FunctionalTester $I): void
{
$I->wantTo('verify adding duplicate user fails');

$this->service->addUser('John Doe', 'john@example.com');
$I->assertFalse($this->service->addUser('Jane Doe', 'john@example.com'));
$I->assertEquals(1, $this->service->getUserCount());
}

public function testRemoveUser(FunctionalTester $I): void
{
$I->wantTo('remove a user successfully');

$this->service->addUser('John Doe', 'john@example.com');
$I->assertTrue($this->service->removeUser('john@example.com'));
$I->assertEquals(0, $this->service->getUserCount());
}

public function testRemoveNonExistentUserFails(FunctionalTester $I): void
{
$I->wantTo('verify removing non-existent user fails');

$I->assertFalse($this->service->removeUser('john@example.com'));
}

public function testGetUser(FunctionalTester $I): void
{
$I->wantTo('retrieve a user by email');

$this->service->addUser('John Doe', 'john@example.com');
$user = $this->service->getUser('john@example.com');

$I->assertIsArray($user);
$I->assertEquals('John Doe', $user['name']);
$I->assertEquals('john@example.com', $user['email']);
}

public function testGetNonExistentUserReturnsNull(FunctionalTester $I): void
{
$I->wantTo('verify getting non-existent user returns null');

$I->assertNull($this->service->getUser('john@example.com'));
}

public function testUserExists(FunctionalTester $I): void
{
$I->wantTo('check if user exists');

$this->service->addUser('John Doe', 'john@example.com');
$I->assertTrue($this->service->userExists('john@example.com'));
$I->assertFalse($this->service->userExists('jane@example.com'));
}

public function testClearLogs(FunctionalTester $I): void
{
$I->wantTo('clear logs');

$this->service->addUser('John Doe', 'john@example.com');
$this->service->clearLogs();
$I->assertFalse($this->service->hasLogs());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Codeception_With_Suite_Overridings\Tests\unit\Covered;

final class CalculatorProvider
{
public static function provideAdditions(): iterable
{
yield [2, 3, 5];
yield [-5, 5, 0];
yield 'with a key' => [-3, -7, -10];
yield 'with a key with (\'"#::&|) special characters' => [-5, -5, -10];
}
}
Loading
Loading