diff --git a/composer.json b/composer.json index 6aec148..b2d64be 100644 --- a/composer.json +++ b/composer.json @@ -1,41 +1,43 @@ { - "name":"codeception/module-phalcon4", - "description":"Codeception module for Phalcon 4 framework", - "keywords":["codeception", "phalcon", "phalcon4"], - "homepage":"https://codeception.com/", - "type":"library", - "license":"MIT", - "authors": [ - { - "name": "Phalcon Team", - "email": "team@phalcon.io", - "homepage": "https://phalcon.io/en/team" - }, - { - "name": "Codeception Team", - "email": "team@codeception.com", - "homepage": "http://codeception.com/" - } - ], - "require": { - "php": ">=7.2.0", - "ext-phalcon": ">=4", - "codeception/codeception": "^4.0", - "codeception/lib-innerbrowser": "^1.0", - "codeception/module-asserts": "^1.0", - "codeception/module-phpbrowser": "^1.0", - "codeception/module-db": "^1.0" - }, - "require-dev": { - "codeception/util-robohelpers": "dev-master", - "vlucas/phpdotenv": "^4.1", - "squizlabs/php_codesniffer": "^3.4", - "vimeo/psalm": "^3.6" - }, - "autoload":{ - "classmap": ["src/"] - }, - "config": { - "classmap-authoritative": true - } + "name": "codeception/module-phalcon4", + "description": "Codeception module for Phalcon 4 framework", + "keywords": [ "codeception", "phalcon", "phalcon4" ], + "homepage": "https://codeception.com/", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Phalcon Team", + "email": "team@phalcon.io", + "homepage": "https://phalcon.io/en/team" + }, + { + "name": "Codeception Team", + "email": "team@codeception.com", + "homepage": "https://codeception.com/" + } + ], + "require": { + "php": ">=7.2.0", + "ext-json": "*", + "codeception/codeception": "^4.0", + "codeception/lib-innerbrowser": "^1.0", + "codeception/module-asserts": "^1.0", + "codeception/module-phpbrowser": "^1.0", + "codeception/module-db": "^1.0" + }, + "require-dev": { + "codeception/util-robohelpers": "dev-master", + "vlucas/phpdotenv": "^4.1", + "squizlabs/php_codesniffer": "^3.4", + "vimeo/psalm": "^3.6" + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "config": { + "classmap-authoritative": true + } } diff --git a/readme.md b/readme.md index fd05755..4b9f3fd 100644 --- a/readme.md +++ b/readme.md @@ -7,6 +7,10 @@ A Codeception module for Phalcon4 framework [![Total Downloads](https://poser.pugx.org/codeception/module-phalcon4/downloads)](https://packagist.org/packages/codeception/module-phalcon4) [![License](https://poser.pugx.org/codeception/module-phalcon4/license)](/LICENSE) +## Requirements + +* `PHP 7.2` or higher. + ## Installation ``` diff --git a/src/Codeception/Lib/Connector/Phalcon4.php b/src/Codeception/Lib/Connector/Phalcon4.php index ba0e6cb..4e6ebfb 100644 --- a/src/Codeception/Lib/Connector/Phalcon4.php +++ b/src/Codeception/Lib/Connector/Phalcon4.php @@ -1,5 +1,7 @@ application = $application; } @@ -43,10 +47,11 @@ public function setApplication($application) public function getApplication() { $application = $this->application; - if ($application instanceof Closure) { return $application(); - } elseif (is_string($application)) { + } + + if (is_string($application)) { /** @noinspection PhpIncludeInspection */ return require $application; } @@ -57,12 +62,11 @@ public function getApplication() /** * Makes a request. * - * @param \Symfony\Component\BrowserKit\Request $request + * @param Request $request * - * @return \Symfony\Component\BrowserKit\Response - * @throws \RuntimeException + * @throws RuntimeException|ReflectionException */ - public function doRequest($request) + public function doRequest($request): Response { $application = $this->getApplication(); if (!$application instanceof Application && !$application instanceof MicroApplication) { @@ -99,7 +103,7 @@ public function doRequest($request) $_POST = $_REQUEST; } - parse_str($queryString, $output); + parse_str((string) $queryString, $output); foreach ($output as $k => $v) { $_GET[$k] = $v; } @@ -125,12 +129,14 @@ public function doRequest($request) $headersProperty = new ReflectionProperty($headers, 'headers'); $headersProperty->setAccessible(true); + $headers = $headersProperty->getValue($headers); if (!is_array($headers)) { $headers = []; } $cookiesProperty = new ReflectionProperty($di['cookies'], 'cookies'); + $cookiesProperty->setAccessible(true); $cookies = $cookiesProperty->getValue($di['cookies']); if (is_array($cookies)) { @@ -156,7 +162,7 @@ public function doRequest($request) return new Response( $response->getContent() ?: '', - $status ? $status : 200, + $status !== 0 ? $status : 200, $headers ); } diff --git a/src/Codeception/Lib/Connector/Phalcon4/MemorySession.php b/src/Codeception/Lib/Connector/Phalcon4/MemorySession.php index c09a82e..ca7596c 100644 --- a/src/Codeception/Lib/Connector/Phalcon4/MemorySession.php +++ b/src/Codeception/Lib/Connector/Phalcon4/MemorySession.php @@ -1,8 +1,11 @@ status() !== PHP_SESSION_ACTIVE) { $this->memory = []; @@ -60,7 +63,7 @@ public function start() * * @param array $options */ - public function setOptions(array $options) + public function setOptions(array $options): void { if (isset($options['uniqueId'])) { $this->sessionId = $options['uniqueId']; @@ -74,7 +77,7 @@ public function setOptions(array $options) * * @return array */ - public function getOptions() + public function getOptions(): array { return $this->options; } @@ -87,7 +90,7 @@ public function getOptions() * @param bool $remove * @return mixed */ - public function get($index, $defaultValue = null, $remove = false) + public function get(string $index, $defaultValue = null, bool $remove = false) { $key = $this->prepareIndex($index); @@ -110,7 +113,7 @@ public function get($index, $defaultValue = null, $remove = false) * @param string $index * @param mixed $value */ - public function set($index, $value) + public function set(string $index, $value): void { $this->memory[$this->prepareIndex($index)] = $value; } @@ -121,7 +124,7 @@ public function set($index, $value) * @param string $index * @return bool */ - public function has($index) + public function has(string $index): bool { return isset($this->memory[$this->prepareIndex($index)]); } @@ -131,7 +134,7 @@ public function has($index) * * @param string $index */ - public function remove($index) + public function remove(string $index): void { unset($this->memory[$this->prepareIndex($index)]); } @@ -141,7 +144,7 @@ public function remove($index) * * @return string */ - public function getId() + public function getId(): string { return $this->sessionId; } @@ -151,7 +154,7 @@ public function getId() * * @return bool */ - public function isStarted() + public function isStarted(): bool { return $this->started; } @@ -164,12 +167,11 @@ public function isStarted() * if ($session->status() !== PHP_SESSION_ACTIVE) { * $session->start(); * } - * ?> * ``` * * @return int */ - public function status() + public function status(): int { if ($this->isStarted()) { return PHP_SESSION_ACTIVE; @@ -181,7 +183,7 @@ public function status() /** * @inheritdoc * - * @param bool $removeData + * @param bool $id * @return bool */ public function destroy($id): bool @@ -195,9 +197,9 @@ public function destroy($id): bool * @inheritdoc * * @param bool $deleteOldSession - * @return \Phalcon\Session\AdapterInterface + * @return AdapterInterface */ - public function regenerateId($deleteOldSession = true) + public function regenerateId(bool $deleteOldSession = true): AdapterInterface { $this->sessionId = $this->generateId(); @@ -209,7 +211,7 @@ public function regenerateId($deleteOldSession = true) * * @param string $name */ - public function setName($name) + public function setName(string $name): void { $this->name = $name; } @@ -219,7 +221,7 @@ public function setName($name) * * @return string */ - public function getName() + public function getName(): string { return $this->name; } @@ -229,7 +231,7 @@ public function getName() * * @return array */ - public function toArray() + public function toArray(): array { return (array) $this->memory; } @@ -240,7 +242,7 @@ public function toArray() * @param string $index * @return mixed */ - public function __get($index) + public function __get(string $index) { return $this->get($index); } @@ -251,7 +253,7 @@ public function __get($index) * @param string $index * @param mixed $value */ - public function __set($index, $value) + public function __set(string $index, $value): void { $this->set($index, $value); } @@ -259,10 +261,10 @@ public function __set($index, $value) /** * Alias: Check whether a session variable is set in an application context * - * @param string $index + * @param string $index * @return bool */ - public function __isset($index) + public function __isset(string $index): bool { return $this->has($index); } @@ -272,12 +274,12 @@ public function __isset($index) * * @param string $index */ - public function __unset($index) + public function __unset(string $index): void { $this->remove($index); } - private function prepareIndex($index) + private function prepareIndex(string $index): string { if ($this->sessionId) { $key = $this->sessionId . '#' . $index; @@ -291,9 +293,9 @@ private function prepareIndex($index) /** * @return string */ - private function generateId() + private function generateId(): string { - return md5(time()); + return sha1((string) time()); } /** diff --git a/src/Codeception/Lib/Connector/Phalcon4/SessionManager.php b/src/Codeception/Lib/Connector/Phalcon4/SessionManager.php index b9066a9..eb8cc65 100644 --- a/src/Codeception/Lib/Connector/Phalcon4/SessionManager.php +++ b/src/Codeception/Lib/Connector/Phalcon4/SessionManager.php @@ -1,5 +1,7 @@ 'app/config/bootstrap.php', 'cleanup' => true, @@ -101,12 +107,14 @@ class Phalcon4 extends Framework implements ActiveRecord, PartedModule /** * Dependency injection container + * * @var DiInterface */ public $di = null; /** * Phalcon Connector + * * @var PhalconConnector */ public $client; @@ -231,7 +239,7 @@ public function getApplication() * @param string $key * @param mixed $val */ - public function haveInSession($key, $val) + public function haveInSession(string $key, $val): void { $this->di->get('session')->set($key, $val); $this->debugSection('Session', json_encode($this->di['session']->getAdapter()->toArray())); @@ -245,13 +253,12 @@ public function haveInSession($key, $val) * seeInSession('key'); * $I->seeInSession('key', 'value'); - * ?> * ``` * * @param string $key * @param mixed $value */ - public function seeInSession($key, $value = null) + public function seeInSession(string $key, $value = null): void { $this->debugSection('Session', json_encode($this->di['session']->getAdapter()->toArray())); @@ -278,13 +285,12 @@ public function seeInSession($key, $value = null) * seeSessionHasValues(['key1', 'key2']); * $I->seeSessionHasValues(['key1' => 'value1', 'key2' => 'value2']); - * ?> * ``` * * @param array $bindings * @return void */ - public function seeSessionHasValues(array $bindings) + public function seeSessionHasValues(array $bindings): void { foreach ($bindings as $key => $value) { if (is_int($key)) { @@ -302,7 +308,6 @@ public function seeSessionHasValues(array $bindings) * haveRecord('App\Models\Users', ['name' => 'Phalcon']); * $I->haveRecord('App\Models\Categories', ['name' => 'Testing']'); - * ?> * ``` * * @param string $model Model name @@ -352,7 +357,6 @@ public function haveRecord($model, $attributes = []) * ``` php * seeRecord('App\Models\Categories', ['name' => 'Testing']); - * ?> * ``` * * @param string $model Model name @@ -374,15 +378,14 @@ public function seeRecord($model, $attributes = []) * ``` php * seeNumberOfRecords('App\Models\Categories', 3, ['name' => 'Testing']); - * ?> * ``` * * @param string $model Model name * @param int $number int number of records - * @param array $attributes Model attributes + * @param array $attributes Model attributes * @part orm */ - public function seeNumberOfRecords($model, $number, $attributes = []) + public function seeNumberOfRecords(string $model, int $number, array $attributes = []): void { $records = $this->findRecords($model, $attributes); if ($records->count() != $number) { @@ -403,7 +406,6 @@ public function seeNumberOfRecords($model, $number, $attributes = []) * ``` php * dontSeeRecord('App\Models\Categories', ['name' => 'Testing']); - * ?> * ``` * * @param string $model Model name @@ -425,7 +427,6 @@ public function dontSeeRecord($model, $attributes = []) * ``` php * grabRecord('App\Models\Categories', ['name' => 'Testing']); - * ?> * ``` * * @param string $model Model name @@ -447,7 +448,7 @@ public function grabRecord($model, $attributes = []) * @return mixed * @part services */ - public function grabServiceFromContainer($service, array $parameters = []) + public function grabServiceFromContainer(string $service, array $parameters = []) { if (!$this->di->has($service)) { $this->fail("Service $service is not available in container"); @@ -466,7 +467,6 @@ public function grabServiceFromContainer($service, array $parameters = []) * $filter = $I->addServiceToContainer('answer', function () { * return rand(0, 1) ? 'Yes' : 'No'; * }, true); - * ?> * ``` * * @param string $name @@ -475,12 +475,12 @@ public function grabServiceFromContainer($service, array $parameters = []) * @return mixed|null * @part services */ - public function addServiceToContainer($name, $definition, $shared = false) + public function addServiceToContainer(string $name, $definition, bool $shared = false) { try { $service = $this->di->set($name, $definition, $shared); return $service->resolve(); - } catch (\Exception $e) { + } catch (Exception $e) { $this->fail($e->getMessage()); return null; @@ -493,13 +493,12 @@ public function addServiceToContainer($name, $definition, $shared = false) * ``` php * amOnRoute('posts.create'); - * ?> * ``` * * @param string $routeName * @param array $params */ - public function amOnRoute($routeName, array $params = []) + public function amOnRoute(string $routeName, array $params = []): void { if (!$this->di->has('url')) { $this->fail('Unable to resolve "url" service.'); @@ -522,11 +521,10 @@ public function amOnRoute($routeName, array $params = []) * ``` php * seeCurrentRouteIs('posts.index'); - * ?> * ``` * @param string $routeName */ - public function seeCurrentRouteIs($routeName) + public function seeCurrentRouteIs(string $routeName): void { if (!$this->di->has('url')) { $this->fail('Unable to resolve "url" service.'); @@ -545,7 +543,7 @@ public function seeCurrentRouteIs($routeName) * * @return \Phalcon\Mvc\Model */ - protected function findRecord($model, $attributes = []) + protected function findRecord(string $model, array $attributes = []) { $this->getModelRecord($model); $conditions = []; @@ -574,9 +572,9 @@ protected function findRecord($model, $attributes = []) * @param string $model Model name * @param array $attributes Model attributes * - * @return \Phalcon\Mvc\ResultsetInterface + * @return ResultsetInterface */ - protected function findRecords($model, $attributes = []) + protected function findRecords(string $model, array $attributes = []) { $this->getModelRecord($model); $conditions = []; @@ -691,10 +689,7 @@ protected function getInternalDomains() return array_unique($internalDomains); } - /** - * @return string - */ - private function getApplicationDomainRegex() + private function getApplicationDomainRegex(): string { $server = ReflectionHelper::readPrivateProperty($this->client, 'server'); $domain = $server['HTTP_HOST']; diff --git a/tests/unit/MemorySessionTest.php b/tests/unit/MemorySessionTest.php index 2476f00..0c336d1 100644 --- a/tests/unit/MemorySessionTest.php +++ b/tests/unit/MemorySessionTest.php @@ -1,9 +1,11 @@