Source of file DataObjectAnnotatorTest.php
Size: 17,487 Bytes - Last Modified: 2021-12-23T10:24:49+00:00
/var/www/docs.ssmods.com/process/src/tests/unit/DataObjectAnnotatorTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 | <?php namespace SilverLeague\IDEAnnotator\Tests; use Page; use PageController; use PHPUnit_Framework_TestCase; use RootTeam; use SilverLeague\IDEAnnotator\DataObjectAnnotator; use SilverLeague\IDEAnnotator\Extensions\Annotatable; use SilverLeague\IDEAnnotator\Helpers\AnnotateClassInfo; use SilverLeague\IDEAnnotator\Helpers\AnnotatePermissionChecker; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Manifest\ModuleManifest; use SilverStripe\Dev\SapphireTest; use SilverStripe\ORM\DataObject; /** * Class DataObjectAnnotatorTest * * Several tests to make sure the Annotator does it's job correctly * * @mixin PHPUnit_Framework_TestCase */ class DataObjectAnnotatorTest extends SapphireTest { /** * @var MockDataObjectAnnotator */ private $annotator; /** * @var AnnotatePermissionChecker $permissionChecker */ private $permissionChecker; /** * Are we enabled? */ public function testIsEnabled() { $this->assertTrue(DataObjectAnnotator::isEnabled()); } /** * Test the expected classes show up in the Classes for Module */ public function testGetClassesForModule() { $expectedClasses = [ Team::class => realpath(__DIR__ . '/../mock/DataObjectAnnotatorTest_Team.php'), TeamChanged::class => realpath(__DIR__ . '/../mock/DataObjectAnnotatorTest_TeamChanged.php'), TeamComment::class => realpath(__DIR__ . '/../mock/DataObjectAnnotatorTest_TeamComment.php'), DocBlockMockWithDocBlock::class => realpath(__DIR__ . '/../mock/DocBlockMockWithDocBlock.php'), OtherDocBlockMockWithDocBlock::class => realpath(__DIR__ . '/../mock/DocBlockMockWithDocBlock.php'), DoubleDataObjectInOneFile1::class => realpath(__DIR__ . '/../mock/DoubleDataObjectInOneFile.php'), DoubleDataObjectInOneFile2::class => realpath(__DIR__ . '/../mock/DoubleDataObjectInOneFile.php'), SubTeam::class => realpath(__DIR__ . '/../mock/DataObjectAnnotatorTest_SubTeam.php'), Player::class => realpath(__DIR__ . '/../mock/DataObjectAnnotatorTest_Player.php'), Team_Extension::class => realpath(__DIR__ . '/../mock/DataObjectAnnotatorTest_Team_Extension.php'), Annotatable::class => realpath(__DIR__ . '/../../src/Extensions/Annotatable.php'), TestAnnotatorPage_Extension::class => realpath(__DIR__ . '/../mock/TestAnnotatorPage.php'), RootTeam::class => realpath(__DIR__ . '/../mock/RootTeam.php'), TestAnnotatorPage::class => realpath(__DIR__ . '/../mock/TestAnnotatorPage.php'), TestAnnotatorPageController::class => realpath(__DIR__ . '/../mock/TestAnnotatorPage.php'), TeamSupporter::class => realpath(__DIR__ . '/../mock/DataObjectAnnotatorTest_TeamSupporter.php'), ]; $classes = $this->annotator->getClassesForModule('silverleague/ideannotator'); // Sort the array, so we don't get accidental errors due to manual ordering ksort($expectedClasses); ksort($classes); $this->assertEquals($expectedClasses, $classes); } /** * As below, as we don't want to actively change the mocks, so enable mysite */ public function testAnnotateObject() { $this->assertFalse($this->annotator->annotateObject(DataObject::class)); Config::modify()->set(DataObjectAnnotator::class, 'enabled_modules', ['ideannotator', 'mysite', 'app']); $this->assertTrue($this->annotator->annotateObject(PageController::class)); } /** * Not testing existing modules, as it wil actively alter the mock files, so enable mysite */ public function testAnnotateModule() { $noModule = $this->annotator->annotateModule(''); $this->assertFalse($noModule); $projectName = ModuleManifest::config()->get('project'); $noModule = $this->annotator->annotateModule($projectName); $this->assertFalse($noModule); // Enable 'mysite' (or 'app') for testing Config::modify()->set(DataObjectAnnotator::class, 'enabled_modules', [$projectName]); $module = $this->annotator->annotateModule($projectName); $this->assertTrue($module); } /** * Test if the correct annotations are generated * for all database fields, relations and extensions * and that the start and end tags are present */ public function testFileContentWithAnnotations() { $classInfo = new AnnotateClassInfo(Team::class); $filePath = $classInfo->getClassFilePath(); $content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), Team::class); // ClassName title $this->assertContains(' * Class \SilverLeague\IDEAnnotator\Tests\Team', $content); // database fields $this->assertContains('@property string $Title', $content); $this->assertContains('@property int $VisitCount', $content); $this->assertContains('@property float $Price', $content); $this->assertContains('@property string $Dude', $content); $this->assertContains('@property string $Dudette', $content); // has_one ID $this->assertContains('@property int $CaptainID', $content); // has_one relation $this->assertContains('@method \SilverLeague\IDEAnnotator\Tests\Player Captain()', $content); // has_many relation $this->assertContains( '@method \SilverStripe\ORM\DataList|\SilverLeague\IDEAnnotator\Tests\SubTeam[] SubTeams()', $content ); // many_many relation $this->assertContains( '@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\Player[] Players()', $content ); $this->assertContains( '@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\Player[] Reserves()', $content ); $this->assertContains( '@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\TeamSupporter[] Supporters()', $content ); // DataExtension $this->assertContains('@mixin \SilverLeague\IDEAnnotator\Tests\Team_Extension', $content); } /** * Test if the correct annotations are generated * for all database fields, relations and extensions * and that the start and end tags are present */ public function testShortFileContentWithAnnotations() { Config::modify()->set(DataObjectAnnotator::class, 'use_short_name', true); $classInfo = new AnnotateClassInfo(Team::class); $filePath = $classInfo->getClassFilePath(); $content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), Team::class); // database fields $this->assertContains('@property string $Title', $content); $this->assertContains('@property int $VisitCount', $content); $this->assertContains('@property float $Price', $content); // has_one ID $this->assertContains('@property int $CaptainID', $content); // has_one relation $this->assertContains('@method Player Captain()', $content); // has_many relation $this->assertContains( '@method DataList|SubTeam[] SubTeams()', $content ); // many_many relation $this->assertContains( '@method ManyManyList|Player[] Players()', $content ); $this->assertContains( '@method ManyManyList|Player[] Reserves()', $content ); $this->assertContains( '@method ManyManyList|TeamSupporter[] Supporters()', $content ); // DataExtension $this->assertContains('@mixin Team_Extension', $content); } public function testInversePlayerRelationOfTeam() { $classInfo = new AnnotateClassInfo(Player::class); $filePath = $classInfo->getClassFilePath(); $content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), Player::class); $this->assertContains('@property boolean $IsRetired', $content); $this->assertContains('@property string $ShirtNumber', $content); $this->assertContains('@property string $Shirt', $content); $this->assertContains('@property int $FavouriteTeamID', $content); $this->assertContains('@method \SilverLeague\IDEAnnotator\Tests\Team CaptainTeam()', $content); $this->assertContains('@method \SilverLeague\IDEAnnotator\Tests\Team FavouriteTeam()', $content); $this->assertContains('@property string $OtherObjectClass', $content); $this->assertContains('@property int $OtherObjectID', $content); $this->assertContains('@method \SilverStripe\ORM\DataObject OtherObject()', $content); $this->assertContains( '@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\Team[] TeamPlayer()', $content ); $this->assertContains( '@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\Team[] TeamReserve()', $content ); } public function testDefaults() { $count = 0; DataObjectAnnotator::pushExtensionClass(Page::class); foreach (DataObjectAnnotator::getExtensionClasses() as $class) { if ($class === 'Page') { $count++; } } $this->assertEquals(1, $count); } public function testSetExtensionClasses() { $expected = [ 'SilverLeague\IDEAnnotator\Tests\TestAnnotatorPageController', 'SilverLeague\IDEAnnotator\Tests\Team', 'SilverStripe\Admin\LeftAndMain', 'SilverStripe\Admin\ModalController', 'SilverStripe\Assets\File', 'SilverStripe\AssetAdmin\Forms\FileFormFactory', 'SilverStripe\Assets\Shortcodes\FileShortcodeProvider', 'SilverStripe\CMS\Controllers\ContentController', 'SilverStripe\CMS\Controllers\ModelAsController', 'SilverStripe\CMS\Model\SiteTree', 'SilverStripe\Control\Controller', 'SilverStripe\Dev\DevBuildController', 'SilverStripe\Forms\Form', 'SilverStripe\ORM\DataObject', 'SilverStripe\Security\Group', 'SilverStripe\Security\Member', 'SilverStripe\Forms\GridField\GridFieldDetailForm', 'SilverStripe\Forms\GridField\GridFieldPrintButton', 'SilverStripe\ORM\FieldType\DBField', ]; // Instantiate - triggers extension class list generation new DataObjectAnnotator(); $result = DataObjectAnnotator::getExtensionClasses(); foreach ($expected as $expectedClass) { $this->assertContains($expectedClass, $result); } } public function testShortInversePlayerRelationOfTeam() { Config::modify()->set(DataObjectAnnotator::class, 'use_short_name', true); $classInfo = new AnnotateClassInfo(Player::class); $filePath = $classInfo->getClassFilePath(); $content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), Player::class); $this->assertContains('@property boolean $IsRetired', $content); $this->assertContains('@property string $ShirtNumber', $content); $this->assertContains('@property int $FavouriteTeamID', $content); $this->assertContains('@method Team FavouriteTeam()', $content); $this->assertContains('@property string $OtherObjectClass', $content); $this->assertContains('@property int $OtherObjectID', $content); $this->assertContains('@method DataObject OtherObject()', $content); $this->assertContains( '@method ManyManyList|Team[] TeamPlayer()', $content ); $this->assertContains( '@method ManyManyList|Team[] TeamReserve()', $content ); } public function testExistingMethodsWillNotBeTagged() { $classInfo = new AnnotateClassInfo(Team::class); $filePath = $classInfo->getClassFilePath(); $content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), Team::class); $this->assertNotContains( '@method \SilverStripe\ORM\ManyManyList|\SilverLeague\IDEAnnotator\Tests\SubTeam[] SecondarySubTeams()', $content ); } public function testShortExistingMethodsWillNotBeTagged() { Config::modify()->set(DataObjectAnnotator::class, 'use_short_name', true); $classInfo = new AnnotateClassInfo(Team::class); $filePath = $classInfo->getClassFilePath(); $content = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), Team::class); $this->assertNotContains( '@method ManyManyList|SubTeam[] SecondarySubTeams()', $content ); } /** * Test that multiple annotation runs won't generate ducplicate docblocks */ public function testNothingHasChangedAfterSecondAnnotation() { $classInfo = new AnnotateClassInfo(Team::class); $filePath = $classInfo->getClassFilePath(); $original = file_get_contents($filePath); $firstRun = $this->annotator->getGeneratedFileContent($original, Team::class); $secondRun = $this->annotator->getGeneratedFileContent($firstRun, Team::class); $this->assertEquals($firstRun, $secondRun); } /** * Test that root (non-namespaced) classes get annotated */ public function testRootAnnotations() { $classInfo = new AnnotateClassInfo(RootTeam::class); $filePath = $classInfo->getClassFilePath(); $run = $this->annotator->getGeneratedFileContent(file_get_contents($filePath), RootTeam::class); $this->assertContains('@property string $Title', $run); } /** * Test the generation of annotations for a DataExtension */ public function testAnnotateDataExtension() { $classInfo = new AnnotateClassInfo(Team_Extension::class); $filePath = $classInfo->getClassFilePath(); $original = file_get_contents($filePath); $annotated = $this->annotator->getGeneratedFileContent($original, Team_Extension::class); $this->assertContains( '@property \SilverLeague\IDEAnnotator\Tests\Team|\SilverLeague\IDEAnnotator\Tests\Team_Extension $owner', $annotated ); $this->assertContains('@property string $ExtendedVarcharField', $annotated); $this->assertContains('@property int $ExtendedIntField', $annotated); $this->assertContains('@property int $ExtendedHasOneRelationshipID', $annotated); $this->assertContains( '@method \SilverLeague\IDEAnnotator\Tests\Player ExtendedHasOneRelationship()', $annotated ); } /** * Test the generation of annotations for a DataExtension */ public function testShortAnnotateDataExtension() { Config::modify()->set(DataObjectAnnotator::class, 'use_short_name', true); $classInfo = new AnnotateClassInfo(Team_Extension::class); $filePath = $classInfo->getClassFilePath(); $original = file_get_contents($filePath); $annotated = $this->annotator->getGeneratedFileContent($original, Team_Extension::class); $this->assertContains( '@property Team|Team_Extension $owner', $annotated ); $this->assertContains('@property string $ExtendedVarcharField', $annotated); $this->assertContains('@property int $ExtendedIntField', $annotated); $this->assertContains('@property int $ExtendedHasOneRelationshipID', $annotated); $this->assertContains( '@method Player ExtendedHasOneRelationship()', $annotated ); } /** * */ public function testTwoClassesInOneFile() { $classInfo = new AnnotateClassInfo(DoubleDataObjectInOneFile1::class); $filePath = $classInfo->getClassFilePath(); $original = file_get_contents($filePath); $annotated = $this->annotator->getGeneratedFileContent($original, DoubleDataObjectInOneFile1::class); $this->assertContains('@property string $Title', $annotated); $annotated = $this->annotator->getGeneratedFileContent($annotated, DoubleDataObjectInOneFile2::class); $this->assertContains('@property string $Name', $annotated); } /** * Setup Defaults */ protected function setUp() { parent::setUp(); Config::modify()->set(DataObjectAnnotator::class, 'use_short_name', false); Config::modify()->set(DataObjectAnnotator::class, 'enabled', true); Config::modify()->set(DataObjectAnnotator::class, 'enabled_modules', ['silverleague/ideannotator']); $this->annotator = Injector::inst()->get(MockDataObjectAnnotator::class); $this->permissionChecker = Injector::inst()->get(AnnotatePermissionChecker::class); } } |