Pest: it and depends

Created on 31 Oct 2020  ยท  1Comment  ยท  Source: pestphp/pest

When a test created with "it", the depends function doesn't work.

````php
it('can create a converter', function () {
$factory = new ConverterFactory();
$factory->register('md', MarkdownConverter::class);
$converter = $factory->createConverter('md');
expect($converter)->toBeInstanceOf(MarkdownConverter::class);
return $converter;
});

it('can convert markdown', function ($converter) {
$html = $converter->convert('TEST');
expect($html)->toBe('

TEST

');
})->depends('it can create a converter');
````
This results in the following output:

````
WARN Tests\Core\Infrastructure\Converter\ConverterFactoryTest
โœ“ it can create a converter
! it can convert markdown โ†’ This test depends on "P\Tests\Core\Infrastructure\Converter\ConverterFactoryTest::can create a converter" which does not exist.

Tests: 1 warnings, 1 passed
Time: 0.24s
````
Leaving "it" out of the testname doesn't work, but adding "it" twice to the testname works:

php it('can convert markdown', function ($converter) { $html = $converter->convert('**TEST**'); expect($html)->toBe('<p><strong>TEST</strong></p>'); })->depends('it it can create a converter');
````
PASS Tests\Core\Infrastructure\Converter\ConverterFactoryTest
โœ“ it can create a converter
โœ“ it can convert markdown

Tests: 2 passed
Time: 0.29s
````

bug

Most helpful comment

Sorry it's taken so long to reply, I was able to replicate this issue. It looks like this is due to our use of ExecutionOrderDependency in the TestCall object.

The createFromDependsAnnotation call splits the string by a space, which allows support for "clone options".

We should create our own version of this method that doesn't split the call. ๐Ÿ‘๐Ÿป Basically just replace the array_map() in TestCall::depends() with:

$tests = array_map(function (string $test) use ($className): ExecutionOrderDependency {
    if (strpos($test, '::') === false) {
        $test = "{$className}::{$test}";
    }

    return new ExecutionOrderDependency($test, null, '');
}, $tests);

I've opened #216 to resolve this issue.

>All comments

Sorry it's taken so long to reply, I was able to replicate this issue. It looks like this is due to our use of ExecutionOrderDependency in the TestCall object.

The createFromDependsAnnotation call splits the string by a space, which allows support for "clone options".

We should create our own version of this method that doesn't split the call. ๐Ÿ‘๐Ÿป Basically just replace the array_map() in TestCall::depends() with:

$tests = array_map(function (string $test) use ($className): ExecutionOrderDependency {
    if (strpos($test, '::') === false) {
        $test = "{$className}::{$test}";
    }

    return new ExecutionOrderDependency($test, null, '');
}, $tests);

I've opened #216 to resolve this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nunomaduro picture nunomaduro  ยท  7Comments

beliolfa picture beliolfa  ยท  3Comments

dingo-d picture dingo-d  ยท  6Comments

rihardssceredins picture rihardssceredins  ยท  5Comments

m1guelpf picture m1guelpf  ยท  6Comments