Something went wrong. Try again.
Parse and validate AT Protocol Lexicons with DTO generation for Laravel
Something went wrong. Try again.
7.9 kB · 244 lines
PHP
at dev
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245<?php
namespace SocialDept\AtpSchema\Tests\Unit\Generator;
use Orchestra\Testbench\TestCase;use SocialDept\AtpSchema\Generator\ConstructorGenerator;
class ConstructorGeneratorTest extends TestCase{ protected ConstructorGenerator $generator;
protected function setUp(): void { parent::setUp();
$this->generator = new ConstructorGenerator(); }
public function test_it_generates_constructor_with_promoted_properties(): void { $constructor = $this->generator->generate( [ 'name' => ['type' => 'string'], 'age' => ['type' => 'integer'], ], ['name', 'age'] );
$this->assertStringContainsString('public function __construct(', $constructor); $this->assertStringContainsString('public readonly string $name', $constructor); $this->assertStringContainsString('public readonly int $age', $constructor); }
public function test_it_handles_optional_parameters(): void { $constructor = $this->generator->generate( [ 'name' => ['type' => 'string'], 'nickname' => ['type' => 'string'], ], ['name'] );
$this->assertStringContainsString('public readonly string $name', $constructor); $this->assertStringContainsString('public readonly ?string $nickname', $constructor); }
public function test_it_returns_empty_for_no_properties(): void { $constructor = $this->generator->generate([], []);
$this->assertEmpty($constructor); }
public function test_it_generates_docblock(): void { $constructor = $this->generator->generate( [ 'name' => ['type' => 'string'], ], ['name'] );
$this->assertStringContainsString('/**', $constructor); $this->assertStringContainsString('* Create a new instance.', $constructor); $this->assertStringContainsString('* @param string $name', $constructor); }
public function test_it_includes_descriptions_in_docblock(): void { $constructor = $this->generator->generate( [ 'name' => [ 'type' => 'string', 'description' => 'The user name', ], ], ['name'] );
$this->assertStringContainsString('The user name', $constructor); }
public function test_it_handles_multiple_properties(): void { $constructor = $this->generator->generate( [ 'id' => ['type' => 'string'], 'name' => ['type' => 'string'], 'email' => ['type' => 'string'], 'age' => ['type' => 'integer'], 'active' => ['type' => 'boolean'], ], ['id', 'name', 'email'] );
$this->assertStringContainsString('$id', $constructor); $this->assertStringContainsString('$name', $constructor); $this->assertStringContainsString('$email', $constructor); $this->assertStringContainsString('$age', $constructor); $this->assertStringContainsString('$active', $constructor); }
public function test_it_generates_with_assignments(): void { $constructor = $this->generator->generateWithAssignments( [ 'name' => ['type' => 'string'], 'age' => ['type' => 'integer'], ], ['name', 'age'] );
$this->assertStringContainsString('public function __construct(', $constructor); $this->assertStringContainsString('string $name,', $constructor); $this->assertStringContainsString('int $age', $constructor); $this->assertStringContainsString('$this->name = $name;', $constructor); $this->assertStringContainsString('$this->age = $age;', $constructor); }
public function test_it_checks_if_constructor_should_be_generated(): void { $this->assertTrue($this->generator->shouldGenerate(['name' => ['type' => 'string']])); $this->assertFalse($this->generator->shouldGenerate([])); }
public function test_it_handles_default_values(): void { $constructor = $this->generator->generate( [ 'status' => [ 'type' => 'string', 'default' => 'active', ], ], [] );
$this->assertStringContainsString("= 'active'", $constructor); }
public function test_it_handles_nullable_with_null_default(): void { $constructor = $this->generator->generate( [ 'middleName' => [ 'type' => 'string', 'default' => null, ], ], [] );
$this->assertStringContainsString('?string $middleName = null', $constructor); }
public function test_it_handles_all_primitive_types(): void { $constructor = $this->generator->generate( [ 'name' => ['type' => 'string'], 'age' => ['type' => 'integer'], 'price' => ['type' => 'number'], 'active' => ['type' => 'boolean'], 'tags' => ['type' => 'array'], ], ['name', 'age', 'price', 'active', 'tags'] );
$this->assertStringContainsString('string $name', $constructor); $this->assertStringContainsString('int $age', $constructor); $this->assertStringContainsString('float $price', $constructor); $this->assertStringContainsString('bool $active', $constructor); $this->assertStringContainsString('array $tags', $constructor); }
public function test_it_does_not_add_trailing_comma_to_last_parameter(): void { $constructor = $this->generator->generate( [ 'first' => ['type' => 'string'], 'last' => ['type' => 'string'], ], ['first', 'last'] );
// Last parameter should not have a trailing comma $this->assertStringNotContainsString('$last,', $constructor); $this->assertStringContainsString('$last', $constructor); }
public function test_it_formats_parameters_with_proper_indentation(): void { $constructor = $this->generator->generate( [ 'name' => ['type' => 'string'], ], ['name'] );
// Check for proper indentation (8 spaces for parameters) $this->assertStringContainsString(' public readonly string $name', $constructor); }
public function test_it_generates_empty_body_for_promoted_properties(): void { $constructor = $this->generator->generate( [ 'name' => ['type' => 'string'], ], ['name'] );
// Constructor body should be empty for promoted properties $this->assertMatchesRegularExpression('/\) \{\s+\}/', $constructor); }
public function test_it_handles_blob_type(): void { $constructor = $this->generator->generate( [ 'image' => ['type' => 'blob'], ], [] );
$this->assertStringContainsString('BlobReference', $constructor); }
public function test_it_handles_ref_type(): void { $constructor = $this->generator->generate( [ 'author' => [ 'type' => 'ref', 'ref' => 'app.test.author', ], ], ['author'] );
$this->assertStringContainsString('public readonly Author $author', $constructor); }}