Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
l-vo committed May 28, 2019
1 parent 2835bcb commit a2af18a
Showing 1 changed file with 1 addition and 141 deletions.
Expand Up @@ -11,30 +11,11 @@

namespace Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler;

use Composer\Autoload\ClassLoader;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
use Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerForV5;

class DeprecationTest extends TestCase
{
public static function setUpBeforeClass(): void
{
$vendorDir = self::getVendorDir();

mkdir($vendorDir.'/myfakevendor/myfakepackage1', 0777, true);
mkdir($vendorDir.'/myfakevendor/myfakepackage2');
touch($vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php');
touch($vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile2.php');
touch($vendorDir.'/myfakevendor/myfakepackage2/MyFakeFile.php');
}

private static function getVendorDir(): string
{
$reflection = new \ReflectionClass(ClassLoader::class);
return dirname($reflection->getFileName(), 2);
}

public function testItCanDetermineTheClassWhereTheDeprecationHappened()
{
$deprecation = new Deprecation('💩', $this->debugBacktrace(), __FILE__);
Expand Down Expand Up @@ -68,133 +49,12 @@ public function testItRulesOutFilesOutsideVendorsAsIndirect()
$this->assertFalse($deprecation->isIndirect());
}

public function providerIsSelf(): array
{
return [
[true, '', 'MyClass1', ''],
[false, '', 'MyClass1', 'dummy_vendor_path'],
[
true,
serialize(['class' => '', 'method' => '', 'deprecation' => '', 'files_stack' => []]),
SymfonyTestsListenerForV5::class,
'',
],
[
true,
serialize([
'class' => '',
'method' => '',
'deprecation' => '',
'triggering_file' => '',
'files_stack' => [],
]),
SymfonyTestsListenerForV5::class,
'',
],
[
false,
serialize([
'class' => '',
'method' => '',
'deprecation' => '',
'triggering_file' => 'dummy_vendor_path',
'files_stack' => [],
]),
SymfonyTestsListenerForV5::class,
'',
],
];
}

/**
* @dataProvider providerIsSelf
*/
/*public function testIsSelf(bool $expectedIsSelf, string $message, string $traceClass, string $file): void
{
$trace = [
['class' => 'MyClass1', 'function' => 'myMethod'],
['class' => $traceClass, 'function' => 'myMethod'],
];
$deprecation = new Deprecation($message, $trace, $file);
$this->assertEquals($expectedIsSelf, $deprecation->isSelf());
}*/

public function providerIsIndirectUsesRightTrace(): array
{
$vendorDir = self::getVendorDir();

return [
'no_file_in_stack' => [false, '', [['function' => 'myfunc1'], ['function' => 'myfunc2']]],
'files_in_stack_from_various_packages' => [
true,
'',
[
['function' => 'myfunc1', 'file' => $vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php'],
['function' => 'myfunc2', 'file' => $vendorDir.'/myfakevendor/myfakepackage2/MyFakeFile.php'],
],
],
'serialized_stack_files_from_same_package' => [
false,
serialize([
'deprecation' => 'My deprecation message',
'class' => 'MyClass',
'method' => 'myMethod',
'files_stack' => [
$vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php',
$vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile2.php',
],
]),
[['function' => 'myfunc1'], ['class' => SymfonyTestsListenerForV5::class, 'method' => 'mymethod']],
],
'serialized_stack_files_from_various_packages' => [
true,
serialize([
'deprecation' => 'My deprecation message',
'class' => 'MyClass',
'method' => 'myMethod',
'files_stack' => [
$vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php',
$vendorDir.'/myfakevendor/myfakepackage2/MyFakeFile.php',
],
]),
[['function' => 'myfunc1'], ['class' => SymfonyTestsListenerForV5::class, 'method' => 'mymethod']],
],
];
}

/**
* @dataProvider providerIsIndirectUsesRightTrace
*/
/*public function testIsIndirectUsesRightTrace(bool $expectedIsIndirect, string $message, array $trace): void
{
$deprecation = new Deprecation($message, $trace, '');
$this->assertEquals($expectedIsIndirect, $deprecation->isIndirect());
}*/

/**
* This method is here to simulate the extra level from the piece of code
* triggering an error to the error handler.
* triggering an error to the error handler
*/
public function debugBacktrace(): array
{
return debug_backtrace();
}

private static function removeDir($dir): void
{
$files = glob($dir.'/*');
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
} else {
self::removeDir($file);
}
}
rmdir($dir);
}

public static function tearDownAfterClass(): void
{
self::removeDir(self::getVendorDir().'/myfakevendor');
}
}

0 comments on commit a2af18a

Please sign in to comment.