/tests/tests.php

https://github.com/pyrsmk/Chernozem · PHP · 515 lines · 413 code · 92 blank · 10 comment · 2 complexity · 96d5aebba88f47bf4f1c96c066c370cb MD5 · raw file

  1. <?php
  2. use Symfony\Component\ClassLoader\Psr4ClassLoader;
  3. ########################################################### Prepare
  4. error_reporting(E_ALL);
  5. require __DIR__.'/vendor/autoload.php';
  6. require __DIR__.'/../vendor/autoload.php';
  7. $loader = new Psr4ClassLoader;
  8. $loader->addPrefix('Chernozem\\', '../src');
  9. $loader->register();
  10. ########################################################### Basics
  11. $suite = new MiniSuite\Suite('Basics');
  12. $suite->hydrate(function($suite) {
  13. $suite['chernozem'] = new Chernozem\Container();
  14. });
  15. $suite->expects('set()/get()')
  16. ->that(function($suite) {
  17. $suite['chernozem']->set('foo', 'bar');
  18. return $suite['chernozem']->get('foo');
  19. })
  20. ->equals('bar');
  21. $suite->expects('set() : with no key')
  22. ->that(function($suite) {
  23. $suite['chernozem'][] = 72;
  24. return $suite['chernozem']->get(0);
  25. })
  26. ->equals(72);
  27. $suite->expects('set()/get() : object as key')
  28. ->that(function($suite) {
  29. $key = new Stdclass();
  30. $suite['chernozem']->set($key, 'bahamut');
  31. return $suite['chernozem']->get($key);
  32. })
  33. ->equals('bahamut');
  34. $suite->expects('get() : not found')
  35. ->that($suite->protect(function($suite) {
  36. $suite['chernozem']->get('bar');
  37. }))
  38. ->throws('Chernozem\NotFoundException');
  39. $suite->expects('has() : exists')
  40. ->that(function($suite) {
  41. $suite['chernozem']->set('foo', 'bar');
  42. return $suite['chernozem']->has('foo');
  43. })
  44. ->equals(true);
  45. $suite->expects('has() : does not exist')
  46. ->that(function($suite) {
  47. return $suite['chernozem']->has('bar');
  48. })
  49. ->equals(false);
  50. $suite->expects('remove()')
  51. ->that(function($suite) {
  52. $suite['chernozem']->set('foo', 'bar');
  53. $suite['chernozem']->remove('foo');
  54. return $suite['chernozem']->has('foo');
  55. })
  56. ->equals(false);
  57. $suite->expects('remove() : not found')
  58. ->that($suite->protect(function($suite) {
  59. $suite['chernozem']->remove('foo');
  60. }))
  61. ->throws('Chernozem\NotFoundException');
  62. $suite->expects('set/get : ArrayAccess')
  63. ->that(function($suite) {
  64. $suite['chernozem']['final'] = 'fantasy';
  65. return $suite['chernozem']['final'];
  66. })
  67. ->equals('fantasy');
  68. $suite->expects('has : ArrayAccess')
  69. ->that(function($suite) {
  70. $suite['chernozem']['final'] = 'fantasy';
  71. return isset($suite['chernozem']['final']);
  72. })
  73. ->equals(true);
  74. $suite->expects('remove : ArrayAccess')
  75. ->that(function($suite) {
  76. $suite['chernozem']['final'] = 'fantasy';
  77. unset($suite['chernozem']['final']);
  78. return isset($suite['chernozem']['final']);
  79. })
  80. ->equals(false);
  81. $suite->hydrate(function($suite) {
  82. $suite['fruits'] = [
  83. 'banana' => 'yellow',
  84. 'strawberry' => 'red',
  85. 'lemon' => 'green',
  86. 'blood_orange' => 'red'
  87. ];
  88. $suite['chernozem'] = new Chernozem\Container($suite['fruits']);
  89. });
  90. $suite->expects('constructor/toArray()')
  91. ->that(function($suite) {
  92. return $suite['chernozem']->toArray();
  93. })
  94. ->equals($suite['fruits']);
  95. $suite->expects('clear()')
  96. ->that(function($suite) {
  97. $suite['chernozem']->clear();
  98. return $suite['chernozem']->toArray();
  99. })
  100. ->equals([]);
  101. $suite->expects('set/get : methods')
  102. ->that(function($suite) {
  103. return $suite['chernozem']->getBloodOrange();
  104. })
  105. ->equals('red');
  106. ########################################################### Loops
  107. $suite = new MiniSuite\Suite('Loops');
  108. $suite->hydrate(function($suite) {
  109. $suite['fruits'] = [
  110. 'banana' => 'yellow',
  111. 'strawberry' => 'red',
  112. 'lemon' => 'green',
  113. 'blood orange' => 'red'
  114. ];
  115. $suite['chernozem'] = new Chernozem\Container($suite['fruits']);
  116. });
  117. $suite->expects('foreach()')
  118. ->that(function($suite) {
  119. $values = [];
  120. foreach($suite['chernozem'] as $key => $value) {
  121. $values[$key] = $value;
  122. }
  123. return $values;
  124. })
  125. ->equals($suite['fruits']);
  126. $suite->expects('count()')
  127. ->that(count($suite['chernozem']))
  128. ->equals(4);
  129. ########################################################### Inflectors
  130. $suite = new MiniSuite\Suite('Inflectors');
  131. $suite->hydrate(function($suite) {
  132. $suite['value'] = $suite->service(function($suite) {
  133. return new Chernozem\Value('hello');
  134. });
  135. $suite['value']->addInputInflector(function($value) {
  136. if(strlen($value) > 8) {
  137. throw new Exception();
  138. }
  139. return $value;
  140. });
  141. $suite['value']->addOutputInflector(function($value) {
  142. return strtoupper($value);
  143. });
  144. });
  145. $suite->expects('getValue()/addOutputInflector()')
  146. ->that($suite['value']->getValue())
  147. ->equals('HELLO');
  148. $suite->expects('getRawValue()')
  149. ->that($suite['value']->getRawValue())
  150. ->equals('hello');
  151. $suite->expects('setValue()/addInputInflector() : invalid')
  152. ->that($suite->protect(function($suite) {
  153. $suite['value']->setValue('wonderful beach with great sunshine');
  154. }))
  155. ->throws();
  156. $suite->expects('setValue()/addInputInflector() : valid')
  157. ->that(function($suite) {
  158. $suite['value']->setValue('bip');
  159. return $suite['value']->getValue();
  160. })
  161. ->equals('BIP');
  162. $suite->hydrate(function($suite) {
  163. $suite['fruits'] = [
  164. 'banana' => 'yellow',
  165. 'strawberry' => 'red',
  166. 'lemon' => 'green',
  167. 'blood orange' => 'red'
  168. ];
  169. $suite['chernozem'] = new Chernozem\Container($suite['fruits']);
  170. $suite['chernozem']->setter('banana', function($value) {
  171. if(strlen($value) > 8) {
  172. throw new Exception();
  173. }
  174. return $value;
  175. });
  176. $suite['chernozem']->getter('banana', function($value) {
  177. return strtoupper($value);
  178. });
  179. });
  180. $suite->expects('setter()')
  181. ->that($suite->protect(function($suite) {
  182. $suite['chernozem']['banana'] = 'wonderful beach with great sunshine';
  183. }))
  184. ->throws();
  185. $suite->expects('getter()')
  186. ->that($suite['chernozem']['banana'])
  187. ->equals('YELLOW');
  188. ########################################################### Factory closures
  189. $suite = new MiniSuite\Suite('Factory closures');
  190. class Factory {
  191. protected $a = 0;
  192. public function get() { return ++$this->a; }
  193. }
  194. $suite->hydrate(function($suite) {
  195. $suite['chernozem'] = new Chernozem\Container();
  196. $suite['chernozem']['factory'] = $suite['chernozem']->factory(function($chernozem) use($suite) {
  197. $suite->expects('factory() : passed container')
  198. ->that($chernozem)
  199. ->isInstanceOf('Chernozem\Container');
  200. return new Factory();
  201. });
  202. });
  203. $suite->expects('factory() : first get')
  204. ->that($suite['chernozem']['factory']->get())
  205. ->equals(1);
  206. $suite->expects('factory() : second get')
  207. ->that($suite['chernozem']['factory']->get())
  208. ->equals(1);
  209. $suite->hydrate(function(){});
  210. $suite['chernozem'] = new Chernozem\Container();
  211. $suite['chernozem']['service'] = $suite['chernozem']->service(function($chernozem) use($suite) {
  212. $suite->expects('service() : passed container')
  213. ->that($chernozem)
  214. ->isInstanceOf('Chernozem\Container');
  215. return new Factory();
  216. });
  217. $suite->expects('service() : first get')
  218. ->that($suite['chernozem']['service']->get())
  219. ->equals(1);
  220. $suite->expects('service() : second get')
  221. ->that($suite['chernozem']['service']->get())
  222. ->equals(2);
  223. ########################################################### Type hinting
  224. $suite = new MiniSuite\Suite('Type hinting');
  225. $suite->hydrate(function($suite) {
  226. $suite['chernozem'] = new Chernozem\Container([
  227. 'int' => null,
  228. 'integer' => null,
  229. 'float' => null,
  230. 'double' => null,
  231. 'bool' => null,
  232. 'boolean' => null,
  233. 'string' => null,
  234. 'array' => null,
  235. 'class' => null,
  236. 'resource' => null,
  237. ]);
  238. $suite['chernozem']->hint('int', 'int');
  239. $suite['chernozem']->hint('integer', 'integer');
  240. $suite['chernozem']->hint('float', 'float');
  241. $suite['chernozem']->hint('double', 'double');
  242. $suite['chernozem']->hint('bool', 'bool');
  243. $suite['chernozem']->hint('boolean', 'boolean');
  244. $suite['chernozem']->hint('string', 'string');
  245. $suite['chernozem']->hint('array', 'array');
  246. $suite['chernozem']->hint('class', 'Factory');
  247. $suite['chernozem']->hint('resource', 'resource');
  248. });
  249. $suite->expects('int : fail')
  250. ->that($suite->protect(function($suite) {
  251. $suite['chernozem']['int'] = 1.72;
  252. }))
  253. ->throws('Chernozem\ContainerException');
  254. $suite->expects('int : pass')
  255. ->that($suite->protect(function($suite) {
  256. $suite['chernozem']['int'] = 72;
  257. }))
  258. ->doesNotThrow('Chernozem\ContainerException');
  259. $suite->expects('integer : fail')
  260. ->that($suite->protect(function($suite) {
  261. $suite['chernozem']['integer'] = 1.72;
  262. }))
  263. ->throws('Chernozem\ContainerException');
  264. $suite->expects('integer : pass')
  265. ->that($suite->protect(function($suite) {
  266. $suite['chernozem']['integer'] = 72;
  267. }))
  268. ->doesNotThrow('Chernozem\ContainerException');
  269. $suite->expects('float : fail')
  270. ->that($suite->protect(function($suite) {
  271. $suite['chernozem']['float'] = 72;
  272. }))
  273. ->throws('Chernozem\ContainerException');
  274. $suite->expects('float : pass')
  275. ->that($suite->protect(function($suite) {
  276. $suite['chernozem']['float'] = 1.72;
  277. }))
  278. ->doesNotThrow('Chernozem\ContainerException');
  279. $suite->expects('double : fail')
  280. ->that($suite->protect(function($suite) {
  281. $suite['chernozem']['float'] = 72;
  282. }))
  283. ->throws('Chernozem\ContainerException');
  284. $suite->expects('double : pass')
  285. ->that($suite->protect(function($suite) {
  286. $suite['chernozem']['float'] = 1.72;
  287. }))
  288. ->doesNotThrow('Chernozem\ContainerException');
  289. $suite->expects('bool : fail')
  290. ->that($suite->protect(function($suite) {
  291. $suite['chernozem']['bool'] = 72;
  292. }))
  293. ->throws('Chernozem\ContainerException');
  294. $suite->expects('bool : pass')
  295. ->that($suite->protect(function($suite) {
  296. $suite['chernozem']['bool'] = true;
  297. }))
  298. ->doesNotThrow('Chernozem\ContainerException');
  299. $suite->expects('boolean : fail')
  300. ->that($suite->protect(function($suite) {
  301. $suite['chernozem']['boolean'] = 72;
  302. }))
  303. ->throws('Chernozem\ContainerException');
  304. $suite->expects('boolean : pass')
  305. ->that($suite->protect(function($suite) {
  306. $suite['chernozem']['boolean'] = true;
  307. }))
  308. ->doesNotThrow('Chernozem\ContainerException');
  309. $suite->expects('string : fail')
  310. ->that($suite->protect(function($suite) {
  311. $suite['chernozem']['string'] = 72;
  312. }))
  313. ->throws('Chernozem\ContainerException');
  314. $suite->expects('string : pass')
  315. ->that($suite->protect(function($suite) {
  316. $suite['chernozem']['string'] = 'string';
  317. }))
  318. ->doesNotThrow('Chernozem\ContainerException');
  319. $suite->expects('array : fail')
  320. ->that($suite->protect(function($suite) {
  321. $suite['chernozem']['array'] = 72;
  322. }))
  323. ->throws('Chernozem\ContainerException');
  324. $suite->expects('array : pass')
  325. ->that($suite->protect(function($suite) {
  326. $suite['chernozem']['array'] = [];
  327. }))
  328. ->doesNotThrow('Chernozem\ContainerException');
  329. $suite->expects('class : fail')
  330. ->that($suite->protect(function($suite) {
  331. $suite['chernozem']['class'] = 72;
  332. }))
  333. ->throws('Chernozem\ContainerException');
  334. $suite->expects('class : pass')
  335. ->that($suite->protect(function($suite) {
  336. $suite['chernozem']['class'] = new Factory();
  337. }))
  338. ->doesNotThrow('Chernozem\ContainerException');
  339. $suite->expects('unsupported type')
  340. ->that($suite->protect(function($suite) {
  341. $suite['chernozem']['resource'] = 72;
  342. }))
  343. ->throws('Chernozem\ContainerException');
  344. ########################################################### Read only values
  345. $suite = new MiniSuite\Suite('Read only values');
  346. $suite->hydrate(function($suite) {
  347. $suite['chernozem'] = new Chernozem\Container(['foo' => 'bar']);
  348. });
  349. $suite->expects('readonly()')
  350. ->that($suite->protect(function($suite) {
  351. $suite['chernozem']->readonly('foo');
  352. $suite['chernozem']['foo'] = 'test';
  353. }))
  354. ->throws('Chernozem\ContainerException');
  355. ########################################################### Service providers
  356. $suite = new MiniSuite\Suite('Service providers');
  357. class MyService implements Chernozem\ServiceProviderInterface {
  358. public function register(Interop\Container\ContainerInterface $container) {
  359. $container['foo'] = 'bar';
  360. }
  361. }
  362. $suite->hydrate(function($suite) {
  363. $suite['chernozem'] = new Chernozem\Container();
  364. });
  365. $suite->expects('register()')
  366. ->that(function($suite) {
  367. $suite['chernozem']->register(new MyService);
  368. return $suite['chernozem']['foo'];
  369. })
  370. ->equals('bar');
  371. ########################################################### Delegate container
  372. $suite = new MiniSuite\Suite('Delegate container');
  373. $suite->hydrate(function($suite) {
  374. $suite['chernozem'] = new Chernozem\Container();
  375. $delegate = new Chernozem\Container();
  376. $delegate['foo'] = 'bar';
  377. $suite['chernozem']->delegate($delegate);
  378. });
  379. $suite->expects('factory() : does not throw')
  380. ->that($suite->protect(function($suite) {
  381. $suite['chernozem']['factory'] = $suite['chernozem']->factory(function($container) use($suite) {
  382. $suite->expects('factory() : value')
  383. ->that($container['foo'])
  384. ->equals('bar');
  385. });
  386. return $suite['chernozem']['factory'];
  387. }))
  388. ->doesNotThrow();
  389. $suite->expects('service() : does not throw')
  390. ->that($suite->protect(function($suite) {
  391. $suite['chernozem']['service'] = $suite['chernozem']->service(function($container) use($suite) {
  392. $suite->expects('service() : value')
  393. ->that($container['foo'])
  394. ->equals('bar');
  395. });
  396. return $suite['chernozem']['service'];
  397. }))
  398. ->doesNotThrow();
  399. ########################################################### Composite container
  400. $suite = new MiniSuite\Suite('Composite container');
  401. $suite->hydrate(function($suite) {
  402. $c1 = new Chernozem\Container();
  403. $c1['bar'] = 'foo';
  404. $c2 = new Chernozem\Container();
  405. $c2['foo'] = 'bar';
  406. $suite['composite'] = new Chernozem\Composite([$c1]);
  407. $suite['composite']->add($c2);
  408. });
  409. $suite->expects('has() : first container')
  410. ->that($suite['composite']->has('bar'))
  411. ->equals(true);
  412. $suite->expects('has() : second container')
  413. ->that($suite['composite']->has('foo'))
  414. ->equals(true);
  415. $suite->expects('has() : not found')
  416. ->that($suite['composite']->has('pwet'))
  417. ->equals(false);
  418. $suite->expects('get() : first container')
  419. ->that($suite['composite']->get('bar'))
  420. ->equals('foo');
  421. $suite->expects('get() : second container')
  422. ->that($suite['composite']->get('foo'))
  423. ->equals('bar');