Skip to content

Commit

Permalink
Fix subnamespace when there is no namespace to avoid issues with the …
Browse files Browse the repository at this point in the history
…base class

When creating a new build configuration without namespace and adding later on a subnamespace to it (e.g. using getTestContainer() will add a subnamespace 'Test') the dumped file will have an invalid namespace '\Test' and that will make the di compiler fail.
  • Loading branch information
lgrossi committed May 5, 2023
1 parent 9b78fbc commit fecb467
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/infection.log
/.phpunit.cache
/build
/.idea
123 changes: 123 additions & 0 deletions bin/phpunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/usr/bin/env php
<?php

/**
* Proxy PHP file generated by Composer
*
* This file includes the referenced bin path (../phpunit/phpunit/phpunit)
* using a stream wrapper to prevent the shebang from being output on PHP<8
*
* @generated
*/

namespace Composer;

$GLOBALS['_composer_bin_dir'] = __DIR__;
$GLOBALS['_composer_autoload_path'] = __DIR__ . '/..'.'/autoload.php';
$GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'] = $GLOBALS['__PHPUNIT_ISOLATION_BLACKLIST'] = array(realpath(__DIR__ . '/..'.'/phpunit/phpunit/phpunit'));

if (PHP_VERSION_ID < 80000) {
if (!class_exists('Composer\BinProxyWrapper')) {
/**
* @internal
*/
final class BinProxyWrapper
{
private $handle;
private $position;
private $realpath;

public function stream_open($path, $mode, $options, &$opened_path)
{
// get rid of phpvfscomposer:// prefix for __FILE__ & __DIR__ resolution
$opened_path = substr($path, 17);
$this->realpath = realpath($opened_path) ?: $opened_path;
$opened_path = 'phpvfscomposer://'.$this->realpath;
$this->handle = fopen($this->realpath, $mode);
$this->position = 0;

return (bool) $this->handle;
}

public function stream_read($count)
{
$data = fread($this->handle, $count);

if ($this->position === 0) {
$data = preg_replace('{^#!.*\r?\n}', '', $data);
}
$data = str_replace('__DIR__', var_export(dirname($this->realpath), true), $data);
$data = str_replace('__FILE__', var_export($this->realpath, true), $data);

$this->position += strlen($data);

return $data;
}

public function stream_cast($castAs)
{
return $this->handle;
}

public function stream_close()
{
fclose($this->handle);
}

public function stream_lock($operation)
{
return $operation ? flock($this->handle, $operation) : true;
}

public function stream_seek($offset, $whence)
{
if (0 === fseek($this->handle, $offset, $whence)) {
$this->position = ftell($this->handle);
return true;
}

return false;
}

public function stream_tell()
{
return $this->position;
}

public function stream_eof()
{
return feof($this->handle);
}

public function stream_stat()
{
return array();
}

public function stream_set_option($option, $arg1, $arg2)
{
return true;
}

public function url_stat($path, $flags)
{
$path = substr($path, 17);
if (file_exists($path)) {
return stat($path);
}

return false;
}
}
}

if (
(function_exists('stream_get_wrappers') && in_array('phpvfscomposer', stream_get_wrappers(), true))
|| (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper'))
) {
include("phpvfscomposer://" . __DIR__ . '/..'.'/phpunit/phpunit/phpunit');
exit(0);
}
}

include __DIR__ . '/..'.'/phpunit/phpunit/phpunit';
2 changes: 1 addition & 1 deletion src/Config/ContainerConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function getDumpOptions(): array
{
$options = [
'class' => self::CLASS_NAME,
'namespace' => $this->namespace,
'namespace' => ltrim($this->namespace, '\\'),
];

if ($this->baseClass !== null) {
Expand Down
30 changes: 30 additions & 0 deletions test/Config/ContainerConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,36 @@ public function getDumpOptionsShouldIncludeBaseWhenWasConfigured(): void
self::assertSame($options, $config->getDumpOptions());
}

#[PHPUnit\Test]
public function getDumpOptionsNamespaceShouldNeverStartWithBackSlash(): void
{
$config = new ContainerConfiguration('\\MyApp');
$config->setBaseClass('Test');

$options = [
'class' => ContainerConfiguration::CLASS_NAME,
'namespace' => 'MyApp',
'base_class' => '\\Test',
'hot_path_tag' => 'container.hot_path',
];

self::assertSame($options, $config->getDumpOptions());
}

#[PHPUnit\Test]
public function getDumpOptionsNamespaceShouldAcceptSubNamespaceWithoutMainNamespace(): void
{
$config =(new ContainerConfiguration(''))->withSubNamespace('Test');

$options = [
'class' => ContainerConfiguration::CLASS_NAME,
'namespace' => 'Test',
'hot_path_tag' => 'container.hot_path',
];

self::assertSame($options, $config->getDumpOptions());
}

#[PHPUnit\Test]
public function withAddedNamespaceShouldModifyTheNamespaceOfANewInstanceOnly(): void
{
Expand Down

0 comments on commit fecb467

Please sign in to comment.