Skip to content

Commit 6966124

Browse files
author
quintenmuijser
committed
More tests and ability to make a classProperty
1 parent b9941a5 commit 6966124

File tree

5 files changed

+130
-16
lines changed

5 files changed

+130
-16
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "quintenmbusiness/php-ast-code-generation-helper",
3+
"keywords": ["ast", "php-parser", "code generation", "ast helper", "php code generator", "code generation tool"],
34
"type": "library",
45
"license": "MIT",
56
"require": {

src/AstGenerationHelper.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/GeneratorHelpers/ClassGenerationHelper.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace quintenmbusiness\PhpAstCodeGenerationHelper\GeneratorHelpers;
66

77
use PhpParser\BuilderFactory;
8+
use PhpParser\Builder\Property;
89

910
class ClassGenerationHelper extends MethodGenerationHelper
1011
{
@@ -15,4 +16,23 @@ public function __construct(BuilderFactory $factory)
1516
{
1617
parent::__construct($factory);
1718
}
19+
20+
/**
21+
* @param string $name
22+
* @param null|string $type
23+
* @param string $visibility
24+
* @return Property
25+
*/
26+
public function createClassProperty(string $name, ?string $type = null, string $visibility = 'public'): Property
27+
{
28+
$property = $this->factory->property($name);
29+
30+
if ($type != null) {
31+
$property->setType($type);
32+
}
33+
34+
$this->addVisibility($property, $visibility);
35+
36+
return $property;
37+
}
1838
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BasicGeneration;
6+
7+
use PhpParser\Node\Expr;
8+
use PhpParser\BuilderFactory;
9+
use PHPUnit\Framework\TestCase;
10+
use PhpParser\Node\Expr\Variable;
11+
use PhpParser\Node\Stmt\Return_;
12+
use PHPUnit\Framework\Attributes\Test;
13+
use quintenmbusiness\PhpAstCodeGenerationHelper\GeneratorHelpers\BasicGenerationHelper;
14+
15+
class ReturnStmtTest extends TestCase
16+
{
17+
/**
18+
* @var BasicGenerationHelper
19+
*/
20+
private BasicGenerationHelper $helper;
21+
22+
protected function setUp(): void
23+
{
24+
$this->helper = new BasicGenerationHelper(new BuilderFactory());
25+
}
26+
27+
#[Test]
28+
public function it_creates_a_return_statement(): void
29+
{
30+
$variableExpr = new Variable('testVar');
31+
$returnStmt = $this->helper->return($variableExpr);
32+
33+
$this->assertInstanceOf(Return_::class, $returnStmt);
34+
$this->assertInstanceOf(Expr::class, $returnStmt->expr);
35+
$this->assertSame($variableExpr, $returnStmt->expr);
36+
}
37+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ClassGeneration;
6+
7+
use PhpParser\Builder\Property;
8+
use PhpParser\BuilderFactory;
9+
use PHPUnit\Framework\TestCase;
10+
use PHPUnit\Framework\Attributes\Test;
11+
use quintenmbusiness\PhpAstCodeGenerationHelper\GeneratorHelpers\ClassGenerationHelper;
12+
13+
class ClassPropertyGenerationTest extends TestCase
14+
{
15+
private ClassGenerationHelper $helper;
16+
17+
protected function setUp(): void
18+
{
19+
$this->helper = new ClassGenerationHelper(new BuilderFactory());
20+
}
21+
22+
#[Test]
23+
public function create_class_property_with_default_visibility(): void
24+
{
25+
$property = $this->helper->createClassProperty('testProperty');
26+
27+
$this->assertInstanceOf(Property::class, $property);
28+
$this->assertSame('testProperty', $property->getNode()->props[0]->name->toString());
29+
$this->assertTrue($property->getNode()->isPublic());
30+
$this->assertNull($property->getNode()->type);
31+
}
32+
33+
#[Test]
34+
public function create_class_property_with_protected_visibility(): void
35+
{
36+
$property = $this->helper->createClassProperty('testProperty', null, 'protected');
37+
38+
$this->assertInstanceOf(Property::class, $property);
39+
$this->assertTrue($property->getNode()->isProtected());
40+
$this->assertNull($property->getNode()->type);
41+
}
42+
43+
#[Test]
44+
public function create_class_property_with_private_visibility(): void
45+
{
46+
$property = $this->helper->createClassProperty('testProperty', null, 'private');
47+
48+
$this->assertInstanceOf(Property::class, $property);
49+
$this->assertTrue($property->getNode()->isPrivate());
50+
$this->assertNull($property->getNode()->type);
51+
}
52+
53+
#[Test]
54+
public function create_class_property_with_type(): void
55+
{
56+
$property = $this->helper->createClassProperty('testProperty', 'string');
57+
58+
$this->assertInstanceOf(Property::class, $property);
59+
$this->assertSame('string', $property->getNode()->type->toString());
60+
$this->assertTrue($property->getNode()->isPublic());
61+
}
62+
63+
#[Test]
64+
public function create_class_property_with_type_and_visibility(): void
65+
{
66+
$property = $this->helper->createClassProperty('testProperty', 'int', 'protected');
67+
68+
$this->assertInstanceOf(Property::class, $property);
69+
$this->assertSame('int', $property->getNode()->type->toString());
70+
$this->assertTrue($property->getNode()->isProtected());
71+
}
72+
}

0 commit comments

Comments
 (0)