/** * Returns ResourceMetadata for the specified resource. * * @param string $resourceName * @throws \BedRest\Resource\Mapping\Exception * @return \BedRest\Resource\Mapping\ResourceMetadata */ public function getMetadataByResourceName($resourceName) { $this->getAllMetadata(); if (!isset($this->resourceClassMap[$resourceName])) { throw Exception::resourceNotFound($resourceName); } return $this->loadedMetadata[$this->resourceClassMap[$resourceName]]; }
/** * {@inheritDoc} * * This has been lifted pretty much wholesale from Doctrine ORM, so credit where credit is due. * @see Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver */ public function getAllClassNames() { if (!$this->paths) { throw Exception::pathsRequired(); } $classes = array(); $includedFiles = array(); foreach ($this->paths as $path) { if (!is_dir($path)) { throw Exception::invalidPath($path); } $iterator = new \RegexIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY), '/^.+\\.php$/i', \RecursiveRegexIterator::GET_MATCH); foreach ($iterator as $file) { $sourceFile = realpath($file[0]); require_once $sourceFile; $includedFiles[] = $sourceFile; } } $declared = get_declared_classes(); foreach ($declared as $className) { $rc = new \ReflectionClass($className); $sourceFile = $rc->getFileName(); if (in_array($sourceFile, $includedFiles) && $this->isResource($className)) { $classes[] = $className; } } return $classes; }
/** * @dataProvider requests * * @param string $method */ public function testDispatchToNonExistentResourceThrows404Exception($method) { $resourceName = 'nonExistentResource'; $notFoundException = \BedRest\Resource\Mapping\Exception::resourceNotFound($resourceName); // configure the Dispatcher with the necessary dependencies $rmdFactory = $this->getMockResourceMetadataFactory(); $rmdFactory->expects($this->any())->method('getMetadataByResourceName')->with($resourceName)->will($this->throwException($notFoundException)); $rmdFactory->expects($this->any())->method('getMetadataFor')->with($resourceName)->will($this->throwException($notFoundException)); $this->dispatcher->setResourceMetadataFactory($rmdFactory); $this->dispatcher->setServiceMetadataFactory($this->getMockServiceMetadataFactory($method)); $this->dispatcher->setServiceLocator($this->getMockServiceLocator()); $eventManager = $this->getMock('BedRest\\Events\\EventManager'); $this->dispatcher->setEventManager($eventManager); // form a basic request $request = new Request(); $request->setResource($resourceName); $request->setMethod($method); // test an exception is thrown $this->setExpectedException('BedRest\\Rest\\Exception', '', 404); $this->dispatcher->dispatch($request); }