PageRenderTime 52ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/mthenw/nosqlite/test/NoSQLite/Tests/StoreTest.php

https://github.com/vladotesanovic/todofix
PHP | 222 lines | 124 code | 26 blank | 72 comment | 1 complexity | 75e23c254d5e9368982c3932ddd28f71 MD5 | raw file
  1. <?php
  2. namespace NoSQLite\Tests;
  3. use PHPUnit_Framework_TestCase;
  4. use NoSQLite\NoSQLite;
  5. use NoSQLite\Store;
  6. class StoreTest extends \PHPUnit_Framework_TestCase
  7. {
  8. const DB_FILE = 'storeTest.db';
  9. /**
  10. * @var NoSQLite
  11. */
  12. protected $nsl;
  13. /**
  14. * @var Store
  15. */
  16. protected $store;
  17. /**
  18. * @return null
  19. */
  20. public function setUp()
  21. {
  22. $this->nsl = new NoSQLite(self::DB_FILE);
  23. $this->store = $this->nsl->getStore('test');
  24. }
  25. /**
  26. * @return null
  27. */
  28. public function testFirstGet()
  29. {
  30. $key = uniqid();
  31. $value = 'value';
  32. $this->store->set($key, $value);
  33. $this->setUp();
  34. $this->assertEquals($this->store->get($key), $value);
  35. }
  36. /**
  37. * @return null
  38. */
  39. public function testGetAll()
  40. {
  41. $data = array(
  42. '_1' => 'value1',
  43. '_2' => 'value2'
  44. );
  45. $this->store->deleteAll();
  46. foreach ($data as $key => $value) {
  47. $this->store->set($key, $value);
  48. }
  49. $this->assertEquals($data, $this->store->getAll());
  50. }
  51. /**
  52. * @param string $key key
  53. * @param string $value value
  54. *
  55. * @dataProvider validData
  56. * @return null
  57. */
  58. public function testSetGetValue($key, $value)
  59. {
  60. $this->store->set($key, $value);
  61. $this->assertEquals($this->store->get($key), $value);
  62. }
  63. /**
  64. * @return null
  65. */
  66. public function testUpdateValue()
  67. {
  68. $key = uniqid();
  69. $value1 = uniqid();
  70. $value2 = uniqid();
  71. $this->store->set($key, $value1);
  72. $this->store->set($key, $value2);
  73. $this->assertEquals($value2, $this->store->get($key));
  74. }
  75. /**
  76. * Test updating earlier set value from a new store without cache
  77. *
  78. * @return void
  79. */
  80. public function testSetExisting()
  81. {
  82. $key = uniqid();
  83. $value1 = uniqid();
  84. $value2 = uniqid();
  85. $this->store->set($key, $value1);
  86. $newStore = $this->nsl->getStore('test');
  87. $newStore->set($key, $value2);
  88. $this->assertEquals($value2, $newStore->get($key));
  89. }
  90. /**
  91. * @param string $key key
  92. * @param string $value value
  93. *
  94. * @dataProvider invalidSetData
  95. * @expectedException \InvalidArgumentException
  96. * @return null
  97. */
  98. public function testSetExceptions($key, $value)
  99. {
  100. $this->store->set($key, $value);
  101. $this->store->get($key);
  102. }
  103. /**
  104. * @param string $key key
  105. *
  106. * @dataProvider invalidGetData
  107. * @expectedException \InvalidArgumentException
  108. * @return null
  109. */
  110. public function testGetExceptions($key)
  111. {
  112. $this->store->get($key);
  113. }
  114. /**
  115. * @return null
  116. */
  117. public function testDelete()
  118. {
  119. $key = uniqid();
  120. $this->store->set($key, 'value');
  121. $this->store->delete($key);
  122. $this->assertEquals(null, $this->store->get($key));
  123. }
  124. /**
  125. * @return null
  126. */
  127. public function testDeleteAll()
  128. {
  129. $this->store->set(uniqid(), 'value');
  130. $this->store->deleteAll();
  131. $this->assertEquals(array(), $this->store->getAll());
  132. }
  133. /**
  134. * @return null
  135. */
  136. public function testCount()
  137. {
  138. $count = rand(1, 100);
  139. for ($i = 0; $i < $count; $i++) {
  140. $this->store->set(uniqid(), uniqid());
  141. }
  142. $this->assertEquals($count, count($this->store));
  143. }
  144. /**
  145. * @return null
  146. */
  147. public function testIteration()
  148. {
  149. $this->store->set('key1', 'value1');
  150. foreach ($this->store as $key => $value) {
  151. $this->assertSame($key, 'key1');
  152. $this->assertSame($value, 'value1');
  153. }
  154. }
  155. /**
  156. * @static
  157. * @return array
  158. */
  159. public static function validData()
  160. {
  161. return array(
  162. array('key', 'value'),
  163. array('0', 'value'),
  164. );
  165. }
  166. /**
  167. * @static
  168. * @return array
  169. */
  170. public static function invalidSetData()
  171. {
  172. return array(
  173. array(0, 'value'),
  174. array('key', 1),
  175. );
  176. }
  177. /**
  178. * @static
  179. * @return array
  180. */
  181. public static function invalidGetData()
  182. {
  183. return array(
  184. array(10),
  185. );
  186. }
  187. /**
  188. * @return null
  189. */
  190. public function tearDown()
  191. {
  192. unset($this->nsl);
  193. unset($this->store);
  194. unlink(self::DB_FILE);
  195. }
  196. }