Skip to content

Commit

Permalink
Merge pull request #23 from doctrine/GH-20-FixConditionForTestsFolder
Browse files Browse the repository at this point in the history
[GH-20] Fix tests condition for detecting outside/inside code and imp…
  • Loading branch information
beberlei authored Mar 13, 2021
2 parents 7a0388d + 6df3bae commit d7eade7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 20 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
},
"autoload-dev": {
"psr-4": {
"DeprecationTests\\": "tests/fixtures/src",
"Doctrine\\Foo\\": "tests/fixtures/vendor/doctrine/foo"
"DeprecationTests\\": "test_fixtures/src",
"Doctrine\\Foo\\": "test_fixtures/vendor/doctrine/foo"
}
}
}
18 changes: 9 additions & 9 deletions lib/Doctrine/Deprecations/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ public static function triggerIfCalledFromOutside(string $package, string $link,
// dependency and the caller is not a file in that package.
// When $package is installed as a root package, then this deprecation
// is always ignored
if (
strpos($backtrace[0]['file'], '/vendor/' . $package . '/') === false &&
strpos($backtrace[1]['file'], '/tests/') !== false
) {
return;
}

if (strpos($backtrace[1]['file'], '/vendor/' . $package . '/') !== false) {
return;
// first check that the caller is not from a tests folder, in which case we always let deprecations pass
if (strpos($backtrace[1]['file'], '/tests/') === false) {
if (strpos($backtrace[0]['file'], '/vendor/' . $package . '/') === false) {
return;
}

if (strpos($backtrace[1]['file'], '/vendor/' . $package . '/') !== false) {
return;
}
}

if (array_key_exists($link, self::$ignoredLinks)) {
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions test_fixtures/src/RootDeprecation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace DeprecationTests;

use Doctrine\Deprecations\Deprecation;

class RootDeprecation
{
public static function run()
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/orm',
'https://github.com/doctrine/deprecations/4444',
'this is deprecated %s %d',
'foo',
1234
);

}
}
File renamed without changes.
File renamed without changes.
13 changes: 4 additions & 9 deletions tests/Doctrine/Deprecations/DeprecationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Deprecations;

use DeprecationTests\Foo;
use DeprecationTests\RootDeprecation;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\Foo\Baz;
use PHPUnit\Framework\Error\Deprecated;
Expand Down Expand Up @@ -229,24 +230,18 @@ public function testDeprecationCalledFromOutsideInRoot(): void
Deprecation::enableWithTriggerError();

$this->expectDeprecation();
$this->expectDeprecationMessage('this is deprecated foo 1234 (DeprecationTest.php');
$this->expectDeprecationMessage('this is deprecated foo 1234 (RootDeprecation.php');

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/deprecations/4444');

$e = null;
try {
Deprecation::triggerIfCalledFromOutside(
'doctrine/orm',
'https://github.com/doctrine/deprecations/4444',
'this is deprecated %s %d',
'foo',
1234
);
RootDeprecation::run();

$this->fail('Should never be reached because of deprecation exception');
} catch (Throwable $e) {
$this->assertStringMatchesFormat(
'this is deprecated foo 1234 (DeprecationTest.php:%d called by TestCase.php:%d, https://github.com/doctrine/deprecations/4444, package doctrine/orm)',
'this is deprecated foo 1234 (RootDeprecation.php:%d called by DeprecationTest.php:%d, https://github.com/doctrine/deprecations/4444, package doctrine/orm)',
$e->getMessage()
);
$this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount());
Expand Down

0 comments on commit d7eade7

Please sign in to comment.