This class takes a class name and parses its methods and parameters. The result of parsing is an instance of ParsedClass.
Esempio n. 1
0
 public function testParseApi()
 {
     $instance = new Parser();
     $parsedApi = $instance->parseApi('Webiny\\Component\\Rest\\Tests\\Mocks\\MockApiClass', true);
     $this->assertCount(2, $parsedApi->versions);
     $this->assertSame('1.0', $parsedApi->currentVersion);
     $this->assertSame('1.1', $parsedApi->latestVersion);
 }
Esempio n. 2
0
 public function setUp()
 {
     Annotations::setConfig(__DIR__ . '/../Mocks/MockAnnotationsConfig.yaml');
     Rest::setConfig(__DIR__ . '/../Mocks/MockRestConfig.yaml');
     // we need to create the cache files so we can test the router
     $parser = new Parser();
     $parserApi = $parser->parseApi('Webiny\\Component\\Rest\\Tests\\Mocks\\MockApiClassRouter', true);
     self::$cache = new Cache(new ArrayDriver());
     $instance = new Compiler('ExampleApi', true, self::$cache);
     $instance->writeCacheFiles($parserApi);
 }
Esempio n. 3
0
 public function testWriteCacheFiles()
 {
     $parser = new Parser();
     $parserApi = $parser->parseApi('Webiny\\Component\\Rest\\Tests\\Mocks\\MockApiClass', true);
     $cache = new Cache(new ArrayDriver());
     $instance = new Compiler('ExampleApi', true, $cache);
     $instance->writeCacheFiles($parserApi);
     // now let's validate what was written
     $cRoot = Rest::getConfig()->ExampleApi->CompilePath;
     $cache = $cache->getCacheContent('ExampleApi', 'Webiny\\Component\\Rest\\Tests\\Mocks\\MockApiClass', 'current');
     $this->assertSame($cache['class'], 'Webiny\\Component\\Rest\\Tests\\Mocks\\MockApiClass');
     $this->assertSame($cache['version'], '1.0');
     $this->assertInternalType('array', $cache['post']);
     $this->assertCount(1, $cache['post']);
     $this->assertCount(1, $cache['get']);
     $this->assertNotFalse($cache['post']['some-method/([^/]+)/([^/]+)/([\\d]+)/']);
     $method = $cache['post']['some-method/([^/]+)/([^/]+)/([\\d]+)/'];
     $this->assertNotNull($method['default']);
     $this->assertSame('SECRET', $method['role']);
     $this->assertSame('someMethod', $method['method']);
     $this->assertSame('some-method/([^/]+)/([^/]+)/([\\d]+)/', $method['urlPattern']);
     $this->assertSame('3600', $method['cache']['ttl']);
     $this->assertSame(['expires' => '3600'], $method['header']['cache']);
     $this->assertSame('201', $method['header']['status']['success']);
     $this->assertSame('403', $method['header']['status']['error']);
     $this->assertSame('No Author for specified id.', $method['header']['status']['errorMessage']);
     $this->assertNotNull($method['rateControl']['ignore']);
     $this->assertCount(3, $method['params']);
     $param = $method['params']['param1'];
     $this->assertTrue($param['required']);
     $this->assertSame('string', $param['type']);
     $this->assertNull($param['default']);
     $param = $method['params']['param2'];
     $this->assertFalse($param['required']);
     $this->assertSame('string', $param['type']);
     $this->assertSame('default', $param['default']);
     $param = $method['params']['param3'];
     $this->assertFalse($param['required']);
     $this->assertSame('integer', $param['type']);
     $this->assertSame(22, $param['default']);
     $method = $cache['get']['simple-method/'];
     $this->assertFalse($method['default']);
     $this->assertSame(false, $method['role']);
     $this->assertSame('simpleMethod', $method['method']);
     $this->assertSame('simple-method/', $method['urlPattern']);
     $this->assertSame(0, $method['cache']['ttl']);
     $this->assertSame(['expires' => 0], $method['header']['cache']);
     $this->assertSame(200, $method['header']['status']['success']);
     $this->assertSame(404, $method['header']['status']['error']);
     $this->assertSame('', $method['header']['status']['errorMessage']);
     $this->assertFalse(isset($method['rateControl']['ignore']));
     $this->assertCount(0, $method['params']);
 }
Esempio n. 4
0
 /**
  * Calls the Parser to parse the class and
  * then Compiler to create a compiled cache file of the parsed class.
  */
 private function parseClass()
 {
     $parser = new Parser();
     $parsedApi = $parser->parseApi($this->class, $this->normalize);
     // in development we always write cache
     $writer = new Compiler($this->api, $this->normalize, $this->cacheInstance);
     $writer->writeCacheFiles($parsedApi);
 }