-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dea30a8
commit 5b2171a
Showing
9 changed files
with
460 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
/** | ||
* AnimeDb package. | ||
* | ||
* @author Peter Gribanov <[email protected]> | ||
* @copyright Copyright (c) 2011, Peter Gribanov | ||
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 | ||
*/ | ||
|
||
namespace AnimeDb\Bundle\MyAnimeListBrowserBundle; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class AnimeDbMyAnimeListBrowserBundle extends Bundle | ||
{ | ||
} |
36 changes: 36 additions & 0 deletions
36
src/DependencyInjection/AnimeDbMyAnimeListBrowserExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/** | ||
* AnimeDb package. | ||
* | ||
* @author Peter Gribanov <[email protected]> | ||
* @copyright Copyright (c) 2011, Peter Gribanov | ||
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 | ||
*/ | ||
|
||
namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Symfony\Component\DependencyInjection\Loader; | ||
|
||
class AnimeDbMyAnimeListBrowserExtension extends Extension | ||
{ | ||
/** | ||
* @param array $configs | ||
* @param ContainerBuilder $container | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | ||
$loader->load('services.yml'); | ||
|
||
$config = $this->processConfiguration(new Configuration(), $configs); | ||
|
||
$container->getDefinition('anime_db.my_anime_list.browser') | ||
->replaceArgument(1, $config['host']) | ||
->replaceArgument(2, $config['client']) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/** | ||
* AnimeDb package. | ||
* | ||
* @author Peter Gribanov <[email protected]> | ||
* @copyright Copyright (c) 2011, Peter Gribanov | ||
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 | ||
*/ | ||
|
||
namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
class Configuration implements ConfigurationInterface | ||
{ | ||
/** | ||
* Config tree builder. | ||
* | ||
* Example config: | ||
* | ||
* anime_db_my_anime_list_browser: | ||
* host: 'http://myanimelist.net' | ||
* client: 'My Custom Bot 1.0' | ||
* | ||
* @return ArrayNodeDefinition | ||
*/ | ||
public function getConfigTreeBuilder() | ||
{ | ||
return (new TreeBuilder()) | ||
->root('anime_db_my_anime_list_browser') | ||
->children() | ||
->scalarNode('host') | ||
->defaultValue('http://myanimelist.net') | ||
->cannotBeEmpty() | ||
->end() | ||
->scalarNode('client') | ||
->defaultValue('') | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
services: | ||
anime_db.my_anime_list.browser: | ||
class: AnimeDb\Bundle\MyAnimeListBrowserBundle\Service\Browser | ||
arguments: [ '@anime_db.my_anime_list.browser.client', ~, ~ ] | ||
|
||
anime_db.my_anime_list.browser.client: | ||
class: GuzzleHttp\Client | ||
public: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/** | ||
* AnimeDb package. | ||
* | ||
* @author Peter Gribanov <[email protected]> | ||
* @copyright Copyright (c) 2011, Peter Gribanov | ||
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 | ||
*/ | ||
|
||
namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\Service; | ||
|
||
use GuzzleHttp\Client; | ||
|
||
class Browser | ||
{ | ||
/** | ||
* @var Client | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $host; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $app_client; | ||
|
||
/** | ||
* @param Client $client | ||
* @param string $host | ||
* @param string $app_client | ||
*/ | ||
public function __construct(Client $client, $host, $app_client) | ||
{ | ||
$this->client = $client; | ||
$this->host = $host; | ||
$this->app_client = $app_client; | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @param array $options | ||
* | ||
* @return string | ||
*/ | ||
public function get($path, array $options = []) | ||
{ | ||
if ($this->app_client) { | ||
$options['headers'] = isset($options['headers']) ? $options['headers'] : []; | ||
$options['headers']['User-Agent'] = $this->app_client; | ||
} | ||
|
||
$response = $this->client->request('GET', $this->host.$path, $options); | ||
|
||
$content = $response->getBody()->getContents(); | ||
|
||
return $content; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
tests/DependencyInjection/AnimeDbMyAnimeListBrowserExtensionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
/** | ||
* AnimeDb package. | ||
* | ||
* @author Peter Gribanov <[email protected]> | ||
* @copyright Copyright (c) 2011, Peter Gribanov | ||
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 | ||
*/ | ||
|
||
namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\Tests\DependencyInjection; | ||
|
||
use AnimeDb\Bundle\MyAnimeListBrowserBundle\DependencyInjection\AnimeDbMyAnimeListBrowserExtension; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
class AnimeDbMyAnimeListBrowserExtensionTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject|ContainerBuilder | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @var AnimeDbMyAnimeListBrowserExtension | ||
*/ | ||
private $extension; | ||
|
||
protected function setUp() | ||
{ | ||
$this->container = $this->getMock(ContainerBuilder::class); | ||
$this->extension = new AnimeDbMyAnimeListBrowserExtension(); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function config() | ||
{ | ||
return [ | ||
[ | ||
[], | ||
'http://myanimelist.net', | ||
'', | ||
], | ||
[ | ||
[ | ||
'anime_db_my_anime_list_browser' => [ | ||
'host' => 'https://myanimelist.net', | ||
'client' => 'My Custom Bot 1.0', | ||
], | ||
], | ||
'https://myanimelist.net', | ||
'My Custom Bot 1.0', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider config | ||
* | ||
* @param array $config | ||
* @param string $host | ||
* @param string $client | ||
*/ | ||
public function testLoad(array $config, $host, $client) | ||
{ | ||
$browser = $this->getMock(Definition::class); | ||
$browser | ||
->expects($this->at(0)) | ||
->method('replaceArgument') | ||
->with(1, $host) | ||
->will($this->returnSelf()) | ||
; | ||
$browser | ||
->expects($this->at(1)) | ||
->method('replaceArgument') | ||
->with(2, $client) | ||
->will($this->returnSelf()) | ||
; | ||
|
||
$this->container | ||
->expects($this->once()) | ||
->method('getDefinition') | ||
->with('anime_db.my_anime_list.browser') | ||
->will($this->returnValue($browser)) | ||
; | ||
|
||
$this->extension->load($config, $this->container); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/** | ||
* AnimeDb package. | ||
* | ||
* @author Peter Gribanov <[email protected]> | ||
* @copyright Copyright (c) 2011, Peter Gribanov | ||
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 | ||
*/ | ||
|
||
namespace AnimeDb\Bundle\MyAnimeListBrowserBundle\Tests\DependencyInjection; | ||
|
||
use AnimeDb\Bundle\MyAnimeListBrowserBundle\DependencyInjection\Configuration; | ||
use Symfony\Component\Config\Definition\ArrayNode; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ScalarNode; | ||
|
||
class ConfigurationTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Configuration | ||
*/ | ||
private $configuration; | ||
|
||
protected function setUp() | ||
{ | ||
$this->configuration = new Configuration(); | ||
} | ||
|
||
public function testConfigTree() | ||
{ | ||
$tree_builder = $this->configuration->getConfigTreeBuilder(); | ||
|
||
$this->assertInstanceOf(TreeBuilder::class, $tree_builder); | ||
|
||
/* @var $tree ArrayNode */ | ||
$tree = $tree_builder->buildTree(); | ||
|
||
$this->assertInstanceOf(ArrayNode::class, $tree); | ||
$this->assertEquals('anime_db_my_anime_list_browser', $tree->getName()); | ||
|
||
/* @var $children ScalarNode[] */ | ||
$children = $tree->getChildren(); | ||
|
||
$this->assertInternalType('array', $children); | ||
$this->assertEquals(['host', 'client'], array_keys($children)); | ||
|
||
$this->assertInstanceOf(ScalarNode::class, $children['host']); | ||
$this->assertEquals('http://myanimelist.net', $children['host']->getDefaultValue()); | ||
$this->assertFalse($children['host']->isRequired()); | ||
|
||
$this->assertInstanceOf(ScalarNode::class, $children['client']); | ||
$this->assertEquals('', $children['client']->getDefaultValue()); | ||
$this->assertFalse($children['client']->isRequired()); | ||
} | ||
} |
Oops, something went wrong.