Skip to content

Commit

Permalink
strict object calisthenics and phpdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleMinotto committed Feb 27, 2015
1 parent 0810ba2 commit 4d26a06
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 29 deletions.
14 changes: 11 additions & 3 deletions EmbedlyServiceProvider.php
Expand Up @@ -17,11 +17,15 @@
class EmbedlyServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}
* Registers services and parameters on the app.
*
* @param Application $app Silex application.
*
* @return void
*/
public function register(Application $app)
{
$app['embedly'] = $app->share(function ($app) {
$app['embedly'] = $app->share(function () {
return new Client();
});

Expand All @@ -30,7 +34,11 @@ public function register(Application $app)
}

/**
* {@inheritdoc}
* Bootstraps the services.
*
* @param Application $app Silex application.
*
* @return void
*/
public function boot(Application $app)
{
Expand Down
84 changes: 58 additions & 26 deletions Tests/EmbedlyServiceProviderTest.php
Expand Up @@ -16,8 +16,12 @@
class EmbedlyServiceProviderTest extends PHPUnit_Framework_TestCase
{
/**
* Test service registration.
*
* @covers ::boot
* @covers ::register
*
* @return void
*/
public function testRegisterServiceProvider()
{
Expand All @@ -29,7 +33,11 @@ public function testRegisterServiceProvider()
}

/**
* Test Twig extension integration in Silex (enabled, default).
*
* @coversNothing
*
* @return void
*/
public function testTwigExtension()
{
Expand All @@ -42,7 +50,11 @@ public function testTwigExtension()
}

/**
* Test Twig extension integration in Silex (disabled).
*
* @coversNothing
*
* @return void
*/
public function testMissingTwigExtension()
{
Expand All @@ -57,46 +69,39 @@ public function testMissingTwigExtension()
}

/**
* Simulates a request and controls the output.
* Simulates a request and controls the output (generic).
*
* @coversNothing
*
* @return void
*/
public function testRequest()
public function testRequestGeneric()
{
$app = new Application();
$app->register(new EmbedlyServiceProvider(), array(
'embedly.api_key' => $_ENV['api_key'],
));

$app->get('/', function () use ($app) {
$data = $app['embedly']->oembed([
'url' => $_ENV['url'],
]);

return $app->json($data);
});

$request = Request::create('/');
$response = $app->handle($request);

$embed = json_decode($response->getContent(), true);
$embed = $this->createTestRequestResponse();

$this->assertInternalType('array', $embed);

$this->assertArrayHasKey('type', $embed);
$this->assertContains($embed['type'], ['photo', 'video', 'rich', 'link', 'error']);
}

/**
* Simulates a request and controls the output (type subsection).
*
* @coversNothing
*
* @return void
*/
public function testRequestType()
{
$embed = $this->createTestRequestResponse();

switch ($embed['type']) {
case 'error':
$this->assertArrayHasKey('error_code', $embed);
$this->assertArrayHasKey('error_message', $embed);
$this->assertArrayHasKey('url', $embed);
$this->assertNotEmpty(array_intersect(array_keys($embed), ['error_code', 'error_message', 'url']));

break;
case 'photo':
$this->assertArrayHasKey('height', $embed);
$this->assertArrayHasKey('url', $embed);
$this->assertArrayHasKey('width', $embed);
$this->assertNotEmpty(array_intersect(array_keys($embed), ['height', 'url', 'width']));

break;
case 'rich':
Expand All @@ -106,4 +111,31 @@ public function testRequest()
break;
}
}

/**
* Helper method used to create a request.
*
* @return array
*/
private function createTestRequestResponse()
{
$app = new Application();
$app->register(new EmbedlyServiceProvider(), array(
'embedly.api_key' => $_ENV['api_key'],
));

$app->get('/', function () use ($app) {
$data = $app['embedly']->oembed([
'url' => $_ENV['url'],
]);

return $app->json($data);
});


$request = Request::create('/');
$response = $app->handle($request);

return json_decode($response->getContent(), true);
}
}

0 comments on commit 4d26a06

Please sign in to comment.