Skip to content

Commit

Permalink
Fixes #51
Browse files Browse the repository at this point in the history
  • Loading branch information
gossi committed Aug 4, 2018
1 parent 37c126e commit 5a561df
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 47 deletions.
6 changes: 3 additions & 3 deletions src/model/parts/InterfacesPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait InterfacesPart {

/** @var Set */
private $interfaces;

private function initInterfaces() {
$this->interfaces = new Set();
}
Expand Down Expand Up @@ -97,11 +97,11 @@ public function hasInterfaces() {
*/
public function hasInterface($interface) {
if ($interface instanceof PhpInterface) {
return $this->interfaces->contains($interface->getName())
return $this->interfaces->contains($interface->getName())
|| $this->interfaces->contains($interface->getQualifiedName());
}

return $this->hasInterface(new PhpInterface($interface));
return $this->interfaces->contains($interface) || $this->hasInterface(new PhpInterface($interface));
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/parser/visitor/ClassParserVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use PhpParser\Node\Stmt\Class_;

class ClassParserVisitor extends StructParserVisitor {

use StructParserPart;

public function visitClass(Class_ $node) {
$struct = $this->getStruct();

if ($node->extends !== null) {
if ($node->extends->getType() === 'Name_FullyQualified') {
$struct->setParentClassName('\\' . implode('\\', $node->extends->parts));
Expand All @@ -19,8 +19,12 @@ public function visitClass(Class_ $node) {
}
}

foreach ($node->implements as $name) {
$struct->addInterface(implode('\\', $name->parts));
foreach ($node->implements as $interface) {
if ($interface->getType() === 'Name_FullyQualified') {
$struct->addInterface('\\' . implode('\\', $interface->parts));
} else {
$struct->addInterface(implode('\\', $interface->parts));
}
}

$struct->setAbstract($node->isAbstract());
Expand Down
3 changes: 1 addition & 2 deletions tests/fixtures/MyCollection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?php
class MyCollection extends phootwork\collection\AbstractCollection implements phootwork\collection\Collection {

}
}
1 change: 0 additions & 1 deletion tests/fixtures/MyCollection2.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?php
class MyCollection2 extends \phootwork\collection\AbstractCollection implements \phootwork\collection\Collection {

}
66 changes: 42 additions & 24 deletions tests/generator/ClassGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @group generator
*/
class ClassGeneratorTest extends \PHPUnit_Framework_TestCase {

use TestUtils;

public function testSignature() {
Expand All @@ -24,103 +24,121 @@ public function testSignature() {

$this->assertEquals($expected, $code);
}

public function testAbstract() {
$expected = 'abstract class MyClass {' . "\n" . '}';

$class = PhpClass::create('MyClass')->setAbstract(true);
$generator = new ModelGenerator();
$code = $generator->generate($class);

$this->assertEquals($expected, $code);
}

public function testFinal() {
$expected = 'final class MyClass {' . "\n" . '}';

$class = PhpClass::create('MyClass')->setFinal(true);
$generator = new ModelGenerator();
$code = $generator->generate($class);

$this->assertEquals($expected, $code);
}

public function testInterfaces() {
$generator = new ModelGenerator();

$expected = 'class MyClass implements \Iterator {' . "\n" . '}';
$class = PhpClass::create('MyClass')->addInterface('\Iterator');
$this->assertEquals($expected, $generator->generate($class));

$expected = 'class MyClass implements \Iterator, \ArrayAccess {' . "\n" . '}';
$class = PhpClass::create('MyClass')->addInterface('\Iterator')->addInterface('\ArrayAccess');
$this->assertEquals($expected, $generator->generate($class));
}

public function testParent() {
$expected = 'class MyClass extends MyParent {' . "\n" . '}';

$class = PhpClass::create('MyClass')->setParentClassName('MyParent');
$generator = new ModelGenerator();
$code = $generator->generate($class);

$this->assertEquals($expected, $code);
}

public function testUseStatements() {
$class = new PhpClass('Foo\\Bar');
$class->addUseStatement('Bam\\Baz');

$codegen = new CodeFileGenerator(['generateDocblock' => false, 'generateEmptyDocblock' => false]);
$code = $codegen->generate($class);

$this->assertEquals($this->getGeneratedContent('FooBar.php'), $code);

$class = new PhpClass('Foo\\Bar');
$class->addUseStatement('Bam\\Baz', 'BamBaz');

$codegen = new CodeFileGenerator(['generateDocblock' => false, 'generateEmptyDocblock' => false]);
$code = $codegen->generate($class);

$this->assertEquals($this->getGeneratedContent('FooBarWithAlias.php'), $code);

$class = new PhpClass('Foo');
$class->addUseStatement('Bar');

$generator = new ModelGenerator();
$code = $generator->generate($class);
$expected = 'class Foo {' . "\n" . '}';

$this->assertEquals($expected, $code);
}

public function testABClass() {
$class = Fixtures::createABClass();

$modelGenerator = new ModelGenerator();
$modelCode = $modelGenerator->generate($class);
$this->assertEquals($this->getGeneratedContent('ABClass.php'), $modelCode);
$generator = new CodeGenerator(['generateDocblock' => false]);
$code = $generator->generate($class);
$this->assertEquals($modelCode, $code);

$modelGenerator = new ModelGenerator(['generateDocblock' => true]);
$modelCode = $modelGenerator->generate($class);
$this->assertEquals($this->getGeneratedContent('ABClassWithComments.php'), $modelCode);
$generator = new CodeGenerator(['generateDocblock' => true]);
$code = $generator->generate($class);
$this->assertEquals($modelCode, $code);
}

public function testRequireTraitsClass() {
$class = PhpClass::create('RequireTraitsClass')
->addRequiredFile('FooBar.php')
->addRequiredFile('ABClass.php')
->addTrait('Iterator');

$generator = new ModelGenerator();
$code = $generator->generate($class);
$this->assertEquals($this->getGeneratedContent('RequireTraitsClass.php'), $code);
}

public function testMyCollection() {
$class = PhpClass::fromFile(__DIR__ . '/../fixtures/MyCollection.php');

$generator = new CodeFileGenerator(['generateDocblock' => false]);
$code = $generator->generate($class);

$this->assertEquals($this->getFixtureContent('MyCollection.php'), $code);
}

public function testMyCollection2() {
$class = PhpClass::fromFile(__DIR__ . '/../fixtures/MyCollection2.php');

$generator = new CodeFileGenerator(['generateDocblock' => false]);
$code = $generator->generate($class);

$this->assertEquals($this->getFixtureContent('MyCollection2.php'), $code);
}

}
4 changes: 4 additions & 0 deletions tests/model/ClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ public function testInterfaces() {
$this->assertTrue($class->hasUseStatement('other\name\space\Interface'));
$this->assertSame($class, $class->removeInterface('other\name\space\Interface'));
$this->assertTrue($class->hasUseStatement('other\name\space\Interface'));

$class->addInterface('\my\Interface');
$this->assertTrue($class->hasInterface('\my\Interface'));
$this->assertFalse($class->hasInterface('my\Interface'));
}

public function testTraits() {
Expand Down
26 changes: 13 additions & 13 deletions tests/parser/ClassParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @group parser
*/
class ClassParserTest extends \PHPUnit_Framework_TestCase {

use ModelAssertions;
use ValueTests;

Expand Down Expand Up @@ -47,7 +47,7 @@ public function testClassWithConstants() {

$this->assertTrue($class->hasConstant('NMBR'));
$this->assertEquals(300, $class->getConstant('NMBR')->getValue());

$this->assertTrue($class->hasConstant('BAR'));
$this->assertEquals('self::FOO', $class->getConstant('BAR')->getExpression());
}
Expand All @@ -67,10 +67,18 @@ public function testClassWithValues() {
$class = PhpClass::fromFile(__DIR__ . '/../fixtures/ClassWithValues.php');
$this->assertClassWithValues($class);
}


public function testTypeClass() {
$class = PhpClass::fromFile(__DIR__ . '/../fixtures/TypeClass.php');

$doSomething = $class->getMethod('doSomething');
$options = $doSomething->getParameter('options');
$this->assertEquals('Symfony\Component\OptionsResolver\OptionsResolver', $options->getType());
}

public function testMyCollection() {
$class = PhpClass::fromFile(__DIR__ . '/../fixtures/MyCollection.php');

$this->assertEquals('phootwork\collection\AbstractCollection', $class->getParentClassName());
$this->assertTrue($class->hasInterface('phootwork\collection\Collection'));
}
Expand All @@ -81,13 +89,5 @@ public function testMyCollection2() {
$this->assertEquals('\phootwork\collection\AbstractCollection', $class->getParentClassName());
$this->assertTrue($class->hasInterface('\phootwork\collection\Collection'));
}

public function testTypeClass() {
$class = PhpClass::fromFile(__DIR__ . '/../fixtures/TypeClass.php');

$doSomething = $class->getMethod('doSomething');
$options = $doSomething->getParameter('options');
$this->assertEquals('Symfony\Component\OptionsResolver\OptionsResolver', $options->getType());
}


}

0 comments on commit 5a561df

Please sign in to comment.