-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #451.
- Loading branch information
Showing
4 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the humbug/php-scoper package. | ||
* | ||
* Copyright (c) 2017 Théo FIDRY <[email protected]>, | ||
* Pádraic Brady <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* Creates a patcher able to fix the paths of the Symfony PHP configuration files. | ||
* | ||
* @param string|array<string> $filesPath | ||
*/ | ||
return static function (array|string $fileOrFilesPath): Closure { | ||
$filesPath = (array) $fileOrFilesPath; | ||
|
||
return static function (string $filePath, string $prefix, string $contents) use ($filesPath): string { | ||
if (!in_array($filePath, $filesPath, true)) { | ||
return $contents; | ||
} | ||
|
||
return preg_replace( | ||
'/(.*->load\((?:\n\s+)?\')(.+?\\\\)(\',.*)/', | ||
'$1'.$prefix.'\\\\$2$3', | ||
$contents, | ||
); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the humbug/php-scoper package. | ||
* | ||
* Copyright (c) 2017 Théo FIDRY <[email protected]>, | ||
* Pádraic Brady <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Humbug\PhpScoper\Patcher; | ||
|
||
use Closure; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class SymfonyPhpServicesPatcherTest extends TestCase | ||
{ | ||
private const FILE_PATH = 'path/to/config.php'; | ||
|
||
private Closure $patcher; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->patcher = (require __DIR__.'/../../res/create-symfony-php-services-patcher.php')([self::FILE_PATH]); | ||
} | ||
|
||
/** | ||
* @dataProvider symfonyConfigFileProvider | ||
*/ | ||
public function test_it_can_patch_a_symfony_service_file( | ||
string $prefix, | ||
string $contents, | ||
string $expected, | ||
): void { | ||
$actual = ($this->patcher)(self::FILE_PATH, $prefix, $contents); | ||
|
||
self::assertSame($expected, $actual); | ||
} | ||
|
||
public static function symfonyConfigFileProvider(): iterable | ||
{ | ||
$prefix = 'Prefix'; | ||
|
||
yield 'load statement' => [ | ||
$prefix, | ||
<<<'PHP' | ||
use SomeNamespace\SomeClass; | ||
return static function (ContainerConfigurator $containerConfigurator) { | ||
$services = $containerConfigurator->services(); | ||
$services->load('SomeNamespace\ConsoleColorDiff\\', __DIR__ . '/../src'); | ||
$services->set(SomeClass::class); | ||
} | ||
PHP, | ||
<<<'PHP' | ||
use SomeNamespace\SomeClass; | ||
return static function (ContainerConfigurator $containerConfigurator) { | ||
$services = $containerConfigurator->services(); | ||
$services->load('Prefix\SomeNamespace\ConsoleColorDiff\\', __DIR__ . '/../src'); | ||
$services->set(SomeClass::class); | ||
} | ||
PHP, | ||
]; | ||
|
||
yield 'multiline load statement' => [ | ||
$prefix, | ||
<<<'PHP' | ||
use SomeNamespace\SomeClass; | ||
return static function (ContainerConfigurator $containerConfigurator) { | ||
$services = $containerConfigurator->services(); | ||
$services->load( | ||
'SomeNamespace\ConsoleColorDiff\\', | ||
__DIR__ . '/../src', | ||
); | ||
$services->set(SomeClass::class); | ||
} | ||
PHP, | ||
<<<'PHP' | ||
use SomeNamespace\SomeClass; | ||
return static function (ContainerConfigurator $containerConfigurator) { | ||
$services = $containerConfigurator->services(); | ||
$services->load( | ||
'Prefix\SomeNamespace\ConsoleColorDiff\\', | ||
__DIR__ . '/../src', | ||
); | ||
$services->set(SomeClass::class); | ||
} | ||
PHP, | ||
]; | ||
} | ||
} |