yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Class-Level Event seem to detach between 2 unit test #15794

Closed vinpel closed 6 years ago

vinpel commented 6 years ago

What steps will reproduce the problem?

i will explain my event structure : in a bootstrap.php file :

Event::on(
  'yii\db\ActiveRecord',
  ActiveRecord::EVENT_AFTER_VALIDATE,
  ['app\events\ActiveRecordEvents', 'logError']
);

Event::on(
  'yii\db\ActiveRecord',
  ActiveRecord::EVENT_AFTER_UPDATE,
  ['app\events\ActiveRecordEvents', 'logError']
);

Event::on(
  'yii\db\ActiveRecord',
  ActiveRecord::EVENT_AFTER_INSERT,
  ['app\events\ActiveRecordEvents', 'logError']
);
Event::on(
  'yii\db\ActiveRecord',
  ActiveRecord::EVENT_AFTER_DELETE,
  ['app\events\ActiveRecordEvents', 'logError']
);

The event file :

<?php
namespace app\events;

use Yii;
use common\models\User;
use app\models\Traitement;
/**
*/
class ActiveRecordEvents
{

  public static function logError($event)
  {

    $model = $event->sender;

    if (count($model->getErrors()) != 0) {

      yii::$app->params['save_error_attributes'] = self::formateAttributes(
        $model->getAttributes()
      );
      yii::$app->params['save_error_error'] = self::formateErreurs(
        $model->getErrors()
      );
      $appelant = '';
      if (isset(debug_backtrace()[7]['file'])) {
        $appelant .= 'file : ' . debug_backtrace()[7]['file'] . "\n";
      }
      if (isset(debug_backtrace()[7]['line'])) {
        $appelant .= 'line : ' . debug_backtrace()[7]['line'] . "\n";
      }
      if (isset(debug_backtrace()[7]['function'])) {
        $appelant .= 'function : ' . debug_backtrace()[7]['function'] . "\n";
      }
        //SKIPPED code : store in database the message
      return false;
    }

   return true;
  }

i have a file with multiple unit test. If i test a unit file :

Unit Tests (6) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Modules: Asserts, Yii2, Db
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- EnsemblePatCreationTest: Creation avec deux categorie ged  [yii\db\Connection::open] 'Opening DB connection: mysql:host=127.0.0.1;dbname=yii2_basic_tests'
  Fixture traitement: cleanTraitementDirectory
  Fixture traitement: cleanTraitementDirectory
  [app\models\mysql\PdfEnsemblePatrimonial] {}
  [app\models\mysql\PdfEnsemblePatrimonial] {}
✔ EnsemblePatCreationTest: Creation avec deux categorie ged (0.16s)
- EnsemblePatCreationTest: Testcreation programme  [yii\db\Connection::open] 'Opening DB connection: mysql:host=127.0.0.1;dbname=yii2_basic_tests'
  Fixture traitement: cleanTraitementDirectory
  [app\models\mysql\PdfEnsemblePatrimonial] {}
✔ EnsemblePatCreationTest: Testcreation programme (0.09s)
- EnsemblePatCreationTest: Testcreation batiment  [yii\db\Connection::open] 'Opening DB connection: mysql:host=127.0.0.1;dbname=yii2_basic_tests'
  Fixture traitement: cleanTraitementDirectory
  [app\models\mysql\PdfEnsemblePatrimonial] {}
✔ EnsemblePatCreationTest: Testcreation batiment (0.10s)
- EnsemblePatCreationTest: Testcreation escalier  [yii\db\Connection::open] 'Opening DB connection: mysql:host=127.0.0.1;dbname=yii2_basic_tests'
  Fixture traitement: cleanTraitementDirectory
  [app\models\mysql\PdfEnsemblePatrimonial] {}
✔ EnsemblePatCreationTest: Testcreation escalier (0.10s)
- EnsemblePatCreationTest: Testcreation logement  [yii\db\Connection::open] 'Opening DB connection: mysql:host=127.0.0.1;dbname=yii2_basic_tests'
  Fixture traitement: cleanTraitementDirectory
  [yii\db\Connection::open] 'Opening DB connection: mysql:host=127.0.0.1;dbname=yii2_basic_tests'
  [app\models\mysql\PdfEnsemblePatrimonial] {}
✔ EnsemblePatCreationTest: Testcreation logement (0.09s)
- EnsemblePatCreationTest: Testcreation sans categorie ged  [yii\db\Connection::open] 'Opening DB connection: mysql:host=127.0.0.1;dbname=yii2_basic_tests'
  Fixture traitement: cleanTraitementDirectory
  [yii\db\Connection::open] 'Opening DB connection: mysql:host=127.0.0.1;dbname=yii2_basic_tests'
  [yii\db\ActiveRecord::insert] 'Model not inserted due to validation error.'
✖ EnsemblePatCreationTest: Testcreation sans categorie ged (0.15s)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Time: 1.01 seconds, Memory: 16.00MB

There was 1 failure:

---------
1) EnsemblePatCreationTest: Testcreation sans categorie ged
 Test  tests/unit/components/ensemblePatrimonial/EnsemblePatCreationTest.php:testcreationSansCategorieGed
Pdf Categorie Ged ID est invalide.
Failed asserting that '0' matches expected 1.
#1  Codeception\Module\Asserts->assertEquals
#2  /data/htdocs/tests/_support/_generated/UnitTesterActions.php:42
#3  /data/htdocs/tests/unit/components/ensemblePatrimonial/EnsemblePatCreationTest.php:206
#4  tests\unit\components\ensemblePatrimonial\EnsemblePatCreationTest->testcreationSansCategorieGed

FAILURES!
Tests: 6, Assertions: 19, Failures: 1.

But if i launch only the relevant test :

Codeception PHP Testing Framework v2.4.0
Powered by PHPUnit 6.5.7 by Sebastian Bergmann and contributors.

  [Db] Connected to yii2_basic_tests
  [Db] Connected to yii2_basic_tests
  [Db] No SQL loaded, loading dump skipped

Unit Tests (1) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Modules: Asserts, Yii2, Db
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- EnsemblePatCreationTest: Testcreation sans categorie ged  [yii\db\Connection::open] 'Opening DB connection: mysql:host=127.0.0.1;dbname=yii2_basic_tests'
  Fixture traitement: cleanTraitementDirectory
  Fixture traitement: cleanTraitementDirectory
  [yii\db\Connection::open] 'Opening DB connection: mysql:host=127.0.0.1;dbname=yii2_basic_tests'
  [yii\db\ActiveRecord::insert] 'Model not inserted due to validation error.'
✔ EnsemblePatCreationTest: Testcreation sans categorie ged (0.11s)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Time: 491 ms, Memory: 12.00MB

OK (1 test, 4 assertions)

When i launch multiple test it seem to loose the event attached beetween test (if i understand it correcly, the app is not "reset" during each test. My event are attached at bootstrap level but event are detached somewhere)

What is the expected result?

no test failure

What do you get instead?

The test search for a datase entry but the event are not fired => test failule

Additional info

Q A
Yii version 2.0.14.1
PHP version 7.0.27
Operating system debian 9.3
Dominus77 commented 6 years ago

It seems to me that this is due to the update of codeception\base to version 2.4.0 When I upgraded to the latest version of this extension, I also broke tests. I decided to install in composer.json

"codeception/base": "~2.3.9"

Tests earned as it should.

vinpel commented 6 years ago

Thanks for the info, i will confirm it monday

vinpel commented 6 years ago

Still go the error after update. composer update log :

 - Removing codeception/phpunit-wrapper (6.0.5)
  - Updating behat/gherkin (v4.5.1 => v4.4.5): Downloading (100%)
  - Updating symfony/dom-crawler (v3.4.4 => v3.4.6): Downloading (100%)
  - Updating symfony/css-selector (v3.4.4 => v3.4.6): Downloading (100%)
  - Updating symfony/browser-kit (v3.4.4 => v3.4.6): Downloading (100%)
  - Updating symfony/yaml (v3.4.4 => v3.4.6): Downloading (100%)
  - Updating symfony/event-dispatcher (v3.4.4 => v3.4.6): Downloading (100%)
  - Updating symfony/debug (v3.4.4 => v3.4.6): Downloading (100%)
  - Updating symfony/console (v3.4.4 => v3.4.6): Downloading (100%)
  - Updating symfony/finder (v3.4.4 => v3.4.6): Downloading (100%)
  - Updating codeception/base (2.4.0 => 2.3.9): Downloading (100%)

"codeception/base": "^2.2.3", =>"codeception/base": "~2.3.9",

Dominus77 commented 6 years ago

I do not know if it helps you or not, but here are the setup logs when there is a problem with codeception / base 2.4.0 https://travis-ci.org/Dominus77/yii2-advanced-start/builds/346727948 It was worth changing the version to 2.3.9 the tests passed on ok! https://travis-ci.org/Dominus77/yii2-advanced-start

lubobill1990 commented 6 years ago

https://github.com/Codeception/Codeception/pull/4621 https://github.com/Codeception/Codeception/pull/4700

I think these discussions are useful for you.

cebe commented 6 years ago

Does not look like something we can fix in yii. Closing the issue.