Codeception / Specify

BDD style code blocks for PHPUnit / Codeception
MIT License
156 stars 22 forks source link

Code inside "specify" closure changes the value of the class attribute #6

Closed kop closed 10 years ago

kop commented 10 years ago

Hello. I have the following test class:

class ValidatorTest extends \Codeception\TestCase\Test
{
    use \Codeception\Specify;

    /**
     * @var \mb\tools\url\Validator $instance ;
     */
    public $instance;

    /**
     * @var \UnitTester
     */
    protected $tester;

    /**
     * @inheritdoc
     */
    protected function _before()
    {
        $this->instance = new \mb\tools\url\Validator();
        $this->assertInstanceOf('\mb\tools\url\Validator', $this->instance);
    }

    /**
     * @covers \mb\tools\url\Validator::url
     */
    public function testUrl()
    {
        $dataDir = \Codeception\Configuration::dataDir();
        $urlSamples = include("{$dataDir}/url_samples.php");

        var_dump($this->instance->validSchemes);

        $this->specify('check URLs with invalid scheme if $validSchemes property is set', function ($testData) {
            $this->instance->validSchemes = ['https', 'ftps'];
            verify($this->instance->url($testData))->false();
        }, ['examples' => $urlSamples['restrictedScheme']]);

        var_dump($this->instance->validSchemes);
        die;
    }
}

The results are:

array(2) {
  [0] => string(4) "http"
  [1] => string(5) "https"
}
array(2) {
  [0] => string(5) "https"
  [1] => string(4) "ftps"
}

Means that $this->instance class attribute has been changed. What am I doing wrong?

For now, I was able to workaround this with help of

        $this->beforeSpecify(function() {
            $this->instance = new \mb\tools\url\Validator();
        });

But it doesn't looks like correct solution.

DavertMik commented 10 years ago

Fixed in https://github.com/Codeception/Specify/commit/f040e2e54b74155c5c699d880b6f400febf299f6