├── .gitignore ├── .travis.yml ├── tests ├── bootstrap.php └── Dflydev │ └── Twig │ └── Extension │ └── GitHubGist │ ├── Transport │ ├── NativePhpTransportTest.php │ └── fixtures │ │ └── 1234 │ ├── Cache │ ├── ArrayCacheTest.php │ └── FilesystemCacheTest.php │ └── GistTwigExtensionTest.php ├── phpunit.xml.dist ├── src └── Dflydev │ └── Twig │ └── Extension │ └── GitHubGist │ ├── Transport │ ├── TransportInterface.php │ └── NativePhpTransport.php │ ├── Cache │ ├── CacheInterface.php │ ├── ArrayCache.php │ └── FilesystemCache.php │ └── GistTwigExtension.php ├── composer.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | phpunit.xml 3 | vendor 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3.3 5 | - 5.3 6 | - 5.4 7 | 8 | before_script: 9 | - curl -s http://getcomposer.org/installer | php -- --quiet 10 | - php composer.phar install --dev 11 | 12 | script: phpunit --coverage-text 13 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | add('dflydev\\tests\\twig\\extension\\gitHub\\gist', 'tests'); 13 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./tests 6 | 7 | 8 | 9 | 10 | 11 | ./src/ 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/Dflydev/Twig/Extension/GitHubGist/Transport/TransportInterface.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | interface TransportInterface 20 | { 21 | /** 22 | * Fetch the contents of a gist 23 | * 24 | * @param array $id 25 | */ 26 | public function fetchGist($id); 27 | } 28 | -------------------------------------------------------------------------------- /tests/Dflydev/Twig/Extension/GitHubGist/Transport/NativePhpTransportTest.php: -------------------------------------------------------------------------------- 1 | fetchGist(1234); 22 | $this->assertEquals('1757612', $gist['id'], 'Should have a valid ID'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dflydev/github-gist-twig-extension", 3 | "description": "GitHub Gist Twig Extension", 4 | "homepage": "http://github.com/dflydev/dflydev-github-gist-twig-extension", 5 | "keywords": ["github", "gist", "twig", "extension"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Dragonfly Development Inc.", 10 | "email": "info@dflydev.com", 11 | "homepage": "http://dflydev.com" 12 | }, 13 | { 14 | "name": "Beau Simensen", 15 | "email": "beau@dflydev.com", 16 | "homepage": "http://beausimensen.com" 17 | } 18 | ], 19 | "require": { 20 | "php": ">=5.3.2", 21 | "twig/twig": ">=1.9.1,<2.0-dev" 22 | }, 23 | "require-dev": { 24 | "mikey179/vfsStream": "1.*" 25 | }, 26 | "autoload": { 27 | "psr-0": { 28 | "Dflydev\\Twig\\Extension\\GitHubGist": "src" 29 | } 30 | }, 31 | "extra": { 32 | "branch-alias": { 33 | "dev-master": "2.0-dev" 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Dragonfly Development Inc. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GitHub Gist Twig Extension 2 | ========================== 3 | 4 | A simple [Twig](http://twig.sensiolabs.org/) extension for embedding 5 | [GitHub](http://github.com) [Gist](http://gist.github.com) snippets 6 | into Twig templates. 7 | 8 | Requirements 9 | ------------ 10 | 11 | * PHP: >=5.3.2 12 | * Twig: >=1.5,<2 13 | 14 | 15 | Usage 16 | ----- 17 | 18 | ```php 19 | addExtension($gistTwigExtension); 23 | ``` 24 | 25 | Once enabled, gists can be embedded by: 26 | 27 | ```twig 28 | {{ gist(3360578) }} 29 | ``` 30 | 31 | 32 | Advanced Usage 33 | -------------- 34 | 35 | The `GistTwigExtension` can optionally accept a `TransportInterface` 36 | and a `CacheInterface` implementation. By default `NativePhpTransport` 37 | and `ArrayCache` are selected if not specified. 38 | 39 | 40 | License 41 | ------- 42 | 43 | MIT, see LICENSE. 44 | 45 | 46 | Community 47 | --------- 48 | 49 | If you have questions or want to help out, join us in the 50 | [#dflydev](irc://irc.freenode.net/#dflydev) channel on irc.freenode.net. 51 | -------------------------------------------------------------------------------- /src/Dflydev/Twig/Extension/GitHubGist/Transport/NativePhpTransport.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class NativePhpTransport implements TransportInterface 20 | { 21 | /** 22 | * Base URI 23 | * 24 | * @var string 25 | */ 26 | protected $baseUri; 27 | 28 | /** 29 | * Constructor 30 | * 31 | * @param string $baseUri 32 | */ 33 | public function __construct($baseUri = 'https://api.github.com/gists/') 34 | { 35 | $this->baseUri = $baseUri; 36 | } 37 | 38 | /** 39 | * {@inheritdoc} 40 | */ 41 | public function fetchGist($id) 42 | { 43 | $response = file_get_contents($this->baseUri.$id); 44 | 45 | return json_decode($response, true); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Dflydev/Twig/Extension/GitHubGist/Cache/CacheInterface.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | interface CacheInterface 20 | { 21 | /** 22 | * Determine if a gist exists 23 | * 24 | * @param unknown_type $id 25 | */ 26 | public function exists($id); 27 | 28 | /** 29 | * Get the content of a gist 30 | * 31 | * @param string $id 32 | * 33 | * @return array 34 | */ 35 | public function get($id); 36 | 37 | /** 38 | * Set the content of a gist 39 | * 40 | * @param string $id ID 41 | * @param array $content Content 42 | */ 43 | public function set($id, $content); 44 | 45 | /** 46 | * Expire a gist 47 | * 48 | * @param string $id 49 | */ 50 | public function expire($id); 51 | } 52 | -------------------------------------------------------------------------------- /src/Dflydev/Twig/Extension/GitHubGist/Cache/ArrayCache.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class ArrayCache implements CacheInterface 20 | { 21 | /** 22 | * Internal cache 23 | * 24 | * @var array 25 | */ 26 | protected $cache = array(); 27 | 28 | /** 29 | * Constructor 30 | * @param array $cache Default cache structure 31 | */ 32 | public function __construct(array $cache = array()) 33 | { 34 | $this->cache = $cache; 35 | } 36 | 37 | /** 38 | * {@inheritdoc} 39 | */ 40 | public function exists($id) 41 | { 42 | return array_key_exists($id, $this->cache); 43 | } 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | public function get($id) 49 | { 50 | return $this->cache[$id]; 51 | } 52 | 53 | /** 54 | * {@inheritdoc} 55 | */ 56 | public function set($id, $content) 57 | { 58 | $this->cache[$id] = $content; 59 | } 60 | 61 | /** 62 | * {@inheritdoc} 63 | */ 64 | public function expire($id) 65 | { 66 | unset($this->cache[$id]); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /tests/Dflydev/Twig/Extension/GitHubGist/Transport/fixtures/1234: -------------------------------------------------------------------------------- 1 | { 2 | "forks": [ 3 | 4 | ], 5 | "url": "https://api.github.com/gists/1757612", 6 | "description": "Sample Gist", 7 | "public": true, 8 | "files": { 9 | "simple.txt": { 10 | "type": "text/plain", 11 | "raw_url": "https://gist.github.com/raw/1757612/5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689/simple.txt", 12 | "content": "Hello World", 13 | "size": 11, 14 | "filename": "simple.txt", 15 | "language": "Text" 16 | } 17 | }, 18 | "git_pull_url": "git://gist.github.com/1757612.git", 19 | "history": [ 20 | { 21 | "url": "https://api.github.com/gists/1757612/def2c5bec07d1408b230d3f5aa213d47e8196ee1", 22 | "committed_at": "2012-02-07T06:15:08Z", 23 | "change_status": { 24 | "deletions": 0, 25 | "total": 1, 26 | "additions": 1 27 | }, 28 | "user": { 29 | "url": "https://api.github.com/users/simensen", 30 | "gravatar_id": "23d971deeb3975a7d28246192fbbe7b7", 31 | "login": "simensen", 32 | "avatar_url": "https://secure.gravatar.com/avatar/23d971deeb3975a7d28246192fbbe7b7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png", 33 | "id": 191200 34 | }, 35 | "version": "def2c5bec07d1408b230d3f5aa213d47e8196ee1" 36 | } 37 | ], 38 | "git_push_url": "git@gist.github.com:1757612.git", 39 | "comments": 0, 40 | "updated_at": "2012-02-07T06:15:08Z", 41 | "user": { 42 | "url": "https://api.github.com/users/simensen", 43 | "gravatar_id": "23d971deeb3975a7d28246192fbbe7b7", 44 | "login": "simensen", 45 | "avatar_url": "https://secure.gravatar.com/avatar/23d971deeb3975a7d28246192fbbe7b7?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png", 46 | "id": 191200 47 | }, 48 | "html_url": "https://gist.github.com/1757612", 49 | "id": "1757612", 50 | "created_at": "2012-02-07T06:15:08Z" 51 | } 52 | -------------------------------------------------------------------------------- /src/Dflydev/Twig/Extension/GitHubGist/Cache/FilesystemCache.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | class FilesystemCache implements CacheInterface 20 | { 21 | /** 22 | * Base path 23 | * 24 | * @var string 25 | */ 26 | protected $basePath; 27 | 28 | /** 29 | * Constructor 30 | * 31 | * @param string $basePath Base path 32 | */ 33 | public function __construct($basePath) 34 | { 35 | $this->basePath = $basePath; 36 | } 37 | 38 | /** 39 | * {@inheritdoc} 40 | */ 41 | public function exists($id) 42 | { 43 | return file_exists($this->generatePathname($id)); 44 | } 45 | 46 | /** 47 | * {@inheritdoc} 48 | */ 49 | public function get($id) 50 | { 51 | return unserialize(file_get_contents($this->generatePathname($id))); 52 | } 53 | 54 | /** 55 | * {@inheritdoc} 56 | */ 57 | public function set($id, $content) 58 | { 59 | file_put_contents($this->generatePathname($id), serialize($content)); 60 | } 61 | 62 | /** 63 | * {@inheritdoc} 64 | */ 65 | public function expire($id) 66 | { 67 | unlink($this->generatePathname($id)); 68 | } 69 | 70 | /** 71 | * Generate a pathname for an ID 72 | * 73 | * @param string $id ID 74 | * 75 | * @return string 76 | */ 77 | protected function generatePathname($id) 78 | { 79 | if (!file_exists($this->basePath)) { 80 | mkdir($this->basePath, 0777, true); 81 | } 82 | 83 | return $this->basePath.'/'.$id; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /tests/Dflydev/Twig/Extension/GitHubGist/Cache/ArrayCacheTest.php: -------------------------------------------------------------------------------- 1 | array())); 21 | $this->assertTrue($cache->exists('a'), 'Should find that "a" exists in cache'); 22 | $this->assertFalse($cache->exists('b'), 'Should not find that "b" exists in cache'); 23 | } 24 | 25 | public function testGet() 26 | { 27 | $cache = new ArrayCache(array('a' => array('test' => 'Hello World'))); 28 | $content = $cache->get('a'); 29 | $this->assertEquals('Hello World', $content['test'], 'Should get valid content for "a" from cache'); 30 | } 31 | 32 | public function testSet() 33 | { 34 | $cache = new ArrayCache(); 35 | $cache->set('a', array('test' => 'Hello World')); 36 | $content = $cache->get('a'); 37 | $this->assertEquals('Hello World', $content['test'], 'Should get valid content for "a" from cache'); 38 | $cache->set('a', array('test' => 'Hello World Too')); 39 | $content = $cache->get('a'); 40 | $this->assertEquals('Hello World Too', $content['test'], 'Should get valid rewritten content for "a" from cache'); 41 | } 42 | 43 | public function testExpire() 44 | { 45 | $cache = new ArrayCache(array('a' => array())); 46 | $this->assertTrue($cache->exists('a'), 'Should find that "a" exists in cache'); 47 | $cache->expire('a'); 48 | $this->assertFalse($cache->exists('a'), 'Should no longer find that "a" exists in cache'); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/Dflydev/Twig/Extension/GitHubGist/Cache/FilesystemCacheTest.php: -------------------------------------------------------------------------------- 1 | serialize(array('test' => 'Hello World')))); 22 | } 23 | public function testExists() 24 | { 25 | $cache = new FilesystemCache(vfsStream::url('root')); 26 | $this->assertTrue($cache->exists('a'), 'Should find that "a" exists in cache'); 27 | $this->assertFalse($cache->exists('b'), 'Should not find that "b" exists in cache'); 28 | } 29 | 30 | public function testGet() 31 | { 32 | $cache = new FilesystemCache(vfsStream::url('root')); 33 | $content = $cache->get('a'); 34 | $this->assertEquals('Hello World', $content['test'], 'Should get valid content for "a" from cache'); 35 | } 36 | 37 | public function testSet() 38 | { 39 | $cache = new FilesystemCache(vfsStream::url('root')); 40 | $cache->set('c', array('test' => 'Hello World')); 41 | $content = $cache->get('c'); 42 | $this->assertEquals('Hello World', $content['test'], 'Should get valid content for "c" from cache'); 43 | $cache->set('c', array('test' => 'Hello World Too')); 44 | $content = $cache->get('c'); 45 | $this->assertEquals('Hello World Too', $content['test'], 'Should get valid rewritten content for "c" from cache'); 46 | } 47 | 48 | public function testExpire() 49 | { 50 | $cache = new FilesystemCache(vfsStream::url('root')); 51 | $this->assertTrue($cache->exists('a'), 'Should find that "a" exists in cache'); 52 | $cache->expire('a'); 53 | $this->assertFalse($cache->exists('a'), 'Should no longer find that "a" exists in cache'); 54 | } 55 | 56 | public function testBasePathMissing() 57 | { 58 | $cache = new FilesystemCache(vfsStream::url('root').'/missing'); 59 | $cache->set('c', array('test' => 'Hello World')); 60 | $content = $cache->get('c'); 61 | $this->assertEquals('Hello World', $content['test'], 'Should get valid content for "c" from cache'); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/Dflydev/Twig/Extension/GitHubGist/GistTwigExtension.php: -------------------------------------------------------------------------------- 1 | 23 | */ 24 | class GistTwigExtension extends \Twig_Extension 25 | { 26 | /** 27 | * Transport 28 | * 29 | * @var TransportInterface 30 | */ 31 | private $transport; 32 | 33 | /** 34 | * Cache 35 | * 36 | * @var CacheInterface 37 | */ 38 | private $cache; 39 | 40 | /** 41 | * Constructor 42 | * 43 | * @param TransportInterface $transport Transport 44 | * @param CacheInterface $cache Cache 45 | */ 46 | public function __construct(TransportInterface $transport = null, CacheInterface $cache = null) 47 | { 48 | $this->transport = $transport; 49 | $this->cache = $cache; 50 | } 51 | 52 | /** 53 | * {@inheritdoc} 54 | */ 55 | public function getFunctions() 56 | { 57 | return array( 58 | 'gist' => new \Twig_Function_Method($this, 'gist', array('pre_escape' => 'html', 'is_safe' => array('html'),)), 59 | ); 60 | } 61 | 62 | /** 63 | * {@inheritdoc} 64 | */ 65 | public function getName() 66 | { 67 | return 'gitHubGist'; 68 | } 69 | 70 | /** 71 | * Get the HTML content for a GitHub Gist 72 | * 73 | * @param string $id ID 74 | * @param string $file File 75 | * 76 | * @return string 77 | */ 78 | public function gist($id, $file = null) 79 | { 80 | if ($this->cache()->exists($id)) { 81 | $gist = $this->cache()->get($id); 82 | } else { 83 | $gist = $this->transport()->fetchGist($id); 84 | $this->cache()->set($id, $gist); 85 | } 86 | $files = array(); 87 | foreach ($gist['files'] as $name => $fileInfo) { 88 | if ($file === null) { 89 | $files[$name] = $fileInfo; 90 | } else { 91 | if ($file == $name) { 92 | $files[$name] = $fileInfo; 93 | break; 94 | } 95 | } 96 | } 97 | 98 | if (!count($files)) { 99 | return ''; 100 | } 101 | 102 | $urlExtra = $file ? '?file='.$file : ''; 103 | $output = ''; 104 | $output .= ''; 105 | $output .= ''; 113 | 114 | return $output; 115 | } 116 | 117 | /** 118 | * Transport 119 | * 120 | * @return TransportInterface 121 | */ 122 | public function transport() 123 | { 124 | if (null === $this->transport) { 125 | $this->transport = new NativePhpTransport; 126 | } 127 | 128 | return $this->transport; 129 | } 130 | 131 | /** 132 | * Set Transport 133 | * 134 | * @param TransportInterface $transport Transport 135 | */ 136 | public function setTransport(TransportInterface $transport) 137 | { 138 | $this->transport = $transport; 139 | } 140 | 141 | /** 142 | * Cache 143 | * 144 | * @return CacheInterface 145 | */ 146 | public function cache() 147 | { 148 | if (null === $this->cache) { 149 | $this->cache = new ArrayCache; 150 | } 151 | 152 | return $this->cache; 153 | } 154 | 155 | /** 156 | * Set Cache 157 | * 158 | * @param CacheInterface $cache Cache 159 | */ 160 | public function setCache(CacheInterface $cache) 161 | { 162 | $this->cache = $cache; 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /tests/Dflydev/Twig/Extension/GitHubGist/GistTwigExtensionTest.php: -------------------------------------------------------------------------------- 1 | 22 | */ 23 | class GistTwigExtensionTest extends \PHPUnit_Framework_TestCase 24 | { 25 | public function testGetFunctions() 26 | { 27 | $gistTwigExtension = new GistTwigExtension(); 28 | $functions = $gistTwigExtension->getFunctions(); 29 | $this->assertTrue(array_key_exists('gist', $functions), 'Should find "gist" to be a valid function'); 30 | } 31 | 32 | public function testGetName() 33 | { 34 | $gistTwigExtension = new GistTwigExtension(); 35 | $this->assertEquals('gitHubGist', $gistTwigExtension->getName(), 'Should have name "gitHubGist"'); 36 | } 37 | 38 | public function testGistCacheSingle() 39 | { 40 | $transport = $this->getMock('Dflydev\Twig\Extension\GitHubGist\Transport\TransportInterface'); 41 | $cache = $this->getMock('Dflydev\Twig\Extension\GitHubGist\Cache\CacheInterface'); 42 | $cache 43 | ->expects($this->once()) 44 | ->method('exists') 45 | ->with($this->equalTo('1234')) 46 | ->will($this->returnValue(true)); 47 | $cache 48 | ->expects($this->once()) 49 | ->method('get') 50 | ->with($this->equalTo('1234')) 51 | ->will($this->returnValue($this->getDefaultPayloadSingle())); 52 | $gistTwigExtension = new GistTwigExtension($transport, $cache); 53 | $this->assertEquals($this->getDefaultPayloadSingleFixture(), $gistTwigExtension->gist(1234), 'Should get valid HTML response'); 54 | } 55 | 56 | public function testGistTransportSingle() 57 | { 58 | $transport = $this->getMock('Dflydev\Twig\Extension\GitHubGist\Transport\TransportInterface'); 59 | $transport 60 | ->expects($this->once()) 61 | ->method('fetchGist') 62 | ->with($this->equalTo('1234')) 63 | ->will($this->returnValue($this->getDefaultPayloadSingle())); 64 | $cache = $this->getMock('Dflydev\Twig\Extension\GitHubGist\Cache\CacheInterface'); 65 | $cache 66 | ->expects($this->once()) 67 | ->method('exists') 68 | ->with($this->equalTo('1234')) 69 | ->will($this->returnValue(false)); 70 | $gistTwigExtension = new GistTwigExtension($transport, $cache); 71 | $this->assertEquals($this->getDefaultPayloadSingleFixture(), $gistTwigExtension->gist(1234), 'Should get valid HTML response'); 72 | } 73 | 74 | public function testGistCacheMultiple() 75 | { 76 | $transport = $this->getMock('Dflydev\Twig\Extension\GitHubGist\Transport\TransportInterface'); 77 | $cache = $this->getMock('Dflydev\Twig\Extension\GitHubGist\Cache\CacheInterface'); 78 | $cache 79 | ->expects($this->once()) 80 | ->method('exists') 81 | ->with($this->equalTo('1234')) 82 | ->will($this->returnValue(true)); 83 | $cache 84 | ->expects($this->once()) 85 | ->method('get') 86 | ->with($this->equalTo('1234')) 87 | ->will($this->returnValue($this->getDefaultPayloadMultiple())); 88 | $gistTwigExtension = new GistTwigExtension($transport, $cache); 89 | $this->assertEquals($this->getDefaultPayloadMultipleFixture(), $gistTwigExtension->gist(1234), 'Should get valid HTML response'); 90 | } 91 | 92 | public function testGistCacheMultipleWithFileSpecified() 93 | { 94 | $transport = $this->getMock('Dflydev\Twig\Extension\GitHubGist\Transport\TransportInterface'); 95 | $cache = $this->getMock('Dflydev\Twig\Extension\GitHubGist\Cache\CacheInterface'); 96 | $cache 97 | ->expects($this->once()) 98 | ->method('exists') 99 | ->with($this->equalTo('1234')) 100 | ->will($this->returnValue(true)); 101 | $cache 102 | ->expects($this->once()) 103 | ->method('get') 104 | ->with($this->equalTo('1234')) 105 | ->will($this->returnValue($this->getDefaultPayloadMultiple())); 106 | $gistTwigExtension = new GistTwigExtension($transport, $cache); 107 | $this->assertEquals($this->getDefaultPayloadWithFileSpecifiedFixture(), $gistTwigExtension->gist(1234, 'simple.txt'), 'Should get valid HTML response'); 108 | } 109 | 110 | public function testGistCacheEmpty() 111 | { 112 | $transport = $this->getMock('Dflydev\Twig\Extension\GitHubGist\Transport\TransportInterface'); 113 | $cache = $this->getMock('Dflydev\Twig\Extension\GitHubGist\Cache\CacheInterface'); 114 | $cache 115 | ->expects($this->once()) 116 | ->method('exists') 117 | ->with($this->equalTo('1234')) 118 | ->will($this->returnValue(true)); 119 | $cache 120 | ->expects($this->once()) 121 | ->method('get') 122 | ->with($this->equalTo('1234')) 123 | ->will($this->returnValue($this->getEmptyPayload())); 124 | $gistTwigExtension = new GistTwigExtension($transport, $cache); 125 | $this->assertEquals('', $gistTwigExtension->gist(1234), 'Should get empty HTML response'); 126 | } 127 | 128 | public function testDefaultCache() 129 | { 130 | $gistTwigExtension = new GistTwigExtension; 131 | $this->assertTrue($gistTwigExtension->cache() instanceof ArrayCache); 132 | } 133 | 134 | public function testDefaultTransport() 135 | { 136 | $gistTwigExtension = new GistTwigExtension; 137 | $this->assertTrue($gistTwigExtension->transport() instanceof NativePhpTransport); 138 | } 139 | 140 | public function testSetCacheAndTransport() 141 | { 142 | $transport = $this->getMock('Dflydev\Twig\Extension\GitHubGist\Transport\TransportInterface'); 143 | $transport 144 | ->expects($this->once()) 145 | ->method('fetchGist') 146 | ->with($this->equalTo('1234')) 147 | ->will($this->returnValue($this->getDefaultPayloadSingle())); 148 | $cache = $this->getMock('Dflydev\Twig\Extension\GitHubGist\Cache\CacheInterface'); 149 | $cache 150 | ->expects($this->once()) 151 | ->method('exists') 152 | ->with($this->equalTo('1234')) 153 | ->will($this->returnValue(false)); 154 | 155 | $gistTwigExtension = new GistTwigExtension; 156 | $gistTwigExtension->setTransport($transport); 157 | $gistTwigExtension->setCache($cache); 158 | $this->assertEquals($this->getDefaultPayloadSingleFixture(), $gistTwigExtension->gist(1234), 'Should get valid HTML response'); 159 | } 160 | 161 | protected function getDefaultPayloadSingleFixture() 162 | { 163 | return << 165 | EOT 166 | ; 167 | } 168 | 169 | protected function getDefaultPayloadSingle() 170 | { 171 | return array( 172 | 'files' => array( 173 | 'simple.txt' => array( 174 | 'language' => 'text', 175 | 'content' => 'Plain text', 176 | ), 177 | ), 178 | ); 179 | } 180 | 181 | protected function getDefaultPayloadMultipleFixture() 182 | { 183 | return << 185 | EOT 186 | ; 187 | } 188 | 189 | protected function getDefaultPayloadMultiple() 190 | { 191 | return array( 192 | 'files' => array( 193 | 'simple.txt' => array( 194 | 'language' => 'text', 195 | 'content' => 'Plain text', 196 | ), 197 | 'sample.php' => array( 198 | 'language' => 'php', 199 | 'content' => '', 200 | ), 201 | ), 202 | ); 203 | } 204 | 205 | protected function getDefaultPayloadWithFileSpecifiedFixture() 206 | { 207 | return << 209 | EOT 210 | ; 211 | } 212 | 213 | protected function getEmptyPayload() 214 | { 215 | return array( 216 | 'files' => array(), 217 | ); 218 | } 219 | } 220 | --------------------------------------------------------------------------------