Skip to content

Commit d68d783

Browse files
committed
More tests on detecting traits
1 parent 709f674 commit d68d783

7 files changed

Lines changed: 50 additions & 4 deletions

File tree

src/model/PhpClass.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,21 @@ public static function fromReflection(\ReflectionClass $ref) {
3838
$class->setLongDescription($docblock->getLongDescription());
3939
}
4040

41+
// methods
4142
foreach ($ref->getMethods() as $method) {
4243
$class->setMethod(static::createMethod($method));
4344
}
4445

46+
// properties
4547
foreach ($ref->getProperties() as $property) {
4648
$class->setProperty(static::createProperty($property));
4749
}
4850

51+
// traits
52+
foreach ($ref->getTraits() as $trait) {
53+
$class->addTrait(PhpTrait::fromReflection($trait));
54+
}
55+
4956
return $class;
5057
}
5158

src/model/PhpTrait.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,21 @@ public static function fromReflection(\ReflectionClass $ref) {
2828
$trait->setLongDescription($docblock->getLongDescription());
2929
}
3030

31+
// methods
3132
foreach ($ref->getMethods() as $method) {
3233
$trait->setMethod(static::createMethod($method));
3334
}
3435

36+
// properties
3537
foreach ($ref->getProperties() as $property) {
3638
$trait->setProperty(static::createProperty($property));
3739
}
3840

41+
// traits
42+
foreach ($ref->getTraits() as $trait) {
43+
$trait->addTrait(PhpTrait::fromReflection($trait));
44+
}
45+
3946
return $trait;
4047
}
4148

src/model/parts/TraitsTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public function getTraits() {
5050
* @param PhpTrait|string $interface
5151
* @return boolean
5252
*/
53-
public function hasTrait($interface) {
54-
if (!$interface instanceof PhpTrait) {
55-
$interface = new PhpTrait($interface);
53+
public function hasTrait($trait) {
54+
if (!$trait instanceof PhpTrait) {
55+
$trait = new PhpTrait($trait);
5656
}
57-
$name = $interface->getName();
57+
$name = $trait->getName();
5858
return in_array($name, $this->traits);
5959
}
6060

tests/fixture/ClassWithTraits.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace gossi\codegen\tests\fixture;
3+
4+
class ClassWithTraits {
5+
6+
use DummyTrait;
7+
8+
}

tests/fixture/DummyTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace gossi\codegen\tests\fixture;
3+
4+
trait DummyTrait {
5+
}

tests/model/PhpClassTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function setUp() {
1717
// they are not explicitely instantiated through new WhatEver(); and such not
1818
// required through composer's autoload
1919
require_once __DIR__ . '/../fixture/Entity.php';
20+
require_once __DIR__ . '/../fixture/ClassWithTraits.php';
2021
}
2122

2223
public function testFromReflection() {
@@ -257,4 +258,10 @@ public function testLongDescription() {
257258
$this->assertSame($class, $class->setLongDescription('very long description'));
258259
$this->assertEquals('very long description', $class->getLongDescription());
259260
}
261+
262+
public function testClassWithTraitsFromReflection() {
263+
$class = PhpClass::fromReflection(new \ReflectionClass('gossi\codegen\tests\fixture\ClassWithTraits'));
264+
265+
$this->assertTrue($class->hasTrait('DummyTrait'));
266+
}
260267
}

tests/model/PhpTraitTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
class PhpTraitTest extends \PHPUnit_Framework_TestCase {
88

9+
public function setUp() {
10+
// they are not explicitely instantiated through new WhatEver(); and such not
11+
// required through composer's autoload
12+
require_once __DIR__ . '/../fixture/DummyTrait.php';
13+
}
14+
915
public function testSignature() {
1016
$expected = 'trait MyTrait {
1117
}';
@@ -18,4 +24,10 @@ public function testSignature() {
1824

1925
$this->assertEquals($expected, $code);
2026
}
27+
28+
public function fromReflection() {
29+
$trait = new PhpTrait('DummyTrait');
30+
$trait->setNamespace('gossi\codegen\tests\fixture');
31+
$this->assertEquals($trait, PhpTrait::fromReflection(new \ReflectionClass('gossi\codegen\tests\fixture\DummyTrait')));
32+
}
2133
}

0 commit comments

Comments
 (0)