parent
a0c873261c
commit
7862541161
5 changed files with 194 additions and 3 deletions
@ -0,0 +1,71 @@ |
||||
<?php |
||||
|
||||
use TypePhp\CompilerBase; |
||||
use TypePhp\CompilerTest; |
||||
|
||||
final class InternalParentExtensionCodegenTest extends BaseTest |
||||
{ |
||||
private string $projectDir; |
||||
|
||||
protected function setUp(): void |
||||
{ |
||||
parent::setUp(); |
||||
$this->projectDir = sys_get_temp_dir() . '/typephp_internal_parent_' . bin2hex(random_bytes(6)); |
||||
mkdir($this->projectDir, 0777, true); |
||||
} |
||||
|
||||
protected function tearDown(): void |
||||
{ |
||||
$this->removeDirectory($this->projectDir); |
||||
parent::tearDown(); |
||||
} |
||||
|
||||
public function testExtensionResolvesInternalParentFromCompilerClassTable(): void |
||||
{ |
||||
$source = $this->projectDir . '/internal-parent.php'; |
||||
file_put_contents($source, <<<'PHP' |
||||
<?php |
||||
|
||||
class InternalParentChild extends ArrayObject |
||||
{ |
||||
} |
||||
PHP); |
||||
|
||||
global $translator; |
||||
$compiler = CompilerTest::create($this->projectDir); |
||||
$translator = $compiler; |
||||
$compiler->setBuildMode(CompilerBase::BUILD_MODE_EXT); |
||||
$compiler->setTargetName('internal_parent'); |
||||
$compiler->addFiles([$source]); |
||||
$compiler->prepareFile($source); |
||||
$compiler->convertFile($source); |
||||
|
||||
$extension = file_get_contents($compiler->genExtension()); |
||||
|
||||
self::assertStringContainsString( |
||||
'php_class_entry_ArrayObject = get_internal_class("ArrayObject");', |
||||
$extension, |
||||
); |
||||
self::assertStringNotContainsString( |
||||
'php_class_entry_ArrayObject = php::getClassEntrySafe("ArrayObject");', |
||||
$extension, |
||||
); |
||||
} |
||||
|
||||
private function removeDirectory(string $directory): void |
||||
{ |
||||
if (!is_dir($directory)) { |
||||
return; |
||||
} |
||||
|
||||
foreach (array_diff(scandir($directory), ['.', '..']) as $entry) { |
||||
$path = $directory . '/' . $entry; |
||||
if (is_dir($path)) { |
||||
$this->removeDirectory($path); |
||||
} else { |
||||
unlink($path); |
||||
} |
||||
} |
||||
rmdir($directory); |
||||
} |
||||
} |
||||
@ -0,0 +1,64 @@ |
||||
--TEST-- |
||||
throw accepts object-valued method call results |
||||
--FILE-- |
||||
<?php |
||||
|
||||
class ExceptionFactory |
||||
{ |
||||
private function typedException(): LogicException |
||||
{ |
||||
return new LogicException('typed'); |
||||
} |
||||
|
||||
private function objectException(): object |
||||
{ |
||||
return new RuntimeException('object'); |
||||
} |
||||
|
||||
private function nonThrowable(): stdClass |
||||
{ |
||||
return new stdClass(); |
||||
} |
||||
|
||||
public function throwTyped(): void |
||||
{ |
||||
throw $this->typedException(); |
||||
} |
||||
|
||||
public function throwObject(): void |
||||
{ |
||||
throw $this->objectException(); |
||||
} |
||||
|
||||
public function throwNonThrowable(): void |
||||
{ |
||||
throw $this->nonThrowable(); |
||||
} |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
$factory = new ExceptionFactory(); |
||||
try { |
||||
$factory->throwTyped(); |
||||
} catch (Throwable $e) { |
||||
echo get_class($e), ':', $e->getMessage(), "\n"; |
||||
} |
||||
|
||||
try { |
||||
$factory->throwObject(); |
||||
} catch (Throwable $e) { |
||||
echo get_class($e), ':', $e->getMessage(), "\n"; |
||||
} |
||||
|
||||
try { |
||||
$factory->throwNonThrowable(); |
||||
} catch (Error $e) { |
||||
echo $e->getMessage(), "\n"; |
||||
} |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
LogicException:typed |
||||
RuntimeException:object |
||||
Cannot throw objects that do not implement Throwable |
||||
@ -0,0 +1,32 @@ |
||||
--TEST-- |
||||
toString keyword converts fluent object results through __toString |
||||
--FILE-- |
||||
<?php |
||||
|
||||
final class FluentText |
||||
{ |
||||
public function __construct(private string $value) |
||||
{ |
||||
} |
||||
|
||||
public function append(string ...$values): self |
||||
{ |
||||
$this->value .= implode('', $values); |
||||
return $this; |
||||
} |
||||
|
||||
public function __toString(): string |
||||
{ |
||||
return $this->value; |
||||
} |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
$start = new FluentText('start'); |
||||
$end = new FluentText('end'); |
||||
echo $start->append(':', $end->toString())->toString(), "\n"; |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
start:end |
||||
Loading…
Reference in new issue