$I->wantTo('test the code')
UnitTester $I
ApiTester $I
Codeception\Util\HttpCode::OK
Codeception\Util\Stub::make(MyApi::class)
UnitTester $I
$I->wantTo('Test only one unit of code')
$I->assertTrue($unit->isWorking());
$I->assertContains('hello', $greeting);
$I->assertEquals(5, $unit->getCount());
"Talk is cheap. Show me the code."~ Linus Torvalds
public function testConstructor(UnitTester $I)
{
$exception = new \Exception();
$I->assertInstanceOf(\Exception::class, $exception);
}
public function testGetMessage(UnitTester $I)
{
$message = 'foobar';
$exception = new \Exception($message);
$I->assertEquals($message, $exception->getMessage());
}
/**
* @example ["de", "male", "Doe", "Sehr geehrter Herr Doe,"]
* @example ["en", "male", "Doe", "Dear Mr Doe"]
* @example ["de", "female", "Doe", "Sehr geehrte Frau Doe,"]
* @example ["en", "female", "Doe", "Dear Ms Doe"]
*/
public function testGetSalutation(UnitTester $I, Example $example)
{
$salutation = MyMailHelper::getSalutation(
$example[0],
$example[1],
$example[2]
);
$I->assertEquals($example[3], $salutation);
}
public function testExceptionOnInvalidLanguage(UnitTester $I)
{
// Give it either just a class name
$I->expectThrowable(
\InvalidArgumentException::class,
function () {
MyMailHelper::getSalutation('foobar', 'male', 'Test');
}
);
// Or an instance of a Throwable
$I->expectThrowable(
new \InvalidArgumentException('language not implemented'),
function () {
MyMailHelper::getSalutation('foobar', 'male', 'Test');
}
);
}
FunctionalTester $I
$I->wantTo('make sure the application works as expected')
$_REQUEST
/$_GET
/$_POST
FunctionalTester $I
$I->haveInRepository(Client::class, ['name' => $testName])
$I->seeInRepository(Client::class, ['id' => $testId])
public function _before()
{
$I->haveInRepository(
User::class,
[
'username' => 'foobar',
'mail' => 'foo.bar@example.com',
'password' => Password::encrypt('test'),
]
);
}
/**
* @example ["foobar", "test"]
* @example ["foo.bar@example.com", "test"]
*/
public function testLogin(FunctionalTester $I, Example $example)
{
$I->amOnPage('/');
$I->seeResponseCodeIs(HttpCode::OK);
$I->seeCurrentRouteIs('login');
$I->submitForm(
'login',
[
'_username' => $example[0],
'_password' => $example[1],
]
);
$I->seeResponseCodeIs(HttpCode::OK);
$I->see('Hello foobar!');
$I->see('Logout');
}
AcceptanceTester $I
$I->wantTo('click around in the browser')
/**
* @example ["https://google.com"]
* @example ["https://duckduckgo.com"]
*/
public function testSearchForCodeception(
AcceptanceTester $I,
Example $example
)
{
$I->amOnUrl($example[0]);
$I->fillField(['name' => 'q'], 'Codeception');
$I->pressKey(['name' => 'q'], WebDriverKeys::ENTER);
$I->see('Codeception', 'a');
$I->seeElement('a', ['href' => 'https://codeception.com/']);
}
ApiTester $I
$I->wantTo('send HTTP requests to the API')
/**
* @example ["de", "Germany"]
* @example ["us", "United States of America"]
*/
public function testCountrySearchByISOAlpha2(
ApiTester $I,
Example $example
) {
$I->sendGET(
'https://restcountries.eu/rest/v2/alpha/'.$example[0]
);
$I->seeResponseCodeIs(HttpCode::OK);
$I->seeResponseIsJson();
$I->seeResponseMatchesJsonType([
'name' => 'string',
'population' => 'integer',
]);
$I->seeResponseContainsJson([
'name' => $example[1]
]);
}
public function testErrorOnInvalidISOAlpha2(ApiTester $I)
{
$I->sendGET('/alpha/xyz');
$I->seeResponseCodeIs(HttpCode::NOT_FOUND);
$response = json_decode($I->grabResponse(), true);
$I->assertEquals('Not Found', $response['message']);
codecept_debug($response);
}
public function testErrorOnPost(ApiTester $I)
{
$I->haveHttpHeader('Content-Type', 'application/json');
$I->sendPOST('/alpha/de', ['nope' => 'nope']);
$I->seeResponseCodeIs(HttpCode::METHOD_NOT_ALLOWED);
}
$ composer require --dev codeception/codeception
$ vendor/bin/codecept bootstrap
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
codeception.yml
# Codeception Test Suite Configuration
#
# Suite for unit or integration tests.
actor: UnitTester
modules:
enabled:
- Asserts
- \Helper\Unit
tests/unit.suite.yml
$ vendor/bin/codecept generate:suite api
$ vendor/bin/codecept generate:suite api
Helper \Helper\Api was created in ~/example/tests/_support/Helper/Api.php
Actor ApiTester was created in ~/example/tests/_support/ApiTester.php
Suite config api.suite.yml was created.
Next steps:
1. Edit api.suite.yml to enable modules for this suite
2. Create first test with generate:cest testName ( or test|cept) command
3. Run tests of this suite with codecept run api command
Suite api generated
actor: ApiTester
modules:
enabled:
- \Helper\Api
- Asserts
- REST:
url: https://restcountries.eu/rest/v2
depends: PhpBrowser
part: Json
tests/api.suite.yml
$ vendor/bin/codecept build
actor: FunctionalTester
modules:
enabled:
- \Helper\Functional
- Asserts
- Symfony:
app_path: 'src'
environment: 'test'
- Doctrine2:
depends: Symfony
tests/functional.suite.yml
$ vendor/bin/codecept build
$ vendor/bin/codecept generate:cest suite class
$ vendor/bin/codecept generate:cest unit "App\MyMailHelper"
$ vendor/bin/codecept generate:cest functional Controller/LoginCest.php
$ vendor/bin/codecept generate:cest acceptance Login
class LoginCest
{
public function _before(AcceptanceTester $I)
{
}
// tests
public function tryToTest(AcceptanceTester $I)
{
}
}
⇒ code will be cleaner & simpler & easier to maintain
$ vendor/bin/codecept run
$ vendor/bin/codecept run unit
$ vendor/bin/codecept run tests/unit/ExceptionCest.php
codecept_debug()
$I->pause()
[...]
coverage:
enabled: true
include:
- src/*
codeception.yml
$ vendor/bin/codecept run --coverage --coverage-html --coverage-xml
Codeception/Codeception
codeception.com
@codeception
Hints: codecept bootstrap/g:cest/run, Stub, Example, $I->expectThrowable()github.com/burned42/codeception-challenge