Skip to content

Commit 8a7f950

Browse files
Modernize codebase and add more code quality tools (Codeception#37)
* Added github actions for php 8.2 and 8.3 * Create dependabot.yml * Added more code quality tools --------- Co-authored-by: TavoNiievez <ganieves@outlook.com>
1 parent a972411 commit 8a7f950

File tree

24 files changed

+727
-581
lines changed

24 files changed

+727
-581
lines changed

.github/dependabot.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: 2
2+
updates:
3+
-
4+
package-ecosystem: composer
5+
directory: "/"
6+
schedule:
7+
interval: monthly
8+
versioning-strategy: auto
9+
groups:
10+
dev-dependencies:
11+
dependency-type: "development"

.github/workflows/main.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ jobs:
88

99
strategy:
1010
matrix:
11-
php: [8.0, 8.1]
11+
php: [8.1, 8.2, 8.3]
1212

1313
steps:
1414
- name: Checkout code
15-
uses: actions/checkout@v2
15+
uses: actions/checkout@v4
1616

1717
- name: Setup PHP
1818
uses: shivammathur/setup-php@v2
@@ -26,6 +26,12 @@ jobs:
2626
- name: Install dependencies
2727
run: composer install --prefer-dist --no-progress --no-interaction --no-suggest
2828

29+
- name: Execute Code Sniffer
30+
run: vendor/bin/phpcs
31+
32+
- name: Execute PHP Stan
33+
run: vendor/bin/phpstan
34+
2935
- name: Run test suite
3036
run: |
3137
php -S 127.0.0.1:8000 -t tests/data/app >/dev/null 2>&1 &

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@
1818
],
1919
"homepage": "https://codeception.com/",
2020
"require": {
21-
"php": "^8.0",
21+
"php": "^8.1",
2222
"ext-json": "*",
2323
"codeception/codeception": "*@dev",
2424
"codeception/lib-innerbrowser": "*@dev",
2525
"guzzlehttp/guzzle": "^7.4",
26-
"symfony/browser-kit": "^5.4 || ^6.0 || ^7.0"
26+
"symfony/browser-kit": "^5.4 | ^6.0 | ^7.0"
2727
},
2828
"require-dev": {
2929
"ext-curl": "*",
30+
"squizlabs/php_codesniffer": "^3.10",
31+
"phpstan/phpstan": "^1.10",
3032
"aws/aws-sdk-php": "^3.199",
31-
"codeception/module-rest": "^2.0 || *@dev"
33+
"codeception/module-rest": "^2.0 | *@dev"
3234
},
3335
"conflict": {
3436
"codeception/codeception": "<5.0",
@@ -37,7 +39,6 @@
3739
"suggest": {
3840
"codeception/phpbuiltinserver": "Start and stop PHP built-in web server for your tests"
3941
},
40-
"minimum-stability": "dev",
4142
"autoload": {
4243
"classmap": [
4344
"src/"

phpcs.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="phpcs.xsd">
3+
<description>The coding standard.</description>
4+
5+
<config name="testVersion" value="8.1-"/>
6+
7+
<file>src</file>
8+
9+
<arg name="basepath" value="./"/>
10+
<arg name="colors"/>
11+
<arg name="parallel" value="5"/>
12+
<arg value="np"/>
13+
14+
<rule ref="Internal.Tokenizer.Exception">
15+
<type>error</type>
16+
</rule>
17+
18+
<rule ref="PSR12"/>
19+
20+
<rule ref="PSR2.Methods.MethodDeclaration.Underscore">
21+
<type>warning</type>
22+
</rule>
23+
</ruleset>

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
paths:
3+
- ./src
4+
level: 5

src/Codeception/Lib/Connector/Guzzle.php

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
class Guzzle extends AbstractBrowser
2525
{
26+
/**
27+
* @var array<string, mixed>
28+
*/
2629
protected array $requestOptions = [
2730
'allow_redirects' => false,
2831
'headers' => [],
@@ -115,7 +118,7 @@ protected function createResponse(Psr7Response $psr7Response): BrowserKitRespons
115118
$contentType = 'text/html';
116119
}
117120

118-
if (strpos($contentType, 'charset=') === false) {
121+
if (str_contains($contentType, 'charset=') === false) {
119122
if (preg_match('#<meta[^>]+charset *= *["\']?([a-zA-Z\-0-9]+)#i', $body, $matches)) {
120123
$contentType .= ';charset=' . $matches[1];
121124
}
@@ -159,10 +162,10 @@ protected function createResponse(Psr7Response $psr7Response): BrowserKitRespons
159162
protected function getAbsoluteUri(string $uri): string
160163
{
161164
$baseUri = $this->client->getConfig('base_uri');
162-
if (strpos($uri, '://') === false && strpos($uri, '//') !== 0) {
163-
if (strpos($uri, '/') === 0) {
165+
if (str_contains($uri, '://') === false && !str_starts_with($uri, '//')) {
166+
if (str_starts_with($uri, '/')) {
164167
$baseUriPath = $baseUri->getPath();
165-
if (!empty($baseUriPath) && strpos($uri, (string) $baseUriPath) === 0) {
168+
if (!empty($baseUriPath) && str_starts_with($uri, (string) $baseUriPath)) {
166169
$uri = substr($uri, strlen($baseUriPath));
167170
}
168171

@@ -178,9 +181,9 @@ protected function getAbsoluteUri(string $uri): string
178181
return Uri::mergeUrls((string)$baseUri, $uri);
179182
}
180183

181-
protected function doRequest($request)
184+
protected function doRequest(object $request)
182185
{
183-
/** @var $request BrowserKitRequest **/
186+
/** @var BrowserKitRequest $request **/
184187
$guzzleRequest = new Psr7Request(
185188
$request->getMethod(),
186189
$request->getUri(),
@@ -190,17 +193,17 @@ protected function doRequest($request)
190193
$options = $this->requestOptions;
191194
$options['cookies'] = $this->extractCookies($guzzleRequest->getUri()->getHost());
192195
$multipartData = $this->extractMultipartFormData($request);
193-
if (!empty($multipartData)) {
196+
if ($multipartData !== []) {
194197
$options['multipart'] = $multipartData;
195198
}
196199

197200
$formData = $this->extractFormData($request);
198-
if (empty($multipartData) && $formData) {
201+
if ($multipartData === [] && $formData) {
199202
$options['form_params'] = $formData;
200203
}
201204

202205
try {
203-
if (null !== $this->awsCredentials) {
206+
if ($this->awsCredentials instanceof AwsCredentials) {
204207
$response = $this->client->send($this->awsSignature->signRequest($guzzleRequest, $this->awsCredentials), $options);
205208
} else {
206209
$response = $this->client->send($guzzleRequest, $options);
@@ -213,6 +216,7 @@ protected function doRequest($request)
213216
$response = $exception->getResponse();
214217
}
215218

219+
// @phpstan-ignore-next-line
216220
return $this->createResponse($response);
217221
}
218222

@@ -227,7 +231,7 @@ protected function extractHeaders(BrowserKitRequest $request): array
227231
$contentHeaders = ['Content-Length' => true, 'Content-Md5' => true, 'Content-Type' => true];
228232
foreach ($server as $header => $val) {
229233
$header = html_entity_decode(implode('-', array_map('ucfirst', explode('-', strtolower(str_replace('_', '-', $header))))), ENT_NOQUOTES);
230-
if (strpos($header, 'Http-') === 0) {
234+
if (str_starts_with($header, 'Http-')) {
231235
$headers[substr($header, 5)] = $val;
232236
} elseif (isset($contentHeaders[$header])) {
233237
$headers[$header] = $val;
@@ -237,6 +241,9 @@ protected function extractHeaders(BrowserKitRequest $request): array
237241
return $headers;
238242
}
239243

244+
/**
245+
* @return array<int, mixed>|null
246+
*/
240247
protected function extractFormData(BrowserKitRequest $browserKitRequest): ?array
241248
{
242249
if (!in_array(strtoupper($browserKitRequest->getMethod()), ['POST', 'PUT', 'PATCH', 'DELETE'])) {
@@ -257,14 +264,17 @@ protected function extractFormData(BrowserKitRequest $browserKitRequest): ?array
257264
return $browserKitRequest->getParameters();
258265
}
259266

260-
protected function extractMultipartFormData(BrowserKitRequest $browserKitRequest)
267+
/**
268+
* @return array<string, mixed>
269+
*/
270+
protected function extractMultipartFormData(BrowserKitRequest $browserKitRequest): array
261271
{
262272
if (!in_array(strtoupper($browserKitRequest->getMethod()), ['POST', 'PUT', 'PATCH'])) {
263273
return [];
264274
}
265275

266276
$parts = $this->mapFiles($browserKitRequest->getFiles());
267-
if (empty($parts)) {
277+
if ($parts === []) {
268278
return [];
269279
}
270280

@@ -275,11 +285,14 @@ protected function extractMultipartFormData(BrowserKitRequest $browserKitRequest
275285
return $parts;
276286
}
277287

278-
protected function formatMultipart($parts, $key, $value)
288+
/**
289+
* @return array<string, mixed>
290+
*/
291+
protected function formatMultipart(mixed $parts, string $key, mixed $value): array
279292
{
280293
if (is_array($value)) {
281294
foreach ($value as $subKey => $subValue) {
282-
$parts = array_merge($this->formatMultipart([], $key.sprintf('[%s]', $subKey), $subValue), $parts);
295+
$parts = array_merge($this->formatMultipart([], $key . sprintf('[%s]', $subKey), $subValue), $parts);
283296
}
284297

285298
return $parts;
@@ -289,7 +302,11 @@ protected function formatMultipart($parts, $key, $value)
289302
return $parts;
290303
}
291304

292-
protected function mapFiles($requestFiles, $arrayName = ''): array
305+
/**
306+
* @param array<int, mixed> $requestFiles
307+
* @return array<int, mixed>
308+
*/
309+
protected function mapFiles(array $requestFiles, ?string $arrayName = ''): array
293310
{
294311
$files = [];
295312
foreach ($requestFiles as $name => $info) {
@@ -329,7 +346,7 @@ protected function mapFiles($requestFiles, $arrayName = ''): array
329346
return $files;
330347
}
331348

332-
protected function extractCookies($host): GuzzleCookieJar
349+
protected function extractCookies(string $host): GuzzleCookieJar
333350
{
334351
$jar = [];
335352
$cookies = $this->getCookieJar()->all();
@@ -345,7 +362,7 @@ protected function extractCookies($host): GuzzleCookieJar
345362
return new GuzzleCookieJar(false, $jar);
346363
}
347364

348-
public static function createHandler($handler): GuzzleHandlerStack
365+
public static function createHandler(mixed $handler): GuzzleHandlerStack
349366
{
350367
if ($handler instanceof GuzzleHandlerStack) {
351368
return $handler;
@@ -360,7 +377,7 @@ public static function createHandler($handler): GuzzleHandlerStack
360377
}
361378

362379
if (is_string($handler) && class_exists($handler)) {
363-
return GuzzleHandlerStack::create(new $handler);
380+
return GuzzleHandlerStack::create(new $handler());
364381
}
365382

366383
if (is_callable($handler)) {
@@ -370,7 +387,10 @@ public static function createHandler($handler): GuzzleHandlerStack
370387
return GuzzleHandlerStack::create();
371388
}
372389

373-
public function setAwsAuth($config): void
390+
/**
391+
* @param array<string, mixed> $config
392+
*/
393+
public function setAwsAuth(array $config): void
374394
{
375395
$this->awsCredentials = new AwsCredentials($config['key'], $config['secret']);
376396
$this->awsSignature = new AwsSignatureV4($config['service'], $config['region']);

src/Codeception/Module/PhpBrowser.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function _initialize()
135135

136136
public function _before(TestInterface $test)
137137
{
138-
if (!$this->client) {
138+
if (!$this->client instanceof AbstractBrowser) {
139139
$this->client = new Guzzle();
140140
}
141141

@@ -155,12 +155,14 @@ public function setHeader(string $name, string $value): void
155155
$this->haveHttpHeader($name, $value);
156156
}
157157

158-
public function amHttpAuthenticated($username, $password): void
158+
public function amHttpAuthenticated(string $username, string $password): void
159159
{
160-
$this->client->setAuth($username, $password);
160+
if ($this->client instanceof Guzzle) {
161+
$this->client->setAuth($username, $password);
162+
}
161163
}
162164

163-
public function amOnUrl($url): void
165+
public function amOnUrl(string $url): void
164166
{
165167
$host = Uri::retrieveHost($url);
166168
$config = $this->config;
@@ -175,7 +177,7 @@ public function amOnUrl($url): void
175177
$this->amOnPage($page);
176178
}
177179

178-
public function amOnSubdomain($subdomain): void
180+
public function amOnSubdomain(string $subdomain): void
179181
{
180182
$url = $this->config['url'];
181183
$url = preg_replace('#(https?://)(.*\.)(.*\.)#', "$1$3", $url); // removing current subdomain
@@ -206,18 +208,13 @@ protected function onReconfigure()
206208
*
207209
* It is not recommended to use this command on a regular basis.
208210
* If Codeception lacks important Guzzle Client methods, implement them and submit patches.
209-
*
210-
* @return mixed
211211
*/
212-
public function executeInGuzzle(Closure $function)
212+
public function executeInGuzzle(Closure $function): mixed
213213
{
214214
return $function($this->guzzle);
215215
}
216216

217-
/**
218-
* @return int|string
219-
*/
220-
public function _getResponseCode()
217+
public function _getResponseCode(): int|string
221218
{
222219
return $this->getResponseStatusCode();
223220
}
@@ -245,21 +242,24 @@ public function _prepareSession(): void
245242

246243
$defaults['base_uri'] = $this->config['url'];
247244
$defaults['curl'] = $curlOptions;
248-
$handler = Guzzle::createHandler($this->config['handler']);
249-
if ($handler && is_array($this->config['middleware'])) {
245+
$handlerStack = Guzzle::createHandler($this->config['handler']);
246+
if (is_array($this->config['middleware'])) {
250247
foreach ($this->config['middleware'] as $middleware) {
251-
$handler->push($middleware);
248+
$handlerStack->push($middleware);
252249
}
253250
}
254251

255-
$defaults['handler'] = $handler;
252+
$defaults['handler'] = $handlerStack;
256253
$this->guzzle = new GuzzleClient($defaults);
257254

258255
$this->client->setRefreshMaxInterval($this->config['refresh_max_interval']);
259256
$this->client->setClient($this->guzzle);
260257
}
261258

262-
public function _backupSession(): array
259+
/**
260+
* @return array<string, mixed>
261+
*/
262+
public function _backupSession()
263263
{
264264
return [
265265
'client' => $this->client,
@@ -269,13 +269,19 @@ public function _backupSession(): array
269269
];
270270
}
271271

272+
/**
273+
* @param array<string, mixed> $session
274+
*/
272275
public function _loadSession($session): void
273276
{
274277
foreach ($session as $key => $val) {
275278
$this->$key = $val;
276279
}
277280
}
278281

282+
/**
283+
* @param ?array<string, mixed> $session
284+
*/
279285
public function _closeSession($session = null): void
280286
{
281287
unset($session);

tests/_support/UnitTester.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
/**
54
* Inherited Methods
65
* @method void wantToTest($text)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy