Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codeception 5 Support #65

Merged
merged 5 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
php: [5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1]
php: [8.0, 8.1]

steps:
- name: Checkout code
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.idea/
/nbproject/
/vendor/
/composer.lock
tests/_support
Expand Down
18 changes: 11 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
"name":"Michael Bodnarchuk"
}
],
"minimum-stability": "RC",
"minimum-stability": "dev",
"require": {
"php": ">=5.6.0 <=8.1 | ~8.1.0",
"codeception/lib-innerbrowser": "^1.0",
"codeception/codeception": "^4.0"
"php": "^8.0",
"codeception/codeception": "^5.0.0-alpha2",
"codeception/lib-innerbrowser": "^3.0"
},
"require-dev": {
"yiisoft/yii2": "dev-master",
"yiisoft/yii2-app-advanced": "dev-master",
"codeception/verify": "<2",
"phpunit/phpunit": "dev-master as 9.5.99",
samdark marked this conversation as resolved.
Show resolved Hide resolved
"codeception/verify": "^2.2",
"codemix/yii2-localeurls": "^1.7",
"codeception/module-asserts": "^1.3",
"codeception/module-filesystem": "^1.0"
"codeception/module-asserts": "^3.0",
"codeception/module-filesystem": "^3.0"
},
"autoload":{
"classmap": ["src/"]
Expand All @@ -40,6 +41,9 @@
]
},
"config": {
"allow-plugins": {
"yiisoft/yii2-composer": true
},
"classmap-authoritative": true
},
"repositories": [
Expand Down
4 changes: 2 additions & 2 deletions src/Codeception/Lib/Connector/Yii2.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function resetApplication($closeSession = true)
}
Yii::$app = null;
\yii\web\UploadedFile::reset();
if (method_exists(\yii\base\Event::className(), 'offAll')) {
if (method_exists(\yii\base\Event::class, 'offAll')) {
\yii\base\Event::offAll();
}
Yii::setLogger(null);
Expand Down Expand Up @@ -306,7 +306,7 @@ public function startApp()
*
* @return \Symfony\Component\BrowserKit\Response
*/
public function doRequest($request)
public function doRequest(object $request)
{
$_COOKIE = $request->getCookies();
$_SERVER = $request->getServer();
Expand Down
2 changes: 1 addition & 1 deletion src/Codeception/Lib/Connector/Yii2/FixturesStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function fixtures()
public function globalFixtures()
{
return [
InitDbFixture::className()
InitDbFixture::class
];
}
}
75 changes: 39 additions & 36 deletions src/Codeception/Module/Yii2.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
use Codeception\Lib\Interfaces\PartedModule;
use Codeception\TestInterface;
use Codeception\Util\Debug;
use Symfony\Component\DomCrawler\Crawler as SymfonyCrawler;
use Yii;
use yii\base\Event;
use yii\db\ActiveRecordInterface;
use yii\db\Connection;
use yii\db\QueryInterface;
use yii\db\Transaction;
use yii\helpers\Url;

/**
* This module provides integration with [Yii framework](http://www.yiiframework.com/) (2.0).
Expand Down Expand Up @@ -130,7 +132,7 @@
*
* ```php
* <?php
* $I->haveFixtures(['posts' => PostsFixture::className()]);
* $I->haveFixtures(['posts' => PostsFixture::class]);
* ```
*
* or, if you need to load fixtures before the test, you
Expand All @@ -141,7 +143,7 @@
* // inside Cest file or Codeception\TestCase\Unit
* public function _fixtures()
* {
* return ['posts' => PostsFixture::className()]
* return ['posts' => PostsFixture::class]
* }
* ```
*
Expand All @@ -152,7 +154,6 @@
*
* ```php
* <?php
* $I->amOnPage(['site/view','page'=>'about']);
* $I->amOnPage('index-test.php?site/index');
* $I->amOnPage('http://localhost/index-test.php?site/index');
* $I->sendAjaxPostRequest(['/user/update', 'id' => 1], ['UserForm[name]' => 'G.Hopper']);
Expand All @@ -171,7 +172,7 @@ class Yii2 extends Framework implements ActiveRecord, MultiSession, PartedModule
* Application config file must be set.
* @var array
*/
protected $config = [
protected array $config = [
'fixturesMethod' => '_fixtures',
'cleanup' => true,
'ignoreCollidingDSN' => false,
Expand All @@ -186,12 +187,12 @@ class Yii2 extends Framework implements ActiveRecord, MultiSession, PartedModule
'applicationClass' => null,
];

protected $requiredFields = ['configFile'];
protected array $requiredFields = ['configFile'];

/**
* @var Yii2Connector\FixturesStore[]
*/
public $loadedFixtures = [];
public array $loadedFixtures = [];

/**
* Helper to manage database connections
Expand Down Expand Up @@ -256,7 +257,7 @@ private function initServerGlobal()
]);
}

protected function validateConfig()
protected function validateConfig(): void
{
parent::validateConfig();

Expand Down Expand Up @@ -402,7 +403,7 @@ protected function rollbackTransactions()
}
}

public function _parts()
public function _parts(): array
{
return ['orm', 'init', 'fixtures', 'email'];
}
Expand Down Expand Up @@ -443,9 +444,9 @@ public function amLoggedInAs($user)
* ```php
* <?php
* $I->haveFixtures([
* 'posts' => PostsFixture::className(),
* 'posts' => PostsFixture::class,
* 'user' => [
* 'class' => UserFixture::className(),
* 'class' => UserFixture::class,
* 'dataFile' => '@tests/_data/models/user.php',
* ],
* ]);
Expand All @@ -461,7 +462,7 @@ public function amLoggedInAs($user)
* public function _fixtures(){
* return [
* 'user' => [
* 'class' => UserFixture::className(),
* 'class' => UserFixture::class,
* 'dataFile' => codecept_data_dir() . 'user.php'
* ]
* ];
Expand Down Expand Up @@ -511,7 +512,7 @@ function ($fixturesStore) {
*
* ```php
* <?php
* $I->haveFixtures(['users' => UserFixture::className()]);
* $I->haveFixtures(['users' => UserFixture::class]);
*
* $users = $I->grabFixture('users');
*
Expand Down Expand Up @@ -577,7 +578,7 @@ public function haveRecord($model, $attributes = [])
* @param array $attributes
* @part orm
*/
public function seeRecord($model, $attributes = [])
public function seeRecord(string $model, array $attributes = []): void
{
$record = $this->findRecord($model, $attributes);
if (!$record) {
Expand All @@ -597,7 +598,7 @@ public function seeRecord($model, $attributes = [])
* @param array $attributes
* @part orm
*/
public function dontSeeRecord($model, $attributes = [])
public function dontSeeRecord(string $model, array $attributes = []): void
{
$record = $this->findRecord($model, $attributes);
$this->debugSection($model, json_encode($record));
Expand All @@ -618,7 +619,7 @@ public function dontSeeRecord($model, $attributes = [])
* @return mixed
* @part orm
*/
public function grabRecord($model, $attributes = [])
public function grabRecord(string $model, array $attributes = []): mixed
{
return $this->findRecord($model, $attributes);
}
Expand All @@ -628,7 +629,7 @@ public function grabRecord($model, $attributes = [])
* @param array $attributes
* @return mixed
*/
protected function findRecord($model, $attributes = [])
protected function findRecord(string $model, array $attributes = []): mixed
{
if (!class_exists($model)) {
throw new \RuntimeException("Class $model does not exist");
Expand Down Expand Up @@ -660,10 +661,14 @@ protected function findRecord($model, $attributes = [])
* @param string $route A route
* @param array $params Additional route parameters
*/
public function amOnRoute($route, array $params = [])
public function amOnRoute(string $route, array $params = []): void
{
if (Yii::$app->controller === null) {
$route = "/{$route}";
}

array_unshift($params, $route);
$this->amOnPage($params);
$this->amOnPage(Url::to($params));
}

/**
Expand All @@ -675,13 +680,11 @@ public function amOnRoute($route, array $params = [])
* $I->amOnPage('/');
* // opens /register page
* $I->amOnPage('/register');
* // opens customer view page for id 25
* $I->amOnPage(['customer/view', 'id' => 25]);
* ```
*
* @param string|array $page the URI or route in array format
*/
public function amOnPage($page)
public function amOnPage(string $page): void
{
parent::amOnPage($page);
}
Expand All @@ -698,7 +701,7 @@ public function amOnPage($page)
* @param bool $changeHistory
* @return mixed
*/
protected function clientRequest($method, $uri, array $parameters = [], array $files = [], array $server = [], $content = null, $changeHistory = true)
protected function clientRequest(string $method, string $uri, array $parameters = [], array $files = [], array $server = [], string $content = null, bool $changeHistory = true): SymfonyCrawler
{
return parent::clientRequest($method, $this->client->createUrl($uri), $parameters, $files, $server, $content, $changeHistory);
}
Expand All @@ -717,7 +720,7 @@ protected function clientRequest($method, $uri, array $parameters = [], array $f
* @throws \Codeception\Exception\ModuleException
* @deprecated in your tests you can use \Yii::$app directly.
*/
public function grabComponent($component)
public function grabComponent(mixed $component)
{
try {
return $this->client->getComponent($component);
Expand All @@ -742,7 +745,7 @@ public function grabComponent($component)
* @throws \Codeception\Exception\ModuleException
* @part email
*/
public function seeEmailIsSent($num = null)
public function seeEmailIsSent(int $num = null): void
{
if ($num === null) {
$this->assertNotEmpty($this->grabSentEmails(), 'emails were sent');
Expand All @@ -756,7 +759,7 @@ public function seeEmailIsSent($num = null)
*
* @part email
*/
public function dontSeeEmailIsSent()
public function dontSeeEmailIsSent(): void
{
$this->seeEmailIsSent(0);
}
Expand All @@ -777,7 +780,7 @@ public function dontSeeEmailIsSent()
* @return array
* @throws \Codeception\Exception\ModuleException
*/
public function grabSentEmails()
public function grabSentEmails(): array
{
try {
return $this->client->getEmails();
Expand All @@ -797,7 +800,7 @@ public function grabSentEmails()
* ```
* @part email
*/
public function grabLastSentEmail()
public function grabLastSentEmail(): object
{
$this->seeEmailIsSent();
$messages = $this->grabSentEmails();
Expand All @@ -811,12 +814,12 @@ public function grabLastSentEmail()
*
* @return array
*/
public function getInternalDomains()
public function getInternalDomains(): array
{
return $this->client->getInternalDomains();
}

private function defineConstants()
private function defineConstants(): void
{
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
Expand All @@ -829,7 +832,7 @@ private function defineConstants()
* @param string $val The value of the cookie
* @param array $params Additional cookie params like `domain`, `path`, `expires` and `secure`.
*/
public function setCookie($name, $val, array $params = [])
public function setCookie($name, $val, $params = [])
{
parent::setCookie($name, $this->client->hashCookieData($name, $val), $params);
}
Expand All @@ -839,15 +842,15 @@ public function setCookie($name, $val, array $params = [])
* @param string $val The value of the CSRF token
* @return string[] Returns an array containing the name of the CSRF param and the masked CSRF token.
*/
public function createAndSetCsrfCookie($val)
public function createAndSetCsrfCookie(string $val): array
{
$masked = $this->client->maskToken($val);
$name = $this->client->getCsrfParamName();
$this->setCookie($name, $val);
return [$name, $masked];
}

public function _afterSuite()
public function _afterSuite(): void
{
parent::_afterSuite();
codecept_debug('Suite done, restoring $_SERVER to original');
Expand All @@ -858,7 +861,7 @@ public function _afterSuite()
/**
* Initialize an empty session. Implements MultiSession.
*/
public function _initializeSession()
public function _initializeSession(): void
{
$this->client->removeContext();
$this->headers = [];
Expand All @@ -870,7 +873,7 @@ public function _initializeSession()
* Return the session content for future restoring. Implements MultiSession.
* @return array backup data
*/
public function _backupSession()
public function _backupSession(): array
{
if (isset(Yii::$app) && Yii::$app->session->useCustomStorage) {
throw new ModuleException($this, "Yii2 MultiSession only supports the default session backend.");
Expand All @@ -887,7 +890,7 @@ public function _backupSession()
* Restore a session. Implements MultiSession.
* @param array output of _backupSession()
*/
public function _loadSession($session)
public function _loadSession($session): void
{
$this->client->setContext($session['clientContext']);
$this->headers = $session['headers'];
Expand All @@ -906,7 +909,7 @@ public function _loadSession($session)
/**
* Close and dump a session. Implements MultiSession.
*/
public function _closeSession($session = null)
public function _closeSession($session = null): void
{
if (!$session) {
$this->_initializeSession();
Expand Down
Loading