PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/Smak/Portfolio/Set.php

https://github.com/eexit/Smak
PHP | 362 lines | 247 code | 73 blank | 42 comment | 2 complexity | 21ef45f4dda2b3d318d2f8af12ec51c6 MD5 | raw file
  1. <?php
  2. namespace Smak\Portfolio\tests\units;
  3. use Smak\Portfolio;
  4. use Smak\Portfolio\SortHelper;
  5. use Symfony\Component\Finder\Adapter;
  6. use tests\units\Smak\Portfolio\Fs;
  7. require_once __DIR__ . '/../../../../vendor/autoload.php';
  8. class Set extends Fs\FsAdapter
  9. {
  10. public function setUp()
  11. {
  12. $this->buildFs();
  13. }
  14. public function buildFs()
  15. {
  16. $fs = new Fs\FsBuilder($this->fsTreeProvider());
  17. $fs->setDiffTime(true)->build();
  18. return $fs;
  19. }
  20. public function buildSet(Adapter\AdapterInterface $adapter, \SplFileInfo $set_root = null)
  21. {
  22. $fs = $this->buildFs();
  23. $tree = $fs->getTree();
  24. if (null == $set_root) {
  25. $set_root = new \SplFileInfo($fs->getRoot()
  26. . DIRECTORY_SEPARATOR
  27. . 'Travels'
  28. . DIRECTORY_SEPARATOR
  29. . 'Chile');
  30. }
  31. $set = new \Smak\Portfolio\Set($set_root);
  32. $set->removeAdapters()->addAdapter($adapter);
  33. return $set;
  34. }
  35. public function beforeTestMethod($method)
  36. {
  37. foreach ($this->getValidAdapters() as $adapter) {
  38. $set = $this->buildSet($adapter);
  39. $this->string($set->name)
  40. ->isEqualTo('Chile');
  41. }
  42. }
  43. /**
  44. * @dataProvider getAdaptersTestData
  45. */
  46. public function testClassDeclaration(Adapter\AdapterInterface $adapter)
  47. {
  48. $set = $this->buildSet($adapter);
  49. $this->object($set)
  50. ->isInstanceOf('\Symfony\Component\Finder\Finder');
  51. $this->object($set)
  52. ->isInstanceOf('\Smak\Portfolio\Portfolio');
  53. $this->class('\Smak\Portfolio\Set')
  54. ->hasInterface('\Countable');
  55. }
  56. /**
  57. * @dataProvider getAdaptersTestData
  58. */
  59. public function testCount(Adapter\AdapterInterface $adapter)
  60. {
  61. $set = $this->buildSet($adapter);
  62. $this->integer($set->count())->isEqualTo(4);
  63. }
  64. /**
  65. * @dataProvider getAdaptersTestData
  66. */
  67. public function testExtensions(Adapter\AdapterInterface $adapter)
  68. {
  69. $set = $this->buildSet($adapter);
  70. $this->array($set->getExtensions())
  71. ->isEqualTo(array('.jpg', '.jpeg', '.jpf', '.png'));
  72. $this->object($set->setExtensions(
  73. $new_ext = array('.tiff', '.gif')
  74. ))->isInstanceOf('\Smak\Portfolio\Set');
  75. $this->array($set->getExtensions())
  76. ->isEqualTo($new_ext);
  77. $unserialized_set = unserialize(serialize($set));
  78. $this->array($unserialized_set->getExtensions())
  79. ->isEqualTo($new_ext);
  80. $this->exception(function() use ($set) {
  81. $set->setExtensions(array());
  82. })->isInstanceOf('\InvalidArgumentException');
  83. }
  84. /**
  85. * @dataProvider getAdaptersTestData
  86. */
  87. public function testTemplateExtension(Adapter\AdapterInterface $adapter)
  88. {
  89. $set = $this->buildSet($adapter);
  90. $this->array($set->getTemplateExtensions())
  91. ->isEqualTo(array('.html.twig'));
  92. $this->object($set->setTemplateExtensions(
  93. $new_ext = array('.html', '.txt')
  94. ))->isInstanceOf('\Smak\Portfolio\Set');
  95. $this->array($set->getTemplateExtensions())
  96. ->isEqualTo($new_ext);
  97. $unserialized_set = unserialize(serialize($set));
  98. $this->array($unserialized_set->getTemplateExtensions())
  99. ->isEqualTo($new_ext);
  100. $this->exception(function() use ($set) {
  101. $set->setTemplateExtensions(array());
  102. })->isInstanceOf('\InvalidArgumentException');
  103. }
  104. /**
  105. * @dataProvider getAdaptersTestData
  106. */
  107. public function testGetAll(Adapter\AdapterInterface $adapter)
  108. {
  109. $set = $this->buildSet($adapter);
  110. $this->object($set->getAll())
  111. ->isInstanceOf('\ArrayIterator');
  112. }
  113. /**
  114. * @dataProvider getAdaptersTestData
  115. */
  116. public function testGetById(Adapter\AdapterInterface $adapter)
  117. {
  118. $set = $this->buildSet($adapter);
  119. $tree = $this->buildFs()->getTree();
  120. $tree = $tree['Travels']['Chile'];
  121. array_pop($tree);
  122. sort($tree);
  123. $this->exception(function() use ($set) {
  124. $set->getById("foo");
  125. })->isInstanceOf('\InvalidArgumentException');
  126. $this->string($set->getById(2)->getFilename())
  127. ->isEqualTo($tree[2]);
  128. $this->exception(function() use ($set) {
  129. $set->getById(123);
  130. })->isInstanceOf('\OutOfRangeException');
  131. }
  132. /**
  133. * @dataProvider getAdaptersTestData
  134. */
  135. public function testGetByName(Adapter\AdapterInterface $adapter)
  136. {
  137. $set = $this->buildSet($adapter);
  138. $tree = $this->buildFs()->getTree();
  139. $tree = $tree['Travels']['Chile'];
  140. array_pop($tree);
  141. sort($tree);
  142. $this->exception(function() use ($set) {
  143. $set->getByName(23.34);
  144. })->isInstanceOf('\InvalidArgumentException');
  145. $this->string($set->getByName('sample-4')->getFilename())
  146. ->isEqualTo($tree[3]);
  147. $this->variable($set->getByName('foobar'))
  148. ->isNull();
  149. }
  150. /**
  151. * @dataProvider getAdaptersTestData
  152. */
  153. public function testGetTemplate(Adapter\AdapterInterface $adapter)
  154. {
  155. $set = $this->buildSet($adapter);
  156. $this->object($set->getTemplate())
  157. ->isInstanceOf('\SplFileInfo');
  158. $this->string($set->getTemplate()->getFilename())
  159. ->isEqualTo('Chile.html.twig');
  160. }
  161. /**
  162. * @dataProvider getAdaptersTestData
  163. */
  164. public function testGetSetSplInfo(Adapter\AdapterInterface $adapter)
  165. {
  166. $set = $this->buildSet($adapter);
  167. $set_root = $this->buildFs()->getRoot()
  168. . DIRECTORY_SEPARATOR
  169. . 'Travels'
  170. . DIRECTORY_SEPARATOR
  171. . 'Chile';
  172. $this->object($set->getSplInfo())
  173. ->isInstanceOf('\SplFileInfo');
  174. $this->string($set->getSplInfo()->getFilename())
  175. ->isEqualTo('Chile');
  176. $this->string($set->getSplInfo()->getRealPath())
  177. ->isEqualTo($set_root);
  178. }
  179. /**
  180. * @dataProvider getAdaptersTestData
  181. */
  182. public function testGetFirst(Adapter\AdapterInterface $adapter)
  183. {
  184. $set = $this->buildSet($adapter);
  185. $first = $set->getFirst();
  186. $tree = $this->buildFs()->getTree();
  187. $expected = array_shift($tree['Travels']['Chile']);
  188. $this->object($first)
  189. ->isInstanceOf('\Smak\Portfolio\Photo');
  190. $this->string($first->getFilename())
  191. ->isEqualTo($expected);
  192. }
  193. /**
  194. * @dataProvider getAdaptersTestData
  195. */
  196. public function testGetLast(Adapter\AdapterInterface $adapter)
  197. {
  198. $set = $this->buildSet($adapter);
  199. $last = $set->getLast();
  200. $tree = $this->buildFs()->getTree();
  201. array_pop($tree['Travels']['Chile']); // Removes the twig file from Tree
  202. $expected = array_pop($tree['Travels']['Chile']);
  203. $this->object($last)
  204. ->isInstanceOf('\Smak\Portfolio\Photo');
  205. $this->string($last->getFilename())
  206. ->isEqualTo($expected);
  207. }
  208. /**
  209. * @dataProvider getAdaptersTestData
  210. */
  211. public function testGetInReversedOrder(Adapter\AdapterInterface $adapter)
  212. {
  213. $set = $this->buildSet($adapter);
  214. $tree = $this->buildFs()->getTree();
  215. $expected = $tree['Travels']['Chile'];
  216. array_pop($expected);
  217. sort($expected);
  218. foreach ($set->sort(SortHelper::reverseName())->getAll() as $file) {
  219. $results[] = $file->getFilename();
  220. }
  221. $this->array(array_reverse($expected))->isEqualTo($results);
  222. }
  223. /**
  224. * @dataProvider getAdaptersTestData
  225. */
  226. public function testGetSetAsCollection(Adapter\AdapterInterface $adapter)
  227. {
  228. $fs = $this->buildFs();
  229. $tree = $fs->getTree();
  230. $set_root = new \SplFileInfo($fs->getRoot() . DIRECTORY_SEPARATOR . 'Travels');
  231. $set = $this->buildSet($adapter, $set_root);
  232. $expected = array_keys($tree['Travels']);
  233. $this->object($collection = $set->asCollection())
  234. ->isInstanceOf('\Smak\Portfolio\Collection');
  235. $this->object($collection)
  236. ->isInstanceOf('\Symfony\Component\Finder\Finder');
  237. foreach ($collection->getAll() as $set) {
  238. $results[] = $set->name;
  239. }
  240. $this->array($expected)->isEqualTo($results);
  241. }
  242. /**
  243. * @dataProvider getAdaptersTestData
  244. */
  245. public function testSerialization(Adapter\AdapterInterface $adapter)
  246. {
  247. $set = $this->buildSet($adapter);
  248. $helpers = array(
  249. 'foo' => 'bar',
  250. 'bar' => 'baz',
  251. 'time' => time()
  252. );
  253. $set_root = $set->getSplInfo()->getRealPath();
  254. $set_name = $set->name;
  255. foreach ($helpers as $key => $value) {
  256. $set->$key = $value;
  257. }
  258. $unserialized_instance = unserialize(serialize($set));
  259. $this->string($unserialized_instance->name)
  260. ->isEqualTo($set_name);
  261. $this->string($unserialized_instance->getSplInfo()->getRealPath())
  262. ->isEqualTo($set_root);
  263. $this->integer($unserialized_instance->count())
  264. ->isEqualTo(4);
  265. unset($unserialized_instance->foo);
  266. array_shift($helpers);
  267. foreach ($helpers as $key => $value) {
  268. $this->variable($unserialized_instance->$key)
  269. ->isNotNull();
  270. $this->variable($unserialized_instance->$key)
  271. ->isEqualTo($value);
  272. }
  273. }
  274. public function tearDown()
  275. {
  276. $this->buildFs()->clear();
  277. }
  278. protected function fsTreeProvider()
  279. {
  280. return array('Travels' => array(
  281. 'Chile' => array(
  282. 'sample-1.jpeg',
  283. 'sample-3.jpG',
  284. 'sample-2.jpg',
  285. 'sample-4.png',
  286. 'Chile.html.twig'
  287. )));
  288. }
  289. }