Skip to content

Commit b23f382

Browse files
committed
add tests for class level attributes
1 parent a522c11 commit b23f382

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

src/Util/ClassSourceManipulator.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -923,12 +923,11 @@ public function addUseStatementIfNecessary(string $class): string
923923
/**
924924
* Builds a PHPParser attribute node.
925925
*
926-
* @param string $attributeClass The attribute class which should be used for the attribute. If the class has a substitute
927-
* prefix, the node is built using the prefix. E.g. #[ORM\Column()] Otherwise, the
928-
* short name is used. E.g. #[Column()]
929-
* @param array $options the named arguments for the attribute ($key = argument name, $value = argument value)
926+
* @param string $attributeClass The attribute class which should be used for the attribute E.g. #[Column()]
927+
* @param array $options The named arguments for the attribute ($key = argument name, $value = argument value)
928+
* @param ?string $attributePrefix If a prefix is provided, the node is built using the prefix. E.g. #[ORM\Column()]
930929
*/
931-
public function buildAttributeNode(string $attributeClass, array $options, string $attributePrefix = null): Node\Attribute
930+
public function buildAttributeNode(string $attributeClass, array $options, ?string $attributePrefix = null): Node\Attribute
932931
{
933932
$options = $this->sortOptionsByClassConstructorParameters($options, $attributeClass);
934933

tests/Util/ClassSourceManipulatorTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bundle\MakerBundle\Tests\Util;
1313

14+
use Doctrine\ORM\Mapping\Column;
15+
use Doctrine\ORM\Mapping\Entity;
1416
use PhpParser\Builder\Param;
1517
use PHPUnit\Framework\TestCase;
1618
use Symfony\Bundle\MakerBundle\Doctrine\RelationManyToMany;
@@ -167,6 +169,36 @@ public function getAddSetterTests()
167169
];
168170
}
169171

172+
/**
173+
* @dataProvider getAttributeClassTests
174+
*/
175+
public function testAddAttributeToClass(string $sourceFilename, string $expectedSourceFilename, string $attributeClass, array $attributeOptions, string $attributePrefix = null): void
176+
{
177+
$source = file_get_contents(__DIR__.'/fixtures/source/'.$sourceFilename);
178+
$expectedSource = file_get_contents(__DIR__.'/fixtures/add_class_attribute/'.$expectedSourceFilename);
179+
$manipulator = new ClassSourceManipulator($source);
180+
$manipulator->addAttributeToClass($attributeClass, $attributeOptions, $attributePrefix);
181+
182+
self::assertSame($expectedSource, $manipulator->getSourceCode());
183+
}
184+
185+
public function getAttributeClassTests(): \Generator
186+
{
187+
yield 'Empty class' => [
188+
'User_empty.php',
189+
'User_empty.php',
190+
Entity::class,
191+
[],
192+
];
193+
194+
yield 'Class already has attributes' => [
195+
'User_simple.php',
196+
'User_simple.php',
197+
Column::class,
198+
['message' => 'We use this attribute for class level tests so we dont have to add additional test dependencies.'],
199+
];
200+
}
201+
170202
/**
171203
* @dataProvider getAnnotationTests
172204
*/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use Doctrine\ORM\Mapping\Entity;
6+
7+
#[Entity]
8+
class User
9+
{
10+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Doctrine\ORM\Mapping\Column;
7+
8+
#[ORM\Entity]
9+
#[Column(message: 'We use this attribute for class level tests so we dont have to add additional test dependencies.')]
10+
class User
11+
{
12+
#[ORM\Id]
13+
#[ORM\GeneratedValue]
14+
#[ORM\Column(type: 'integer')]
15+
private $id;
16+
17+
public function getId(): ?int
18+
{
19+
return $this->id;
20+
}
21+
}

0 commit comments

Comments
 (0)