public function testGetParentClasses() { $analyser = new StaticAnalyser(); $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/Child.php'); $analysis->addAnalysis($analyser->fromFile(__DIR__ . '/Fixtures/GrandParent.php')); $analysis->addAnalysis($analyser->fromFile(__DIR__ . '/Fixtures/Parent.php')); $this->assertCount(3, $analysis->classes, '3 classes should\'ve been detected'); $superclasses = $analysis->getSuperClasses('\\AnotherNamespace\\Child'); $this->assertCount(2, $superclasses, 'Child has a chain of 2 super classes'); $this->assertSame(['\\SwaggerFixtures\\Parent', '\\SwaggerFixtures\\GrandParent'], array_keys($superclasses)); $this->assertSame(['\\SwaggerFixtures\\GrandParent'], array_keys($analysis->getSuperClasses('\\SwaggerFixtures\\Parent'))); }
public function testAugmentProperties() { $analyser = new StaticAnalyser(); $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/Customer.php'); $analysis->process(new MergeIntoSwagger()); $this->assertCount(1, $analysis->swagger->definitions); $customer = $analysis->swagger->definitions[0]; $this->assertSame(null, $customer->properties, 'Sanity check. @SWG\\Property\'s not yet erged '); $analysis->process(new AugmentDefinitions()); $analysis->process(new AugmentProperties()); $firstname = $customer->properties[0]; $this->assertSame('firstname', $firstname->property, '@SWG\\Property()->property based on propertyname'); $this->assertSame('The firstname of the customer.', $firstname->description, '@SWG\\Property()->description based on @var description'); $this->assertSame('string', $firstname->type, '@SWG\\Property()->type based on @var declaration'); $lastname = $customer->properties[1]; $this->assertSame('lastname', $lastname->property); $this->assertSame('The lastname of the customer.', $lastname->description); $this->assertSame('string', $lastname->type); $tags = $customer->properties[2]; $this->assertSame('tags', $tags->property); $this->assertSame('array', $tags->type, 'Detect array notation: @var string[]'); $this->assertSame('string', $tags->items->type); $submittedBy = $customer->properties[3]; $this->assertSame('submittedBy', $submittedBy->property); $this->assertSame('#/definitions/Customer', $submittedBy->ref); $friends = $customer->properties[4]; $this->assertSame('friends', $friends->property); $this->assertSame('array', $friends->type); $this->assertSame('#/definitions/Customer', $friends->items->ref); }
public function testNestedProperties() { $analyser = new StaticAnalyser(); $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/NestedProperty.php'); $analysis->process(new MergeIntoSwagger()); $analysis->process(new AugmentDefinitions()); $analysis->process(new AugmentProperties()); $this->assertCount(1, $analysis->swagger->definitions); $definition = $analysis->swagger->definitions[0]; $this->assertEquals('NestedProperty', $definition->definition); $this->assertCount(1, $definition->properties); $parentProperty = $definition->properties[0]; $this->assertEquals('parentProperty', $parentProperty->property); $this->assertCount(1, $parentProperty->properties); $babyProperty = $parentProperty->properties[0]; $this->assertEquals('babyProperty', $babyProperty->property); $this->assertCount(1, $babyProperty->properties); $theBabyOfBaby = $babyProperty->properties[0]; $this->assertEquals('theBabyOfBaby', $theBabyOfBaby->property); $this->assertCount(1, $theBabyOfBaby->properties); // verbose not-recommend notations $theBabyOfBabyBaby = $theBabyOfBaby->properties[0]; $this->assertEquals('theBabyOfBabyBaby', $theBabyOfBabyBaby->property); $this->assertNull($theBabyOfBabyBaby->properties); }
public function testInheritProperties() { $analyser = new StaticAnalyser(); $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/Child.php'); $analysis->addAnalysis($analyser->fromFile(__DIR__ . '/Fixtures/GrandParent.php')); $analysis->addAnalysis($analyser->fromFile(__DIR__ . '/Fixtures/Parent.php')); $analysis->process([new MergeIntoSwagger(), new AugmentDefinitions(), new AugmentProperties()]); $definitions = $analysis->getAnnotationsOfType('\\Swagger\\Annotations\\Definition'); $childDefinition = $definitions[0]; $this->assertSame('Child', $childDefinition->definition); $this->assertCount(1, $childDefinition->properties); $analysis->process(new InheritProperties()); $this->assertCount(3, $childDefinition->properties); $analysis->swagger->info = new Info(['title' => 'test', 'version' => 1]); $analysis->validate(); }
public function testTrait() { $analyser = new StaticAnalyser(); $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/HelloTrait.php'); $this->assertCount(2, $analysis->annotations); $property = $analysis->getAnnotationsOfType('Swagger\\Annotations\\Property')[0]; $this->assertSame('Hello', $property->_context->trait); }
/** * Tests, if InheritProperties works even without any * docBlocks at all in the parent class. */ public function testInheritPropertiesWithoutDocBlocks() { $analyser = new StaticAnalyser(); // this class has docblocks $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/ChildWithDocBlocks.php'); // this one doesn't $analysis->addAnalysis($analyser->fromFile(__DIR__ . '/Fixtures/ParentWithoutDocBlocks.php')); $analysis->process([new MergeIntoSwagger(), new AugmentDefinitions(), new AugmentProperties()]); $definitions = $analysis->getAnnotationsOfType('\\Swagger\\Annotations\\Definition'); $childDefinition = $definitions[0]; $this->assertSame('ChildWithDocBlocks', $childDefinition->definition); $this->assertCount(1, $childDefinition->properties); // no error occurs $analysis->process(new InheritProperties()); $this->assertCount(1, $childDefinition->properties); $analysis->swagger->info = new Info(['title' => 'test', 'version' => 1]); $analysis->validate(); }
public function testAugmentDefinitions() { $analyser = new StaticAnalyser(); $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/Customer.php'); $analysis->process(new MergeIntoSwagger()); $this->assertCount(1, $analysis->swagger->definitions); $customer = $analysis->swagger->definitions[0]; $this->assertNull($customer->properties, 'Sanity check. @SWG\\Property\'s not yet merged '); $analysis->process(new AugmentDefinitions()); $this->assertSame('Customer', $customer->definition, '@SWG\\Definition()->definition based on classname'); $this->assertCount(5, $customer->properties, '@SWG\\Property()s are merged into the @SWG\\Definition of the class'); }
public function testAugmentOperation() { $analyser = new StaticAnalyser(); $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/UsingPhpDoc.php'); $analysis->process([new AugmentOperations()]); $operations = $analysis->getAnnotationsOfType('\\Swagger\\Annotations\\Operation'); $this->assertSame('api/test1', $operations[0]->path); $this->assertSame('Example summary', $operations[0]->summary, 'Operation summary should be taken from phpDoc'); $this->assertSame("Example description...\nMore description...", $operations[0]->description, 'Operation description should be taken from phpDoc'); $this->assertSame('api/test2', $operations[1]->path); $this->assertSame('Example summary', $operations[1]->summary, 'Operation summary should be taken from phpDoc'); $this->assertNull($operations[1]->description, 'This operation only has summary in the phpDoc, no description'); }
/** * {@inheritDoc} */ public function getServiceConfig() { return array('aliases' => array('service.swagger' => 'Swagger\\Annotations\\Swagger'), 'factories' => array('SwaggerModule\\Options\\ModuleOptions' => function ($serviceManager) { $config = $serviceManager->get('Config'); $config = isset($config['swagger']) ? $config['swagger'] : null; if ($config === null) { throw new RuntimeException('Configuration for SwaggerModule was not found'); } return new SwaggerModuleOptions($config); }, 'Swagger\\Annotations\\Swagger' => function ($serviceManager) { /** @var $options \SwaggerModule\Options\ModuleOptions */ $options = $serviceManager->get('SwaggerModule\\Options\\ModuleOptions'); $analyser = new SwaggerStaticAnalyser(); $analysis = new SwaggerAnalysis(); $processors = SwaggerAnalysis::processors(); // Crawl directory and parse all files $paths = $options->getPaths(); foreach ($paths as $directory) { $finder = SwaggerUtil::finder($directory); foreach ($finder as $file) { $analysis->addAnalysis($analyser->fromFile($file->getPathname())); } } // Post processing $analysis->process($processors); // Validation (Generate notices & warnings) $analysis->validate(); // Pass options to analyzer $resourceOptions = $options->getResourceOptions(); if (!empty($resourceOptions['defaultBasePath'])) { $analysis->swagger->basePath = $resourceOptions['defaultBasePath']; } if (!empty($resourceOptions['defaultHost'])) { $analysis->swagger->host = $resourceOptions['defaultHost']; } if (!empty($resourceOptions['schemes'])) { $analysis->swagger->schemes = $resourceOptions['schemes']; } return $analysis->swagger; })); }
public function testThirdPartyAnnotations() { $backup = Analyser::$whitelist; Analyser::$whitelist = ['Swagger\\Annotations\\']; $analyser = new StaticAnalyser(); $defaultAnalysis = $analyser->fromFile(__DIR__ . '/Fixtures/ThirdPartyAnnotations.php'); $this->assertCount(3, $defaultAnalysis->annotations, 'Only read the @SWG annotations, skip the others.'); // Allow Swagger to parse 3rd party annotations // might contain useful info that could be extracted with a custom processor Analyser::$whitelist[] = 'Zend\\Form\\Annotation'; $swagger = \Swagger\scan(__DIR__ . '/Fixtures/ThirdPartyAnnotations.php'); $this->assertSame('api/3rd-party', $swagger->paths[0]->path); $this->assertCount(10, $swagger->_unmerged); Analyser::$whitelist = $backup; $analysis = $swagger->_analysis; $annotations = $analysis->getAnnotationsOfType('Zend\\Form\\Annotation\\Name'); $this->assertCount(1, $annotations); $context = $analysis->getContext($annotations[0]); $this->assertInstanceOf('Swagger\\Context', $context); $this->assertSame('ThirdPartyAnnotations', $context->class); $this->assertSame('\\SwaggerFixtures\\ThirdPartyAnnotations', $context->fullyQualifiedName($context->class)); $this->assertCount(2, $context->annotations); }
public function testIndentationCorrection() { $analyser = new StaticAnalyser(); $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/routes.php'); $this->assertCount(12, $analysis->annotations); }