Skip to content

Commit

Permalink
refactor: add typed on private property based on assigns (#1200)
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Jan 10, 2025
1 parent e213ac7 commit dffd6d4
Show file tree
Hide file tree
Showing 33 changed files with 384 additions and 658 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"ext-json": "*",
"php": ">=8.1",
"myclabs/deep-copy": "^1.9",
"spiral/core": "^3.14.9"
"spiral/core": "^3.15"
},
"require-dev": {
"phpunit/phpunit": "^10.1",
Expand Down
108 changes: 54 additions & 54 deletions tests/BufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class BufferTest extends TestCase
public function testNext(): void
{
$src = $this->buffer('abc');
$this->assertEquals(new Byte(0, 'a'), $src->next());
$this->assertEquals(new Byte(1, 'b'), $src->next());
$this->assertEquals(new Byte(2, 'c'), $src->next());
$this->assertEquals(null, $src->next());
self::assertEquals(new Byte(0, 'a'), $src->next());
self::assertEquals(new Byte(1, 'b'), $src->next());
self::assertEquals(new Byte(2, 'c'), $src->next());
self::assertEquals(null, $src->next());
}

public function testIterate(): void
Expand All @@ -29,117 +29,117 @@ public function testIterate(): void
$out .= $n->char;
}

$this->assertEquals('abc', $out);
self::assertSame('abc', $out);
}

public function testGetBytes(): void
{
$src = $this->buffer('abc');
$this->assertEquals('abc', $src->nextBytes());
self::assertEquals('abc', $src->nextBytes());

$this->assertEquals('', $src->nextBytes());
self::assertEquals('', $src->nextBytes());
}

public function testLookahead(): void
{
$src = $this->buffer('abc');
$this->assertEquals('a', $src->lookahead()->char);
$this->assertEquals(0, $src->lookahead()->offset);
self::assertEquals('a', $src->lookahead()->char);
self::assertEquals(0, $src->lookahead()->offset);

// no iteration expected
$this->assertEquals('a', $src->lookahead()->char);
$this->assertEquals(0, $src->lookahead()->offset);
self::assertEquals('a', $src->lookahead()->char);
self::assertEquals(0, $src->lookahead()->offset);

$this->assertEquals(new Byte(0, 'a'), $src->next());
self::assertEquals(new Byte(0, 'a'), $src->next());

$this->assertEquals('b', $src->lookahead()->char);
$this->assertEquals(1, $src->lookahead()->offset);
self::assertEquals('b', $src->lookahead()->char);
self::assertEquals(1, $src->lookahead()->offset);

// no iteration expected
$this->assertEquals('b', $src->lookahead()->char);
$this->assertEquals(1, $src->lookahead()->offset);
self::assertEquals('b', $src->lookahead()->char);
self::assertEquals(1, $src->lookahead()->offset);

$this->assertEquals(new Byte(1, 'b'), $src->next());
self::assertEquals(new Byte(1, 'b'), $src->next());

$this->assertEquals('c', $src->lookahead()->char);
$this->assertEquals(2, $src->lookahead()->offset);
self::assertEquals('c', $src->lookahead()->char);
self::assertEquals(2, $src->lookahead()->offset);

// no iteration expected
$this->assertEquals('c', $src->lookahead()->char);
$this->assertEquals(2, $src->lookahead()->offset);
self::assertEquals('c', $src->lookahead()->char);
self::assertEquals(2, $src->lookahead()->offset);

$this->assertEquals(new Byte(2, 'c'), $src->next());
self::assertEquals(new Byte(2, 'c'), $src->next());

$this->assertEquals(null, $src->lookahead());
$this->assertEquals(null, $src->lookahead());
$this->assertEquals(null, $src->next());
self::assertEquals(null, $src->lookahead());
self::assertEquals(null, $src->lookahead());
self::assertEquals(null, $src->next());
}

public function testLookaheadByte(): void
{
$src = $this->buffer('abc');
$this->assertEquals('a', $src->lookaheadByte());
self::assertEquals('a', $src->lookaheadByte());

$this->assertEquals(new Byte(0, 'a'), $src->next());
$this->assertEquals('b', $src->lookaheadByte());
self::assertEquals(new Byte(0, 'a'), $src->next());
self::assertEquals('b', $src->lookaheadByte());

$this->assertEquals(new Byte(1, 'b'), $src->next());
$this->assertEquals('c', $src->lookaheadByte());
self::assertEquals(new Byte(1, 'b'), $src->next());
self::assertEquals('c', $src->lookaheadByte());

$this->assertEquals(new Byte(2, 'c'), $src->next());
$this->assertEquals(null, $src->lookaheadByte());
self::assertEquals(new Byte(2, 'c'), $src->next());
self::assertEquals(null, $src->lookaheadByte());
}

public function testReplay(): void
{
$src = $this->buffer('abc');
$this->assertEquals(new Byte(0, 'a'), $a = $src->next());
$this->assertEquals(new Byte(1, 'b'), $b = $src->next());
$this->assertEquals(new Byte(2, 'c'), $c = $src->next());
self::assertEquals(new Byte(0, 'a'), $a = $src->next());
self::assertEquals(new Byte(1, 'b'), $b = $src->next());
self::assertEquals(new Byte(2, 'c'), $c = $src->next());

$src->replay($a->offset);
$this->assertEquals(new Byte(1, 'b'), $src->next());
$this->assertEquals(new Byte(2, 'c'), $src->next());
self::assertEquals(new Byte(1, 'b'), $src->next());
self::assertEquals(new Byte(2, 'c'), $src->next());

$src->replay($b->offset);
$this->assertEquals(new Byte(2, 'c'), $src->next());
self::assertEquals(new Byte(2, 'c'), $src->next());

$src->replay($c->offset);
$this->assertEquals(null, $src->next());
self::assertEquals(null, $src->next());
}

public function testOffset(): void
{
$src = $this->buffer('abc');
$this->assertEquals(0, $src->getOffset());
self::assertEquals(0, $src->getOffset());

$this->assertEquals(new Byte(0, 'a'), $src->next());
$this->assertEquals(0, $src->getOffset());
self::assertEquals(new Byte(0, 'a'), $src->next());
self::assertEquals(0, $src->getOffset());

$this->assertEquals(new Byte(1, 'b'), $src->next());
$this->assertEquals(1, $src->getOffset());
self::assertEquals(new Byte(1, 'b'), $src->next());
self::assertEquals(1, $src->getOffset());

$this->assertEquals(new Byte(2, 'c'), $src->next());
$this->assertEquals(2, $src->getOffset());
self::assertEquals(new Byte(2, 'c'), $src->next());
self::assertEquals(2, $src->getOffset());

$src = new Buffer($this->generateToken(new StringStream('abc')));
$this->assertEquals(new Token(0, null, 'a'), $src->next());
$this->assertEquals(0, $src->getOffset());
self::assertEquals(new Token(0, null, 'a'), $src->next());
self::assertSame(0, $src->getOffset());
}

public function testLookupBytes(): void
{
$src = $this->buffer('abc');
$this->assertEquals(0, $src->getOffset());
self::assertEquals(0, $src->getOffset());

$this->assertEquals('ab', $src->lookaheadByte(2));
self::assertEquals('ab', $src->lookaheadByte(2));

$this->assertEquals(new Byte(0, 'a'), $src->next());
self::assertEquals(new Byte(0, 'a'), $src->next());

$this->assertEquals('bc', $src->lookaheadByte(2));
$this->assertEquals('bc', $src->lookaheadByte(3));
self::assertEquals('bc', $src->lookaheadByte(2));
self::assertEquals('bc', $src->lookaheadByte(3));

$this->assertEquals(new Byte(1, 'b'), $src->next());
self::assertEquals(new Byte(1, 'b'), $src->next());
}

protected function buffer(string $string)
Expand Down
2 changes: 1 addition & 1 deletion tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testRaw(): void
$builder = $this->getBuilder(new StringLoader());
$builder->getLoader()->set('home', 'hello world');

$this->assertSame('hello world', $builder->compile('home')->getContent());
self::assertSame('hello world', $builder->compile('home')->getContent());
}

public function testInvalidPath(): void
Expand Down
10 changes: 2 additions & 8 deletions tests/Compiler/DynamicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public function testOutput(): void
{
$doc = $this->parse('{{ $name }}');

$this->assertSame(
"<?php echo htmlspecialchars((string) (\$name), ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8'); ?>",
$this->compile($doc)
);
self::assertSame("<?php echo htmlspecialchars((string) (\$name), ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8'); ?>", $this->compile($doc));
}

public function testOutputEscapeOptions(): void
Expand All @@ -41,9 +38,6 @@ public function testOutputEscapeOptions(): void

$doc->nodes[0]->filter = 'e(%s)';

$this->assertSame(
'<?php echo e($name); ?>',
$this->compile($doc)
);
self::assertSame('<?php echo e($name); ?>', $this->compile($doc));
}
}
10 changes: 5 additions & 5 deletions tests/Compiler/HTMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,34 @@ public function testCompileRaw(): void
{
$doc = $this->parse('<a href="google.com">click me</a>');

$this->assertSame('<a href="google.com">click me</a>', $this->compile($doc));
self::assertSame('<a href="google.com">click me</a>', $this->compile($doc));
}

public function testCompileNested(): void
{
$doc = $this->parse('<a href="google.com"><b>click me</b></a>');

$this->assertSame('<a href="google.com"><b>click me</b></a>', $this->compile($doc));
self::assertSame('<a href="google.com"><b>click me</b></a>', $this->compile($doc));
}

public function testCompileNestedSingleQuote(): void
{
$doc = $this->parse('<a href=\'google.com\'><b>click me</b></a>');

$this->assertSame('<a href=\'google.com\'><b>click me</b></a>', $this->compile($doc));
self::assertSame('<a href=\'google.com\'><b>click me</b></a>', $this->compile($doc));
}

public function testCompileVoid(): void
{
$doc = $this->parse('<br>');

$this->assertSame('<br/>', $this->compile($doc));
self::assertSame('<br/>', $this->compile($doc));
}

public function testCompileScript(): void
{
$doc = $this->parse('<script>alert("hello <b>name</b>");</script>');

$this->assertSame('<script>alert("hello <b>name</b>");</script>', $this->compile($doc));
self::assertSame('<script>alert("hello <b>name</b>");</script>', $this->compile($doc));
}
}
2 changes: 1 addition & 1 deletion tests/Compiler/RawTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public function testCompileRaw(): void
{
$doc = $this->parse('hello world');

$this->assertSame('hello world', $this->compile($doc));
self::assertSame('hello world', $this->compile($doc));
}
}
40 changes: 8 additions & 32 deletions tests/Directive/ConditionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,79 +16,55 @@ public function testIfEndif(): void
{
$doc = $this->parse('@if(true) ok @endif');

$this->assertSame(
'<?php if(true): ?> ok <?php endif; ?>',
$this->compile($doc)
);
self::assertSame('<?php if(true): ?> ok <?php endif; ?>', $this->compile($doc));
}

public function testIfElseEndif(): void
{
$doc = $this->parse('@if(true) ok @else bad @endif');

$this->assertSame(
'<?php if(true): ?> ok <?php else: ?> bad <?php endif; ?>',
$this->compile($doc)
);
self::assertSame('<?php if(true): ?> ok <?php else: ?> bad <?php endif; ?>', $this->compile($doc));
}

public function testIfElseifElseEndif(): void
{
$doc = $this->parse('@if(true) ok @elseif(true) other @else bad @endif');

$this->assertSame(
'<?php if(true): ?> ok <?php elseif(true): ?> other <?php else: ?> bad <?php endif; ?>',
$this->compile($doc)
);
self::assertSame('<?php if(true): ?> ok <?php elseif(true): ?> other <?php else: ?> bad <?php endif; ?>', $this->compile($doc));
}

public function testUnless(): void
{
$doc = $this->parse('@unless(false) ok @endunless');

$this->assertSame(
'<?php if(!(false)): ?> ok <?php endif; ?>',
$this->compile($doc)
);
self::assertSame('<?php if(!(false)): ?> ok <?php endif; ?>', $this->compile($doc));
}

public function testIsset(): void
{
$doc = $this->parse('@isset($var) ok @endisset');

$this->assertSame(
'<?php if(isset($var)): ?> ok <?php endif; ?>',
$this->compile($doc)
);
self::assertSame('<?php if(isset($var)): ?> ok <?php endif; ?>', $this->compile($doc));
}

public function testEmpty(): void
{
$doc = $this->parse('@empty($var) ok @endempty');

$this->assertSame(
'<?php if(empty($var)): ?> ok <?php endif; ?>',
$this->compile($doc)
);
self::assertSame('<?php if(empty($var)): ?> ok <?php endif; ?>', $this->compile($doc));
}

public function testSwitchCase(): void
{
$doc = $this->parse('@switch(1) @case(1) 1 @endswitch');

$this->assertSame(
'<?php switch(1): case (1): ?> 1 <?php endswitch; ?>',
$this->compile($doc)
);
self::assertSame('<?php switch(1): case (1): ?> 1 <?php endswitch; ?>', $this->compile($doc));
}

public function testSwitchCaseDefaultBreak(): void
{
$doc = $this->parse('@switch($var) @case(1) 1 @break @default default @endswitch');

$this->assertSame(
'<?php switch($var): case (1): ?> 1 <?php break; ?> <?php default: ?> default <?php endswitch; ?>',
$this->compile($doc)
);
self::assertSame('<?php switch($var): case (1): ?> 1 <?php break; ?> <?php default: ?> default <?php endswitch; ?>', $this->compile($doc));
}
}
15 changes: 3 additions & 12 deletions tests/Directive/DirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,20 @@ public function testStringWithZeroChars(): void
{
$doc = $this->parse('@image("blog", "test.png", "150|250", "webp")');

$this->assertSame(
'<img title="blog" src="test.png" size="150|250" type="webp">',
$this->compile($doc),
);
self::assertSame('<img title="blog" src="test.png" size="150|250" type="webp">', $this->compile($doc));
}

public function testStringWithSingleQuotes(): void
{
$doc = $this->parse("@image('blog', 'test.png', '150|250', 'webp')");

$this->assertSame(
"<img title='blog' src='test.png' size='150|250' type='webp'>",
$this->compile($doc),
);
self::assertSame("<img title='blog' src='test.png' size='150|250' type='webp'>", $this->compile($doc));
}

public function testVariableInjection(): void
{
$doc = $this->parse('@image("blog", $src, "150|250", "webp")');

$this->assertSame(
'<img title="blog" src="<?php echo $src; ?>" size="150|250" type="webp">',
$this->compile($doc),
);
self::assertSame('<img title="blog" src="<?php echo $src; ?>" size="150|250" type="webp">', $this->compile($doc));
}
}
Loading

0 comments on commit dffd6d4

Please sign in to comment.