Example #1
0
 public function testParseConfig()
 {
     $pathAnnotation = $this->getMockBuilder('Sonno\\Annotation\\Path')->disableOriginalConstructor()->getMock();
     $pathAnnotation->expects($this->exactly(2))->method('getPath')->will($this->onConsecutiveCalls('/messages', '/something'));
     $consumesAnnotation = $this->getMockBuilder('Sonno\\Annotation\\Consumes')->disableOriginalConstructor()->getMock();
     $consumesAnnotation->expects($this->exactly(1))->method('getMediaTypes')->will($this->returnValue(array('application/xml')));
     $producesAnnotation = $this->getMockBuilder('Sonno\\Annotation\\Produces')->disableOriginalConstructor()->getMock();
     $producesAnnotation->expects($this->exactly(2))->method('getMediaTypes')->will($this->onConsecutiveCalls(array('application/xml'), array('application/json')));
     $httpMethodAnnotation = $this->getMockBuilder('Sonno\\Annotation\\GET')->disableOriginalConstructor()->getMock();
     $httpMethodAnnotation->expects($this->exactly(1))->method('__toString')->will($this->returnValue('GET'));
     $pathParamAnnotation = $this->getMockBuilder('Sonno\\Annotation\\PathParam')->disableOriginalConstructor()->getMock();
     $pathParamAnnotation->expects($this->exactly(1))->method('getParams')->will($this->returnValue(array('id')));
     $contextAnnotation = $this->getMockBuilder('Sonno\\Annotation\\Context')->disableOriginalConstructor()->getMock();
     $contextAnnotation->expects($this->exactly(1))->method('getContext')->will($this->returnValue('Request'));
     $reader = $this->getMockBuilder('Sonno\\Annotation\\Reader\\DoctrineReader')->disableOriginalConstructor()->getMock();
     $reader->expects($this->exactly(3))->method('getClassAnnotation')->will($this->onConsecutiveCalls($pathAnnotation, $consumesAnnotation, $producesAnnotation));
     $reader->expects($this->exactly(9))->method('getMethodAnnotation')->will($this->onConsecutiveCalls($httpMethodAnnotation, $pathAnnotation, null, $producesAnnotation, $pathParamAnnotation, null, null, null, null));
     $reader->expects($this->exactly(1))->method('getPropertyAnnotation')->will($this->onConsecutiveCalls($contextAnnotation));
     $driver = new AnnotationDriver(array('Sonno\\Test\\Configuration\\Driver\\Asset\\ValidResourceBasic'), $reader);
     $driver->parseConfig();
 }
Example #2
0
File: index.php Project: 360i/sonno
// up and running.
use Sonno\Configuration\Driver\AnnotationDriver, Sonno\Annotation\Reader\DoctrineReader, Sonno\Application\Application, Sonno\Http\Request\Request, Doctrine\Common\Annotations\AnnotationReader, Doctrine\Common\Annotations\AnnotationRegistry;
// Configure Doctrine's annotation reader. For more information see the
// following link:
// http://doctrine-project.org/docs/common/2.1/en/reference/annotations.html
$doctrineReader = new AnnotationReader();
AnnotationRegistry::registerAutoloadNamespace('Sonno\\Annotation', realpath(__DIR__ . '/../../../src'));
// Sonno ships with one Annotation reader adapter, and that supports Doctrine's
// Annotation reader. Sonno's adapter-based architecture allows for developers
// to write Annotation readers using any library. The
// Sonno\Annotation\Reader\ReaderInterface is provided so different readers can
// use different engines.
$annotationReader = new DoctrineReader($doctrineReader);
// An array of Resource classes must be provided so the AnnotationDriver knows
// where to find annotated resource classes.
$resources = array('Sonno\\Example\\Resource\\HelloResource');
// Sonno\Configuration\Configuration objects are retrieved through Configuration
// Drivers. A driver must implement the
// Sonno\Configuration\Driver\DriverInterface interface. Sonno ships with a
// driver that parses annotations, but future versions will ship with config
// drivers for XML, YAML, and anything else that may make sense.
$driver = new AnnotationDriver($resources, $annotationReader);
$config = $driver->parseConfig();
// It's also possible to set other config options, such as the base path.
// $config->setBasePath('/api/rest/v1');
// The application class requires a Sonno\Configuration\Configuration object
// to run. This object can be cached so annotations don't have to be read and
// parsed on every request. It's also not necessary that this config object
// is created using annotations.
$application = new Application($config);
$application->run(Request::getInstanceOfCurrentRequest());