Skip to content

Commit

Permalink
minor fixes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardofiorani committed Sep 1, 2015
1 parent 8d67ca9 commit 6d64b26
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ require __DIR__ . '/vendor/autoload.php';
$vsd = new VideoServiceDetector();

//This is where the magic is done
$vsd->setRenderer('MyOwnRenderer', 'MyVendor\\MyRenderer\\Factory\\MyOwnRendererFactory');
$vsd->getServiceContainer()->setRenderer('MyOwnRenderer', 'MyVendor\\MyRenderer\\Factory\\MyOwnRendererFactory');

$video = $vsd->parse('https://www.youtube.com/watch?v=PkOcm_XaWrw');

Expand All @@ -121,9 +121,9 @@ echo $video->getEmbedCode(500,500);
### Currently Suported Services
* Youtube
* Vimeo
* Dailymotion

# TODO List goals for release 1.0:

* Fix the Exceptions Messages
* Create PHPUnit Tests
* Add more Services
2 changes: 1 addition & 1 deletion example/RegisteringANewService.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ $patterns = array(
);

//Register the new service
$vsd->registerService($serviceName, $patterns, "\\MyVendor\\ServiceAdapter\\Factory\\DailyMotionServiceAdapterFactory");
$vsd->getServiceContainer()->registerService($serviceName, $patterns, "\\MyVendor\\ServiceAdapter\\Factory\\DailyMotionServiceAdapterFactory");

//This will get you an DailyMotionServiceAdapter
$video = $vsd->parse('http://www.dailymotion.com/video/x33ncwc_kittens-fight-in-tiny-boxing-ring_animals');
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/Vimeo/VimeoServiceAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function getThumbnail($size)
*/
public function getEmbedUrl($autoplay = false)
{
return "http://player.vimeo.com/video/" . $this->getVideoId() . "?byline=0&portrait=0&amp" . ($autoplay ? '&amp&autoplay=1' : '');
return "http://player.vimeo.com/video/" . $this->getVideoId() . ($autoplay ? '?autoplay=1' : '');
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Adapter/Youtube/YoutubeServiceAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class YoutubeServiceAdapter extends AbstractServiceAdapter
public function __construct($url, $pattern, EmbedRendererInterface $renderer)
{
preg_match($pattern, $url, $match);
$videoId = $match[2];
if (isset($match[2])) {
$videoId = $match[2];
}
if (empty($videoId)) {
$videoId = $match[1];
}
Expand Down
11 changes: 10 additions & 1 deletion src/Detector/VideoServiceDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class VideoServiceDetector
*/
private $serviceContainer;

/**
* @var array
*/
private $parsedUrls = array();

/**
* VideoServiceDetector constructor.
*/
Expand All @@ -32,6 +37,9 @@ public function __construct()
*/
public function parse($url)
{
if (isset($this->parsedUrls[$url])) {
return $this->parsedUrls[$url];
}
/** @var array $patterns */
/** @var string $serviceName */
foreach ($this->getServiceContainer()->getPatterns() as $serviceName => $patterns) {
Expand All @@ -40,7 +48,8 @@ public function parse($url)
if (false != preg_match($pattern, $url)) {
$factory = $this->getServiceContainer()->getFactory($serviceName);

return $factory($url, $pattern, $this->getServiceContainer()->getRenderer());
return $this->parsedUrls[$url] = $factory($url, $pattern,
$this->getServiceContainer()->getRenderer());
}
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/Exception/DuplicatedServiceNameException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,4 @@
class DuplicatedServiceNameException extends Exception
{

/**
* DuplicatedServiceNameException constructor.
* @param string $message
* @param int $code
* @param Exception $previous
*/
public function __construct($code = 0, Exception $previous = null)
{
return parent::__construct("", $code, $previous);
}
}
70 changes: 70 additions & 0 deletions test/Detector/ServiceDetectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Created by PhpStorm.
* User: Ricardo Fiorani
* Date: 31/08/2015
* Time: 21:54
*/

namespace RicardoFiorani\Test;

use PHPUnit_Framework_TestCase;
use RicardoFiorani\Detector\VideoServiceDetector;
use RicardoFiorani\Exception\ServiceNotAvailableException;

class ServiceDetectorTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider youtubeDataProvider
* @param $url
* @param $expectedEmbedCode
* @throws ServiceNotAvailableException
*/
public function testCanParseYoutubeUrl($url, $expectedEmbedCode)
{
$detector = new VideoServiceDetector();
$video = $detector->parse($url);
$this->assertEquals($video->getEmbedUrl(), $expectedEmbedCode);
}

public function youtubeDataProvider()
{

return array(
'Normal Youtube URL' => array(
'https://www.youtube.com/watch?v=mWRsgZuwf_8',
'http://www.youtube.com/embed/mWRsgZuwf_8',
),
'Short Youtube URL' => array(
'https://youtu.be/JMLBOKVfHaA',
'http://www.youtube.com/embed/JMLBOKVfHaA',
),
'Embed Youtube URL' => array(
'<iframe width="420" height="315" src="https://www.youtube.com/embed/vwp9JkaESdg" frameborder="0" allowfullscreen></iframe>',
'http://www.youtube.com/embed/vwp9JkaESdg',
),
);
}

/**
* @dataProvider vimeoDataProvider
* @param string $url
* @param string $expectedEmbedCode
*/
public function testCanParseVimeoUrl($url, $expectedEmbedCode)
{
$detector = new VideoServiceDetector();
$video = $detector->parse($url);
$this->assertEquals($video->getEmbedUrl(), $expectedEmbedCode);
}

public function vimeoDataProvider()
{
return array(
'Common Vimeo URL' => array(
'https://vimeo.com/137781541',
'http://player.vimeo.com/video/137781541'
)
);
}
}

0 comments on commit 6d64b26

Please sign in to comment.