sebastianbergmann / phpunit

The PHP Unit Testing framework.
https://phpunit.de/
BSD 3-Clause "New" or "Revised" License
19.66k stars 2.2k forks source link

PHPUnit test contaminate each other's state #3044

Closed robertmain closed 6 years ago

robertmain commented 6 years ago
Q A
PHPUnit version 7.0.2
PHP version 7.9.1
Installation Method Composer

My two tests cases seem to be contaminating each other's state. The first test runs and passes because it gets an exception containing the messages it expects. The second test fails because it encounters exception containing The age field must contain a number greater than or equal to 18. which was thrown by an entirely different test case.

Surely, this shouldn't happen?

Test Class

<?php

use PHPUnit\Framework\TestCase;
use Exceptions\Data\ValidationException;
use App\Core\Model as BaseModel;

class Model extends TestCase{

    use \Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;

    /**
     * @var BaseModel Abstract base model instance
     */
    private $model;

    public function setUp()
    {
        $this->model     = Mockery::mock(BaseModel::class)->makePartial();
        $this->model->db = Mockery::mock(CI_DB_query_builder::class);
    }

    /**
     * @test
     */
    public function validation_prevents_insertion_of_invalid_data()
    {
        $this->set_protected_property(
            $this->model,
            'validation_rules',
            [
                'firstname' => 'required',
                'age'       => 'required|greater_than_equal_to[18]'
            ]
        );

        $this->expectException(ValidationException::class);
        $this->expectExceptionMessage('The firstname field is required');
        $this->expectExceptionMessage('The age field must contain a number greater than or equal to 18.');

        $this->model->insert(['lastname' => 'Smith', 'age' => 12]);
    }

    /**
     * @test
    */
    public function validation_prevents_update_with_invalid_data()
    {
        $this->set_protected_property(
            $this->model,
            'validation_rules',
            [
                'firstname' => 'required',
                'age'       => 'is_numeric'
            ]
        );

        $this->expectException(ValidationException::class);
        $this->expectExceptionMessage('The firstname field is required');
        $this->expectExceptionMessage('The age field is required');

        $this->model->update(3, ['lastname' => 'Smith', 'age' => 'twelve']);
    }
}

Composer Info

cilex/cilex                         1.1.0              The PHP micro-framework for Command line tools based on the Symfony2 Components
cilex/console-service-provider      1.0.0              Console Service Provider
codeigniter/framework               3.1.7              The CodeIgniter framework
composer/ca-bundle                  1.1.0              Lets you find a path to the system CA bundle, and includes a fallback to the Mozilla CA bundle.
container-interop/container-interop 1.2.0              Promoting the interoperability of container objects (DIC, SL, etc.)
crazycodr/standard-exceptions       2.1.2              This project is aimed at providing additional standard exceptions to php. Many exceptions that ...
doctrine/annotations                v1.6.0             Docblock Annotations Parser
doctrine/instantiator               1.1.0              A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                      v1.0.1             Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
erusev/parsedown                    1.7.1              Parser for Markdown.
hamcrest/hamcrest-php               v2.0.0             This is the PHP port of Hamcrest Matchers
jms/metadata                        1.6.0              Class/method/property metadata management in PHP
jms/parser-lib                      1.0.0              A library for easily creating recursive-descent parsers.
jms/serializer                      1.7.1              Library for (de-)serializing data of any complexity; supports XML, JSON, and YAML.
league/plates                       3.3.0              Plates, the native PHP template system that's fast, easy to use and easy to extend.
limedeck/phpunit-detailed-printer   4.0.0              Formatter for the detailed output of PHPUnit tests.
mockery/mockery                     1.0                Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit...
monolog/monolog                     1.23.0             Sends your logs to files, sockets, inboxes, databases and various web services
myclabs/deep-copy                   1.7.0              Create deep copies (clones) of your objects
nikic/php-parser                    v1.4.1             A PHP parser written in PHP
padraic/humbug_get_contents         1.1.2              Secure wrapper for accessing HTTPS resources with file_get_contents for PHP 5.3+
padraic/phar-updater                v1.0.5             A thing to make PHAR self-updating easy and secure.
phar-io/manifest                    1.0.1              Component for reading phar.io manifest information from a PHP Archive (PHAR)
phar-io/version                     1.0.1              Library for handling version information and constraints
phpcollection/phpcollection         0.5.0              General-Purpose Collection Library for PHP
phpdocumentor/fileset               1.0.0              Fileset component for collecting a set of files given directories and file paths
phpdocumentor/graphviz              1.0.4
phpdocumentor/phpdocumentor         dev-master 394d2e3 Documentation Generator for PHP
phpdocumentor/reflection            3.0.1              Reflection library to do Static Analysis for PHP Projects
phpdocumentor/reflection-docblock   2.0.5
phpoption/phpoption                 1.5.0              Option Type for PHP
phpspec/prophecy                    1.7.5              Highly opinionated mocking framework for PHP 5.3+
phpunit/php-code-coverage           6.0.1              Library that provides collection, processing, and rendering functionality for PHP code coverage...
phpunit/php-file-iterator           1.4.5              FilterIterator implementation that filters files based on a list of suffixes.
phpunit/php-text-template           1.2.1              Simple template engine.
phpunit/php-timer                   2.0.0              Utility class for timing
phpunit/php-token-stream            3.0.0              Wrapper around PHP's tokenizer extension.
phpunit/phpunit                     7.0.2              The PHP Unit Testing framework.
phpunit/phpunit-mock-objects        6.0.1              Mock Object library for PHPUnit
pimple/pimple                       v1.1.1             Pimple is a simple Dependency Injection Container for PHP 5.3
psr/container                       1.0.0              Common Container Interface (PHP FIG PSR-11)
psr/log                             1.0.2              Common interface for logging libraries
sebastian/code-unit-reverse-lookup  1.0.1              Looks up which function or method a line of code belongs to
sebastian/comparator                2.1.3              Provides the functionality to compare PHP values for equality
sebastian/diff                      3.0.0              Diff implementation
sebastian/environment               3.1.0              Provides functionality to handle HHVM/PHP environments
sebastian/exporter                  3.1.0              Provides the functionality to export PHP variables for visualization
sebastian/global-state              2.0.0              Snapshotting of global state
sebastian/object-enumerator         3.0.3              Traverses array structures and object graphs to enumerate all referenced objects
sebastian/object-reflector          1.1.1              Allows reflection of object attributes, including inherited and non-public ones
sebastian/recursion-context         3.0.0              Provides functionality to recursively process PHP variables
sebastian/resource-operations       1.0.0              Provides a list of PHP built-in functions that operate on resources
sebastian/version                   2.0.1              Library that helps with managing the version number of Git-hosted PHP projects
symfony/config                      v2.8.36            Symfony Config Component
symfony/console                     v2.8.36            Symfony Console Component
symfony/debug                       v3.0.9             Symfony Debug Component
symfony/event-dispatcher            v2.8.36            Symfony EventDispatcher Component
symfony/filesystem                  v3.0.9             Symfony Filesystem Component
symfony/finder                      v2.8.36            Symfony Finder Component
symfony/polyfill-mbstring           v1.7.0             Symfony polyfill for the Mbstring extension
symfony/process                     v2.8.36            Symfony Process Component
symfony/stopwatch                   v2.8.36            Symfony Stopwatch Component
symfony/translation                 v3.0.9             Symfony Translation Component
symfony/validator                   v2.8.36            Symfony Validator Component
theseer/tokenizer                   1.1.0              A small library for converting tokenized PHP source code into XML and potentially other formats
twig/twig                           v1.35.2            Twig, the flexible, fast, and secure template language for PHP
vlucas/phpdotenv                    v2.4.0             Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
webmozart/assert                    1.3.0              Assertions to validate method input/output with nice error messages.
zendframework/zend-cache            2.7.2              provides a generic way to cache any data
zendframework/zend-config           2.6.0              provides a nested object property based user interface for accessing this configuration data wi...
zendframework/zend-eventmanager     3.2.0              Trigger and listen to events within a PHP application
zendframework/zend-filter           2.7.2              provides a set of commonly needed data filters
zendframework/zend-hydrator         1.1.0
zendframework/zend-i18n             2.7.4
zendframework/zend-json             3.1.0              provides convenience methods for serializing native PHP to JSON and decoding JSON to native PHP
zendframework/zend-serializer       2.8.1              provides an adapter based interface to simply generate storable representation of PHP types by ...
zendframework/zend-servicemanager   2.7.10
zendframework/zend-stdlib           2.7.7
zetacomponents/base                 1.9.1              The Base package provides the basic infrastructure that all packages rely on. Therefore every c...
zetacomponents/document             1.3.1              The Document components provides a general conversion framework for different semantic document...
sebastianbergmann commented 6 years ago

Thank you for your report.

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

sebastianbergmann commented 6 years ago

No feedback, closing.