|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the FOSRestBundle package. |
| 5 | + * |
| 6 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FOS\RestBundle\Tests\Routing\Loader; |
| 13 | + |
| 14 | +use FOS\RestBundle\Routing\Loader\DirectoryRouteLoader; |
| 15 | +use FOS\RestBundle\Routing\Loader\RestRouteProcessor; |
| 16 | +use Symfony\Component\Config\Loader\LoaderResolver; |
| 17 | + |
| 18 | +class DirectoryRouteLoaderTest extends LoaderTest |
| 19 | +{ |
| 20 | + public function testLoad() |
| 21 | + { |
| 22 | + $collection = $this->loadFromDirectory(__DIR__.'/../../Fixtures/Controller/Directory'); |
| 23 | + |
| 24 | + $this->assertCount(9, $collection); |
| 25 | + |
| 26 | + $this->assertInstanceOf('Symfony\Component\Routing\Route', $collection->get('get_users')); |
| 27 | + $this->assertInstanceOf('Symfony\Component\Routing\Route', $collection->get('get_user')); |
| 28 | + $this->assertInstanceOf('Symfony\Component\Routing\Route', $collection->get('post_users')); |
| 29 | + $this->assertInstanceOf('Symfony\Component\Routing\Route', $collection->get('put_user')); |
| 30 | + $this->assertInstanceOf('Symfony\Component\Routing\Route', $collection->get('get_comments')); |
| 31 | + $this->assertInstanceOf('Symfony\Component\Routing\Route', $collection->get('put_comment')); |
| 32 | + $this->assertInstanceOf('Symfony\Component\Routing\Route', $collection->get('get_topics')); |
| 33 | + $this->assertInstanceOf('Symfony\Component\Routing\Route', $collection->get('get_topic')); |
| 34 | + $this->assertInstanceOf('Symfony\Component\Routing\Route', $collection->get('put_topic')); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @dataProvider supportsDataProvider |
| 39 | + */ |
| 40 | + public function testSupports($resource, $type, $expected) |
| 41 | + { |
| 42 | + $loader = new DirectoryRouteLoader(new RestRouteProcessor()); |
| 43 | + |
| 44 | + if ($expected) { |
| 45 | + $this->assertTrue($loader->supports($resource, $type)); |
| 46 | + } else { |
| 47 | + $this->assertFalse($loader->supports($resource, $type)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public function supportsDataProvider() |
| 52 | + { |
| 53 | + return array( |
| 54 | + 'existing-directory' => array(__DIR__.'/../../Fixtures/Controller', 'rest', true), |
| 55 | + 'non-existing-directory' => array(__DIR__.'/Fixtures/Controller', 'rest', false), |
| 56 | + 'class-name' => array('FOS\RestBundle\Tests\Fixtures\Controller\UsersController', 'rest', false), |
| 57 | + 'null-type' => array(__DIR__.'/../../Fixtures/Controller', null, false), |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + private function loadFromDirectory($resource) |
| 62 | + { |
| 63 | + $directoryLoader = new DirectoryRouteLoader(new RestRouteProcessor()); |
| 64 | + $controllerLoader = $this->getControllerLoader(); |
| 65 | + |
| 66 | + // LoaderResolver sets the resolvers on the loaders passed to it |
| 67 | + new LoaderResolver(array($directoryLoader, $controllerLoader)); |
| 68 | + |
| 69 | + return $directoryLoader->load($resource, 'rest'); |
| 70 | + } |
| 71 | +} |
0 commit comments