';
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 <<