Skip to content

Commit

Permalink
add src and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Jul 19, 2017
1 parent dea30a8 commit 5b2171a
Show file tree
Hide file tree
Showing 9 changed files with 460 additions and 0 deletions.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,55 @@

MyAnimeList.net API browser
===========================

Installation
------------

Pretty simple with [Composer](http://packagist.org), run:

```sh
composer require anime-db/my-anime-list-browser-bundle
```

Add AnimeDbWorldArtBrowserBundle to your application kernel

```php
// app/appKernel.php

public function registerBundles()
{
$bundles = array(
// ...
new AnimeDb\Bundle\MyAnimeListBrowserBundle\AnimeDbMyAnimeListBrowserBundle(),
);
}
```

Configuration
-------------

```yml
anime_db_my_anime_list_browser:
# Host name
# As a default used 'http://myanimelist.net'
host: 'http://myanimelist.net'

# HTTP User-Agent
# No default value
client: 'My Custom Bot 1.0'
```
Usage
-----
Get info for anime [Cowboy Bebop](https://myanimelist.net/anime/1/Cowboy_Bebop):
```php
$content = $this->get('anime_db.my_anime_list.browser')->get('/anime/1');
```

License
-------

This bundle is under the [GPL v3 license](http://opensource.org/licenses/GPL-3.0).
See the complete license in the file: LICENSE
17 changes: 17 additions & 0 deletions src/AnimeDbMyAnimeListBrowserBundle.php
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 src/DependencyInjection/AnimeDbMyAnimeListBrowserExtension.php
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'])
;
}
}
46 changes: 46 additions & 0 deletions src/DependencyInjection/Configuration.php
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()
;
}
}
8 changes: 8 additions & 0 deletions src/Resources/config/services.yml
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
63 changes: 63 additions & 0 deletions src/Service/Browser.php
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;
}
}
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);
}
}
56 changes: 56 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
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());
}
}
Loading

0 comments on commit 5b2171a

Please sign in to comment.