silverstripe-framework/tests/php/Core/Manifest/TokenisedRegularExpressionTest.php

179 lines
4.2 KiB
PHP
Raw Normal View History

<?php
2016-09-09 08:43:05 +02:00
use SilverStripe\Core\Manifest\ClassManifest;
use SilverStripe\Dev\SapphireTest;
class TokenisedRegularExpressionTest extends SapphireTest {
public function getTokens() {
return token_get_all(<<<PHP
<?php
class ClassA {
2014-08-15 08:53:05 +02:00
}
class ClassB{
2014-08-15 08:53:05 +02:00
}
class ClassC extends ParentClassC {
2014-08-15 08:53:05 +02:00
}
class ClassD extends ParentClassD
implements InterfaceA {
2014-08-15 08:53:05 +02:00
}
interface InterfaceA {
2014-08-15 08:53:05 +02:00
}
interface InterfaceB extends Something{
2014-08-15 08:53:05 +02:00
}
2014-08-15 08:53:05 +02:00
class ClassE extends ParentClassE
implements InterfaceA,InterfaceB {
2014-08-15 08:53:05 +02:00
}
2014-08-15 08:53:05 +02:00
class ClassF extends ParentClassF
implements InterfaceA, InterfaceB {
2014-08-15 08:53:05 +02:00
}
interface InterfaceC extends InterfaceA, InterfaceB {
}
interface InterfaceD extends InterfaceA, InterfaceB, InterfaceC {
}
PHP
);
}
2014-08-15 08:53:05 +02:00
public function getNamespaceTokens() {
return token_get_all(<<<PHP
<?php
namespace silverstripe\\test;
class ClassA {
2014-08-15 08:53:05 +02:00
}
class ClassB extends ParentClassB {
2014-08-15 08:53:05 +02:00
}
class ClassC extends \\ParentClassC {
2014-08-15 08:53:05 +02:00
}
class ClassD extends subtest\\ParentClassD {
2014-08-15 08:53:05 +02:00
}
class ClassE implements InterfaceE {
}
class ClassF implements \\InterfaceF {
}
class ClassG implements subtest\\InterfaceG {
}
PHP
);
}
2014-08-15 08:53:05 +02:00
public function testClassDefParser() {
2016-09-09 08:43:05 +02:00
$parser = ClassManifest::get_class_parser();
2014-08-15 08:53:05 +02:00
$tokens = $this->getTokens();
2014-08-15 08:53:05 +02:00
$matches = $parser->findAll($tokens);
$classes = array();
if($matches) foreach($matches as $match) $classes[$match['className']] = $match;
2014-08-15 08:53:05 +02:00
$this->assertArrayHasKey('ClassA', $classes);
$this->assertArrayHasKey('ClassB', $classes);
2014-08-15 08:53:05 +02:00
$this->assertArrayHasKey('ClassC', $classes);
$this->assertEquals(array('ParentClassC'), $classes['ClassC']['extends']);
2014-08-15 08:53:05 +02:00
$this->assertArrayHasKey('ClassD', $classes);
$this->assertEquals(array('ParentClassD'), $classes['ClassD']['extends']);
$this->assertContains('InterfaceA', $classes['ClassD']['interfaces']);
2014-08-15 08:53:05 +02:00
$this->assertArrayHasKey('ClassE', $classes);
$this->assertEquals(array('ParentClassE'), $classes['ClassE']['extends']);
$this->assertContains('InterfaceA', $classes['ClassE']['interfaces']);
$this->assertContains('InterfaceB', $classes['ClassE']['interfaces']);
2014-08-15 08:53:05 +02:00
$this->assertArrayHasKey('ClassF', $classes);
$this->assertEquals(array('ParentClassF'), $classes['ClassF']['extends']);
$this->assertContains('InterfaceA', $classes['ClassF']['interfaces']);
$this->assertContains('InterfaceB', $classes['ClassF']['interfaces']);
}
2014-08-15 08:53:05 +02:00
public function testNamesapcedClassDefParser() {
2016-09-09 08:43:05 +02:00
$parser = ClassManifest::get_namespaced_class_parser();
2014-08-15 08:53:05 +02:00
$tokens = $this->getNamespaceTokens();
2014-08-15 08:53:05 +02:00
$matches = $parser->findAll($tokens);
2014-08-15 08:53:05 +02:00
$classes = array();
if($matches) foreach($matches as $match) $classes[$match['className']] = $match;
2014-08-15 08:53:05 +02:00
$this->assertArrayHasKey('ClassA', $classes);
$this->assertArrayHasKey('ClassB', $classes);
$this->assertEquals(array('ParentClassB'), $classes['ClassB']['extends']);
2014-08-15 08:53:05 +02:00
$this->assertArrayHasKey('ClassC', $classes);
$this->assertEquals(array('\\', 'ParentClassC'), $classes['ClassC']['extends']);
2014-08-15 08:53:05 +02:00
$this->assertArrayHasKey('ClassD', $classes);
$this->assertEquals(array('subtest', '\\', 'ParentClassD'), $classes['ClassD']['extends']);
2014-08-15 08:53:05 +02:00
$this->assertArrayHasKey('ClassE', $classes);
$this->assertContains('InterfaceE', $classes['ClassE']['interfaces']);
2014-08-15 08:53:05 +02:00
$this->assertArrayHasKey('ClassF', $classes);
$this->assertEquals(array('\\', 'InterfaceF'), $classes['ClassF']['interfaces']);
}
2014-08-15 08:53:05 +02:00
public function testInterfaceDefParser() {
2016-09-09 08:43:05 +02:00
$parser = ClassManifest::get_interface_parser();
2014-08-15 08:53:05 +02:00
$tokens = $this->getTokens();
2014-08-15 08:53:05 +02:00
$matches = $parser->findAll($tokens);
$interfaces = array();
if($matches) foreach($matches as $match) $interfaces[$match['interfaceName']] = $match;
2014-08-15 08:53:05 +02:00
$this->assertArrayHasKey('InterfaceA', $interfaces);
$this->assertArrayHasKey('InterfaceB', $interfaces);
$this->assertArrayHasKey('InterfaceC', $interfaces);
$this->assertArrayHasKey('InterfaceD', $interfaces);
}
2014-08-15 08:53:05 +02:00
public function testNamespaceDefParser() {
2016-09-09 08:43:05 +02:00
$parser = ClassManifest::get_namespace_parser();
2014-08-15 08:53:05 +02:00
$namespacedTokens = $this->getNamespaceTokens();
$tokens = $this->getTokens();
2014-08-15 08:53:05 +02:00
$namespacedMatches = $parser->findAll($namespacedTokens);
$matches = $parser->findAll($tokens);
2014-08-15 08:53:05 +02:00
$this->assertEquals(array(), $matches);
$this->assertEquals(array('silverstripe', '\\', 'test'), $namespacedMatches[0]['namespaceName']);
}
}