From e1e02b20418c5c642e8eaf88bb712d2688302d66 Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Mon, 1 Jan 2024 10:43:47 +0100 Subject: [PATCH] fix(test): fail on php errors --- .github/workflows/continuous-integration.yml | 4 +++- .gitignore | 1 + docker-compose.yml | 6 +++++ lib/autoload/sfCoreAutoload.class.php | 7 +++--- lib/config/autoload/swift.php | 6 +++++ lib/config/sfFactoryConfigHandler.class.php | 5 +--- lib/mailer/sfMailer.class.php | 4 ++-- lib/mailer/sfMailerSwiftMessage.php | 17 ++++++++++++++ .../lib/mailer/Swift_DoctrineSpool.class.php | 4 ++-- .../fixtures/apps/backend/config/settings.yml | 2 +- .../apps/frontend/config/settings.yml | 2 +- lib/storage/sfCacheSessionStorage.class.php | 23 +++++++++++-------- lib/task/sfCommandApplicationTask.class.php | 6 +---- lib/validator/sfValidatorFile.class.php | 6 ++++- lib/validator/sfValidatorSchema.class.php | 16 ++++++++++--- lib/vendor/lime/lime.php | 16 +++++++++++-- require_for_php8.txt | 2 ++ test/bootstrap/functional.php | 4 ++++ test/bootstrap/unit.php | 4 ++++ .../fixtures/apps/cache/config/settings.yml | 6 ++--- .../apps/frontend/config/settings.yml | 2 +- .../fixtures/apps/i18n/config/settings.yml | 2 +- .../mailer/fixtures/TestMailMessage.class.php | 2 +- .../fixtures/TestMailerTransport.class.php | 7 +++++- test/unit/mailer/fixtures/TestSpool.class.php | 2 +- test/unit/mailer/sfMailerTest.php | 2 +- .../mailer/sfMailerWithoutComposerTest.php | 13 +++++++++++ 27 files changed, 127 insertions(+), 44 deletions(-) create mode 100644 lib/config/autoload/swift.php mode change 100755 => 100644 lib/mailer/sfMailer.class.php create mode 100644 lib/mailer/sfMailerSwiftMessage.php create mode 100644 require_for_php8.txt mode change 100755 => 100644 test/unit/mailer/sfMailerTest.php create mode 100644 test/unit/mailer/sfMailerWithoutComposerTest.php diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 47e723aed..b924629a1 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -28,6 +28,8 @@ jobs: services: memcached: image: "memcached:${{ matrix.memcached-version }}" + ports: + - 11211:11211 steps: - name: Checkout @@ -40,7 +42,7 @@ jobs: with: php-version: "${{ matrix.php-version }}" extensions: apcu - ini-values: apc.enable_cli=1 + ini-values: error_reporting=5111,apc.enable_cli=on,apc.use_request_time=off,memory_limit=-1,short_open_tag=off,magic_quotes_gpc=off,date.timezone="UTC" - name: Get composer cache directory id: composer-cache diff --git a/.gitignore b/.gitignore index 3c17d4767..0c192c4e2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ /lib/plugins/sfDoctrinePlugin/test/functional/fixtures/lib/*/doctrine/*/base/ /lib/plugins/sfDoctrinePlugin/test/functional/fixtures/cache/ lib/plugins/sfDoctrinePlugin/test/functional/fixtures/log/ +/lib/plugins/sfDoctrinePlugin/test/functional/fixtures/data/*.sqlite* /vendor /composer.lock .php-cs-fixer.cache diff --git a/docker-compose.yml b/docker-compose.yml index b0f11aa46..db019aa96 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -28,6 +28,9 @@ services: { echo 'pdo_mysql.default_socket = /var/run/mysqld/mysql.sock' echo 'memory_limit = -1' + echo 'error_reporting = '` + php -r 'echo (E_ALL | E_STRICT) ^ E_USER_DEPRECATED ^ E_DEPRECATED ^ E_STRICT ^ E_USER_NOTICE ^ E_NOTICE;' + ` echo 'short_open_tag = off' echo 'magic_quotes_gpc = off' echo 'date.timezone = "UTC"' @@ -57,6 +60,9 @@ services: { echo 'pdo_mysql.default_socket = /var/run/mysqld/mysql.sock' echo 'memory_limit = -1' + echo 'error_reporting = '` + php -r 'echo (E_ALL | E_STRICT) ^ E_USER_DEPRECATED ^ E_DEPRECATED ^ E_STRICT ^ E_USER_NOTICE ^ E_NOTICE;' + ` echo 'short_open_tag = off' echo 'magic_quotes_gpc = off' echo 'date.timezone = "UTC"' diff --git a/lib/autoload/sfCoreAutoload.class.php b/lib/autoload/sfCoreAutoload.class.php index fafe37026..9a8523460 100755 --- a/lib/autoload/sfCoreAutoload.class.php +++ b/lib/autoload/sfCoreAutoload.class.php @@ -194,6 +194,7 @@ class sfCoreAutoload 'sfwebdebuglogger' => 'log/sfWebDebugLogger.class.php', 'sfmailer' => 'mailer/sfMailer.class.php', 'sfmailermessageloggerplugin' => 'mailer/sfMailerMessageLoggerPlugin.class.php', + 'sfmailerswiftmessage' => 'mailer/sfMailerSwiftMessage.php', 'sfnomailer' => 'mailer/sfNoMailer.class.php', 'sfpearconfig' => 'plugin/sfPearConfig.class.php', 'sfpeardownloader' => 'plugin/sfPearDownloader.class.php', @@ -373,8 +374,8 @@ class sfCoreAutoload 'sfwidgetforminput' => 'widget/sfWidgetFormInput.class.php', 'sfwidgetforminputcheckbox' => 'widget/sfWidgetFormInputCheckbox.class.php', 'sfwidgetforminputfile' => 'widget/sfWidgetFormInputFile.class.php', - 'sfwidgetforminputfilemulti' => 'widget/sfWidgetFormInputFileMulti.class.php', 'sfwidgetforminputfileeditable' => 'widget/sfWidgetFormInputFileEditable.class.php', + 'sfwidgetforminputfilemulti' => 'widget/sfWidgetFormInputFileMulti.class.php', 'sfwidgetforminputhidden' => 'widget/sfWidgetFormInputHidden.class.php', 'sfwidgetforminputpassword' => 'widget/sfWidgetFormInputPassword.class.php', 'sfwidgetforminputread' => 'widget/sfWidgetFormInputRead.class.php', @@ -521,11 +522,11 @@ public static function make() if (false !== stripos($contents, 'class '.$class) || false !== stripos($contents, 'interface '.$class) || false !== stripos($contents, 'trait '.$class)) { - $classes .= sprintf(" '%s' => '%s',\n", strtolower($class), substr(str_replace($libDir, '', $file), 1)); + $classes .= sprintf(" '%s' => '%s',\n", strtolower($class), substr(str_replace($libDir, '', $file), 1)); } } - $content = preg_replace('/protected \$classes = array *\(.*?\);/s', sprintf("protected \$classes = array(\n%s );", $classes), file_get_contents(__FILE__)); + $content = preg_replace('/protected \$classes = array *\(.*?\);/s', sprintf("protected \$classes = array(\n%s );", $classes), file_get_contents(__FILE__)); file_put_contents(__FILE__, $content); } diff --git a/lib/config/autoload/swift.php b/lib/config/autoload/swift.php new file mode 100644 index 000000000..ea842521e --- /dev/null +++ b/lib/config/autoload/swift.php @@ -0,0 +1,6 @@ +setMailerConfiguration(array_merge(array('class' => sfConfig::get('sf_factory_mailer', '%s')), sfConfig::get('sf_factory_mailer_parameters', %s)));\n", $class, var_export($parameters, true) diff --git a/lib/mailer/sfMailer.class.php b/lib/mailer/sfMailer.class.php old mode 100755 new mode 100644 index bb55247f4..597fb2ad6 --- a/lib/mailer/sfMailer.class.php +++ b/lib/mailer/sfMailer.class.php @@ -58,7 +58,7 @@ public function __construct(sfEventDispatcher $dispatcher, $options) 'logging' => false, 'delivery_strategy' => self::REALTIME, 'transport' => array( - 'class' => 'Swift_MailTransport', + 'class' => class_exists('Swift_MailTransport') ? 'Swift_MailTransport' : 'Swift_SmtpTransport', 'param' => array(), ), ), $options); @@ -262,7 +262,7 @@ public function sendNextImmediately() * * @return false|int The number of sent emails */ - public function send($message, &$failedRecipients = null) + public function send(sfMailerSwiftMessage $message, &$failedRecipients = null) { if ($this->force) { $this->force = false; diff --git a/lib/mailer/sfMailerSwiftMessage.php b/lib/mailer/sfMailerSwiftMessage.php new file mode 100644 index 000000000..ffb0e1a15 --- /dev/null +++ b/lib/mailer/sfMailerSwiftMessage.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +class_exists('Swift'); + +if (version_compare(Swift::VERSION, '6.0.0') >= 0) { + class_alias('Swift_Mime_SimpleMessage', 'sfMailerSwiftMessage'); +} else { + class_alias('Swift_Mime_Message', 'sfMailerSwiftMessage'); +} diff --git a/lib/plugins/sfDoctrinePlugin/lib/mailer/Swift_DoctrineSpool.class.php b/lib/plugins/sfDoctrinePlugin/lib/mailer/Swift_DoctrineSpool.class.php index 7235a2e1e..dd2fbb056 100644 --- a/lib/plugins/sfDoctrinePlugin/lib/mailer/Swift_DoctrineSpool.class.php +++ b/lib/plugins/sfDoctrinePlugin/lib/mailer/Swift_DoctrineSpool.class.php @@ -69,9 +69,9 @@ public function stop() /** * Stores a message in the queue. * - * @param Swift_Mime_Message $message The message to store + * @param Swift_Mime_Message|Swift_Mime_SimpleMessage $message The message to store */ - public function queueMessage(Swift_Mime_Message $message) + public function queueMessage(sfMailerSwiftMessage $message) { $object = new $this->model(); diff --git a/lib/plugins/sfDoctrinePlugin/test/functional/fixtures/apps/backend/config/settings.yml b/lib/plugins/sfDoctrinePlugin/test/functional/fixtures/apps/backend/config/settings.yml index 73fd9fc6b..4c7dbce26 100644 --- a/lib/plugins/sfDoctrinePlugin/test/functional/fixtures/apps/backend/config/settings.yml +++ b/lib/plugins/sfDoctrinePlugin/test/functional/fixtures/apps/backend/config/settings.yml @@ -13,7 +13,7 @@ dev: test: .settings: - error_reporting: + error_reporting: cache: false web_debug: false no_script_name: false diff --git a/lib/plugins/sfDoctrinePlugin/test/functional/fixtures/apps/frontend/config/settings.yml b/lib/plugins/sfDoctrinePlugin/test/functional/fixtures/apps/frontend/config/settings.yml index 73fd9fc6b..4c7dbce26 100644 --- a/lib/plugins/sfDoctrinePlugin/test/functional/fixtures/apps/frontend/config/settings.yml +++ b/lib/plugins/sfDoctrinePlugin/test/functional/fixtures/apps/frontend/config/settings.yml @@ -13,7 +13,7 @@ dev: test: .settings: - error_reporting: + error_reporting: cache: false web_debug: false no_script_name: false diff --git a/lib/storage/sfCacheSessionStorage.class.php b/lib/storage/sfCacheSessionStorage.class.php index 6dd0442c3..790130445 100644 --- a/lib/storage/sfCacheSessionStorage.class.php +++ b/lib/storage/sfCacheSessionStorage.class.php @@ -99,11 +99,7 @@ public function initialize($options = array()) } if (empty($this->id)) { - $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'localhost'; - $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'ua'; - - // generate new id based on random # / ip / user agent / secret - $this->id = md5(mt_rand(0, 999999).$ip.$ua.$this->options['session_cookie_secret']); + $this->id = $this->generateId(); if (sfConfig::get('sf_logging_enabled')) { $this->dispatcher->notify(new sfEvent($this, 'application.log', array('New session created'))); @@ -224,10 +220,7 @@ public function regenerate($destroy = false) $this->cache->remove($this->id); } - // generate session id - $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'ua'; - - $this->id = md5(mt_rand(0, 999999).$_SERVER['REMOTE_ADDR'].$ua.$this->options['session_cookie_secret']); + $this->id = $this->generateId(); // save data to cache $this->cache->set($this->id, serialize($this->data)); @@ -275,4 +268,16 @@ public function shutdown() } } } + + /** + * @return string + */ + private function generateId() + { + $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'localhost'; + $ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'ua'; + + // generate new id based on random # / ip / user agent / secret + return md5(mt_rand(0, 999999).$ip.$ua.$this->options['session_cookie_secret']); + } } diff --git a/lib/task/sfCommandApplicationTask.class.php b/lib/task/sfCommandApplicationTask.class.php index bf3c23635..46b978852 100644 --- a/lib/task/sfCommandApplicationTask.class.php +++ b/lib/task/sfCommandApplicationTask.class.php @@ -142,11 +142,7 @@ protected function getMailer() */ protected function initializeMailer() { - if (!class_exists('Swift')) { - $swift_dir = sfConfig::get('sf_symfony_lib_dir').'/vendor/swiftmailer/lib'; - - require_once $swift_dir.'/swift_required.php'; - } + require_once sfConfig::get('sf_symfony_lib_dir').'/config/autoload/swift.php'; $config = $this->getFactoryConfiguration(); diff --git a/lib/validator/sfValidatorFile.class.php b/lib/validator/sfValidatorFile.class.php index 368bea6f1..f20691f4d 100644 --- a/lib/validator/sfValidatorFile.class.php +++ b/lib/validator/sfValidatorFile.class.php @@ -96,10 +96,14 @@ protected function configure($options = array(), $messages = array()) */ protected function doClean($value) { - if (!is_array($value) || !isset($value['tmp_name'])) { + if (!is_array($value)) { throw new sfValidatorError($this, 'invalid', array('value' => (string) $value)); } + if (!isset($value['tmp_name'])) { + throw new sfValidatorError($this, 'invalid', array('value' => 'Array')); + } + if (!isset($value['name'])) { $value['name'] = ''; } diff --git a/lib/validator/sfValidatorSchema.class.php b/lib/validator/sfValidatorSchema.class.php index 9064138c9..973c0471a 100644 --- a/lib/validator/sfValidatorSchema.class.php +++ b/lib/validator/sfValidatorSchema.class.php @@ -89,7 +89,9 @@ public function clean($values) */ public function preClean($values) { - if (null === $validator = $this->getPreValidator()) { + $validator = $this->getPreValidator(); + + if (null === $validator) { return $values; } @@ -110,7 +112,9 @@ public function preClean($values) */ public function postClean($values) { - if (null === $validator = $this->getPostValidator()) { + $validator = $this->getPostValidator(); + + if (null === $validator) { return $values; } @@ -355,8 +359,14 @@ protected function doClean($values) protected function getBytes($value) { $value = trim($value); + $length = strlen($value); + + if (0 === $length) { + return 0.0; + } + $number = (float) $value; - $modifier = strtolower($value[strlen($value) - 1]); + $modifier = strtolower($value[$length - 1]); $exp_by_modifier = array( 'k' => 1, diff --git a/lib/vendor/lime/lime.php b/lib/vendor/lime/lime.php index b5dad340f..cd88e36a9 100644 --- a/lib/vendor/lime/lime.php +++ b/lib/vendor/lime/lime.php @@ -42,7 +42,7 @@ public function __construct($plan = null, $options = array()) 'force_colors' => false, 'output' => null, 'verbose' => false, - 'error_reporting' => false, + 'error_reporting' => true, ), $options); $this->output = $this->options['output'] ? $this->options['output'] : new lime_output($this->options['force_colors']); @@ -140,6 +140,7 @@ public function __destruct() $plan = $this->results['stats']['plan']; $passed = count($this->results['stats']['passed']); $failed = count($this->results['stats']['failed']); + $errors = count($this->results['stats']['errors']); $total = $this->results['stats']['total']; is_null($plan) and $plan = $total and $this->output->echoln(sprintf("1..%d", $plan)); @@ -156,6 +157,10 @@ public function __destruct() { $this->output->red_bar(sprintf("# Looks like you failed %d tests of %d.", $failed, $passed + $failed)); } + else if ($errors) + { + $this->output->red_bar(sprintf("# Looks like test pass but with %d errors.", $errors)); + } else if ($total == $plan) { $this->output->green_bar("# Looks like everything went fine."); @@ -174,10 +179,11 @@ private function getExitCode() { $plan = $this->results['stats']['plan']; $failed = count($this->results['stats']['failed']); + $errors = count($this->results['stats']['errors']); $total = $this->results['stats']['total']; is_null($plan) and $plan = $total and $this->output->echoln(sprintf("1..%d", $plan)); - if ($failed) + if ($failed || $errors) { return 1; } @@ -608,6 +614,12 @@ public function handle_error($code, $message, $file, $line, $context = null) case E_WARNING: $type = 'Warning'; break; + case E_STRICT: + $type = 'Strict'; + break; + case E_DEPRECATED: + $type = 'Deprecated'; + break; default: $type = 'Notice'; break; diff --git a/require_for_php8.txt b/require_for_php8.txt new file mode 100644 index 000000000..b4e981be3 --- /dev/null +++ b/require_for_php8.txt @@ -0,0 +1,2 @@ +swiftmailer/swiftmailer:^5.4.6 +egulias/email-validator:^2.1.10 diff --git a/test/bootstrap/functional.php b/test/bootstrap/functional.php index fd1f3a96a..815bdd163 100644 --- a/test/bootstrap/functional.php +++ b/test/bootstrap/functional.php @@ -8,6 +8,10 @@ * file that was distributed with this source code. */ +if (!defined('SF_TEST_WITHOUT_COMPOSER')) { + require_once __DIR__.'/../../vendor/autoload.php'; +} + // setup expected test environment (per check_configuration.php) ini_set('magic_quotes_runtime', 'off'); ini_set('session.auto_start', 'off'); diff --git a/test/bootstrap/unit.php b/test/bootstrap/unit.php index 616ca94eb..5e94cd213 100644 --- a/test/bootstrap/unit.php +++ b/test/bootstrap/unit.php @@ -8,6 +8,10 @@ * file that was distributed with this source code. */ +if (!defined('SF_TEST_WITHOUT_COMPOSER')) { + require_once __DIR__.'/../../vendor/autoload.php'; +} + // setup expected test environment (per check_configuration.php) ini_set('magic_quotes_runtime', 'off'); ini_set('session.auto_start', 'off'); diff --git a/test/functional/fixtures/apps/cache/config/settings.yml b/test/functional/fixtures/apps/cache/config/settings.yml index bef752e08..659590fcb 100644 --- a/test/functional/fixtures/apps/cache/config/settings.yml +++ b/test/functional/fixtures/apps/cache/config/settings.yml @@ -5,8 +5,7 @@ prod: dev: .settings: - # E_ALL | E_STRICT = 4095 - error_reporting: 4095 + error_reporting: web_debug: true cache: true no_script_name: false @@ -14,8 +13,7 @@ dev: test: .settings: - # E_ALL | E_STRICT = 4095 - error_reporting: 4095 + error_reporting: web_debug: false cache: true no_script_name: false diff --git a/test/functional/fixtures/apps/frontend/config/settings.yml b/test/functional/fixtures/apps/frontend/config/settings.yml index 97d21a71f..fa84d0714 100644 --- a/test/functional/fixtures/apps/frontend/config/settings.yml +++ b/test/functional/fixtures/apps/frontend/config/settings.yml @@ -13,7 +13,7 @@ dev: test: .settings: - error_reporting: + error_reporting: cache: false web_debug: false no_script_name: false diff --git a/test/functional/fixtures/apps/i18n/config/settings.yml b/test/functional/fixtures/apps/i18n/config/settings.yml index bffb419a9..5dcc49f01 100644 --- a/test/functional/fixtures/apps/i18n/config/settings.yml +++ b/test/functional/fixtures/apps/i18n/config/settings.yml @@ -13,7 +13,7 @@ dev: test: .settings: - error_reporting: + error_reporting: cache: false web_debug: false no_script_name: false diff --git a/test/unit/mailer/fixtures/TestMailMessage.class.php b/test/unit/mailer/fixtures/TestMailMessage.class.php index 1e43fc076..9242f5aee 100644 --- a/test/unit/mailer/fixtures/TestMailMessage.class.php +++ b/test/unit/mailer/fixtures/TestMailMessage.class.php @@ -9,7 +9,7 @@ */ class TestMailMessage { - public function setMessage(Swift_Mime_Message $message) + public function setMessage(sfMailerSwiftMessage $message) { } diff --git a/test/unit/mailer/fixtures/TestMailerTransport.class.php b/test/unit/mailer/fixtures/TestMailerTransport.class.php index bcb8f43bd..da096c0e4 100644 --- a/test/unit/mailer/fixtures/TestMailerTransport.class.php +++ b/test/unit/mailer/fixtures/TestMailerTransport.class.php @@ -34,6 +34,11 @@ public function stop() $this->started = false; } + public function ping() + { + return false; + } + public function registerPlugin(Swift_Events_EventListener $plugin) { } @@ -53,7 +58,7 @@ public function getSentCount() return $this->count; } - public function send(Swift_Mime_Message $message, &$failedRecipients = null) + public function send(sfMailerSwiftMessage $message, &$failedRecipients = null) { ++$this->count; diff --git a/test/unit/mailer/fixtures/TestSpool.class.php b/test/unit/mailer/fixtures/TestSpool.class.php index f0a80ec00..4a6a1b70d 100644 --- a/test/unit/mailer/fixtures/TestSpool.class.php +++ b/test/unit/mailer/fixtures/TestSpool.class.php @@ -28,7 +28,7 @@ public function stop() { } - public function queueMessage(Swift_Mime_Message $message) + public function queueMessage(sfMailerSwiftMessage $message) { $this->messages[] = $message; diff --git a/test/unit/mailer/sfMailerTest.php b/test/unit/mailer/sfMailerTest.php old mode 100755 new mode 100644 index 68a3ff23f..da7feccf7 --- a/test/unit/mailer/sfMailerTest.php +++ b/test/unit/mailer/sfMailerTest.php @@ -9,7 +9,7 @@ */ require_once __DIR__.'/../../bootstrap/unit.php'; -require_once sfConfig::get('sf_symfony_lib_dir').'/vendor/swiftmailer/lib/swift_required.php'; +require_once sfConfig::get('sf_symfony_lib_dir').'/config/autoload/swift.php'; require_once __DIR__.'/fixtures/TestMailerTransport.class.php'; diff --git a/test/unit/mailer/sfMailerWithoutComposerTest.php b/test/unit/mailer/sfMailerWithoutComposerTest.php new file mode 100644 index 000000000..ab3afa553 --- /dev/null +++ b/test/unit/mailer/sfMailerWithoutComposerTest.php @@ -0,0 +1,13 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +define('SF_TEST_WITHOUT_COMPOSER', 1); + +require_once __DIR__.'/sfMailerTest.php';