RedisTest.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. <?php
  2. use Codeception\Lib\ModuleContainer;
  3. use Codeception\Module\Redis;
  4. use Codeception\Test\Unit;
  5. class RedisTest extends Unit
  6. {
  7. /**
  8. * @var array
  9. */
  10. protected static $config = [
  11. 'database' => 15
  12. ];
  13. /**
  14. * @var Redis
  15. */
  16. protected $module;
  17. /**
  18. * Keys that will be created for the tests
  19. *
  20. * @var array
  21. */
  22. protected static $keys = [
  23. 'string' => [
  24. 'name' => 'test:string',
  25. 'value' => 'hello'
  26. ],
  27. 'list' => [
  28. 'name' => 'test:list',
  29. 'value' => ['riri', 'fifi', 'loulou']
  30. ],
  31. 'set' => [
  32. 'name' => 'test:set',
  33. 'value' => ['huey', 'dewey', 'louie']
  34. ],
  35. 'zset' => [
  36. 'name' => 'test:zset',
  37. 'value' => ['juanito' => 1, 'jorgito' => 2, 'jaimito' => 3]
  38. ],
  39. 'hash' => [
  40. 'name' => 'test:hash',
  41. 'value' => ['Tick' => true, 'Trick' => 'dewey', 'Track' => 42]
  42. ]
  43. ];
  44. /**
  45. * {@inheritdoc}
  46. *
  47. * Every time a test starts, cleanup the database and populates it with some
  48. * dummy data.
  49. */
  50. protected function setUp()
  51. {
  52. if (!class_exists('Predis\Client')) {
  53. $this->markTestSkipped('Predis is not installed');
  54. }
  55. /** @var ModuleContainer $container */
  56. $container = make_container();
  57. try {
  58. $this->module = new Redis($container);
  59. $this->module->_setConfig(self::$config);
  60. $this->module->_initialize();
  61. $this->module->driver->flushDb();
  62. } catch (Predis\Connection\ConnectionException $e) {
  63. $this->markTestSkipped($e->getMessage());
  64. }
  65. $addMethods = [
  66. 'string' => 'set',
  67. 'list' => 'rPush',
  68. 'set' => 'sAdd',
  69. 'zset' => 'zAdd',
  70. 'hash' => 'hMSet'
  71. ];
  72. foreach (self::$keys as $type => $key) {
  73. $this->module->driver->{$addMethods[$type]}(
  74. $key['name'],
  75. $key['value']
  76. );
  77. }
  78. }
  79. /**
  80. * Indicates that the next test is expected to fail
  81. * @param null $exceptionClass The fully qualified class name of the
  82. * expected exception
  83. */
  84. protected function shouldFail($exceptionClass = null)
  85. {
  86. if (!$exceptionClass) {
  87. $exceptionClass = 'PHPUnit\Framework\AssertionFailedError';
  88. }
  89. $this->setExpectedException($exceptionClass);
  90. }
  91. // ****************************************
  92. // Test grabFromRedis() with non existing keys
  93. // ****************************************
  94. public function testGrabFromRedisNonExistingKey()
  95. {
  96. $this->shouldFail('\Codeception\Exception\ModuleException');
  97. $this->module->grabFromRedis('doesnotexist');
  98. }
  99. // *******************************
  100. // Test grabFromRedis() with Strings
  101. // *******************************
  102. public function testGrabFromRedisString()
  103. {
  104. $result = $this->module->grabFromRedis(self::$keys['string']['name']);
  105. $this->assertSame(
  106. self::$keys['string']['value'],
  107. $result
  108. );
  109. }
  110. // *******************************
  111. // Test grabFromRedis() with Lists
  112. // *******************************
  113. public function testGrabFromRedisListMember()
  114. {
  115. $index = 2;
  116. $result = $this->module->grabFromRedis(
  117. self::$keys['list']['name'],
  118. $index
  119. );
  120. $this->assertSame(
  121. self::$keys['list']['value'][$index],
  122. $result
  123. );
  124. }
  125. public function testGrabFromRedisListRange()
  126. {
  127. $rangeFrom = 1;
  128. $rangeTo = 2;
  129. $result = $this->module->grabFromRedis(
  130. self::$keys['list']['name'],
  131. $rangeFrom,
  132. $rangeTo
  133. );
  134. $this->assertSame(
  135. array_slice(
  136. self::$keys['list']['value'],
  137. $rangeFrom,
  138. $rangeTo - $rangeFrom + 1
  139. ),
  140. $result
  141. );
  142. }
  143. // *******************************
  144. // Test grabFromRedis() with Sets
  145. // *******************************
  146. public function testGrabFromRedisSet()
  147. {
  148. $result = $this->module->grabFromRedis(
  149. self::$keys['set']['name']
  150. );
  151. sort($result);
  152. $reference = self::$keys['set']['value'];
  153. sort($reference);
  154. $this->assertSame($reference, $result);
  155. }
  156. // *******************************
  157. // Test grabFromRedis() with Sorted Sets
  158. // *******************************
  159. public function testGrabFromRedisZSetWithTwoArguments()
  160. {
  161. $this->shouldFail('\Codeception\Exception\ModuleException');
  162. $this->module->grabFromRedis(
  163. self::$keys['zset']['name'],
  164. 1
  165. );
  166. }
  167. public function testGrabFromRedisZSetAll()
  168. {
  169. $expected = self::$keys['zset']['value'];
  170. $result = $this->module->grabFromRedis(self::$keys['zset']['name']);
  171. $this->assertSame(
  172. $this->scoresToFloat($expected),
  173. $this->scoresToFloat($result)
  174. );
  175. }
  176. public function testGrabFromRedisZSetRange()
  177. {
  178. $rangeFrom = 1;
  179. $rangeTo = 2;
  180. $expected = array_slice(
  181. self::$keys['zset']['value'],
  182. $rangeFrom,
  183. ($rangeTo - $rangeFrom + 1)
  184. );
  185. $result = $this->module->grabFromRedis(
  186. self::$keys['zset']['name'],
  187. $rangeFrom,
  188. $rangeTo
  189. );
  190. $this->assertSame(
  191. $this->scoresToFloat($expected),
  192. $this->scoresToFloat($result)
  193. );
  194. }
  195. // *******************************
  196. // Test grabFromRedis() with Hashes
  197. // *******************************
  198. public function testGrabFromRedisHashAll()
  199. {
  200. $result = $this->module->grabFromRedis(
  201. self::$keys['hash']['name']
  202. );
  203. $this->assertEquals(
  204. $this->boolToString(self::$keys['hash']['value']),
  205. $result
  206. );
  207. }
  208. public function testGrabFromRedisHashField()
  209. {
  210. $field = 'Trick';
  211. $result = $this->module->grabFromRedis(
  212. self::$keys['hash']['name'],
  213. $field
  214. );
  215. $this->assertSame(
  216. self::$keys['hash']['value'][$field],
  217. $result
  218. );
  219. }
  220. // *******************************
  221. // Test haveInRedis() with Strings
  222. // *******************************
  223. public function testHaveInRedisNonExistingString()
  224. {
  225. $newKey = [
  226. 'name' => 'test:string-create',
  227. 'value' => 'testing string creation'
  228. ];
  229. $this->module->haveInRedis(
  230. 'string',
  231. $newKey['name'],
  232. $newKey['value']
  233. );
  234. $this->assertSame(
  235. $newKey['value'],
  236. $this->module->driver->get($newKey['name'])
  237. );
  238. }
  239. public function testHaveInRedisExistingString()
  240. {
  241. $newValue = 'new value';
  242. $this->module->haveInRedis(
  243. 'string',
  244. self::$keys['string']['name'],
  245. $newValue
  246. );
  247. $this->assertSame(
  248. $newValue,
  249. $this->module->driver->get(self::$keys['string']['name'])
  250. );
  251. }
  252. // *******************************
  253. // Test haveInRedis() with Lists
  254. // *******************************
  255. public function testHaveInRedisNonExistingListArrayArg()
  256. {
  257. $newKey = [
  258. 'name' => 'test:list-create-array',
  259. 'value' => ['testing', 'list', 'creation']
  260. ];
  261. $this->module->haveInRedis(
  262. 'list',
  263. $newKey['name'],
  264. $newKey['value']
  265. );
  266. $this->assertSame(
  267. $newKey['value'],
  268. $this->module->driver->lrange($newKey['name'], 0, -1)
  269. );
  270. }
  271. public function testHaveInRedisNonExistingListScalarArg()
  272. {
  273. $newKey = [
  274. 'name' => 'test:list-create-scalar',
  275. 'value' => 'testing list creation'
  276. ];
  277. $this->module->haveInRedis(
  278. 'list',
  279. $newKey['name'],
  280. $newKey['value']
  281. );
  282. $this->assertSame(
  283. [$newKey['value']],
  284. $this->module->driver->lrange($newKey['name'], 0, -1)
  285. );
  286. }
  287. public function testHaveInRedisExistingListArrayArg()
  288. {
  289. $newValue = ['testing', 'list', 'creation'];
  290. $this->module->haveInRedis(
  291. 'list',
  292. self::$keys['list']['name'],
  293. $newValue
  294. );
  295. $this->assertSame(
  296. array_merge(
  297. self::$keys['list']['value'],
  298. $newValue
  299. ),
  300. $this->module->driver->lrange(self::$keys['list']['name'], 0, -1)
  301. );
  302. }
  303. public function testHaveInRedisExistingListArrayScalar()
  304. {
  305. $newValue = 'testing list creation';
  306. $this->module->haveInRedis(
  307. 'list',
  308. self::$keys['list']['name'],
  309. $newValue
  310. );
  311. $this->assertSame(
  312. array_merge(
  313. self::$keys['list']['value'],
  314. [$newValue]
  315. ),
  316. $this->module->driver->lrange(self::$keys['list']['name'], 0, -1)
  317. );
  318. }
  319. // *******************************
  320. // Test haveInRedis() with Sets
  321. // *******************************
  322. public function testHaveInRedisNonExistingSetArrayArg()
  323. {
  324. $newKey = [
  325. 'name' => 'test:set-create-array',
  326. 'value' => ['testing', 'set', 'creation']
  327. ];
  328. $this->module->haveInRedis(
  329. 'set',
  330. $newKey['name'],
  331. $newKey['value']
  332. );
  333. $expected = $newKey['value'];
  334. sort($expected);
  335. $result = $this->module->driver->sMembers($newKey['name']);
  336. sort($result);
  337. $this->assertSame($expected, $result);
  338. }
  339. public function testHaveInRedisNonExistingSetScalarArg()
  340. {
  341. $newKey = [
  342. 'name' => 'test:set-create-scalar',
  343. 'value' => 'testing set creation'
  344. ];
  345. $this->module->haveInRedis(
  346. 'set',
  347. $newKey['name'],
  348. $newKey['value']
  349. );
  350. $this->assertSame(
  351. [$newKey['value']],
  352. $this->module->driver->sMembers($newKey['name'])
  353. );
  354. }
  355. public function testHaveInRedisExistingSetArrayArg()
  356. {
  357. $newValue = ['testing', 'set', 'creation'];
  358. $this->module->haveInRedis(
  359. 'set',
  360. self::$keys['set']['name'],
  361. $newValue
  362. );
  363. $expectedValue = array_merge(
  364. self::$keys['set']['value'],
  365. $newValue
  366. );
  367. sort($expectedValue);
  368. $result = $this->module->driver->sMembers(self::$keys['set']['name']);
  369. sort($result);
  370. $this->assertSame($expectedValue, $result);
  371. }
  372. public function testHaveInRedisExistingSetArrayScalar()
  373. {
  374. $newValue = 'testing set creation';
  375. $this->module->haveInRedis(
  376. 'set',
  377. self::$keys['set']['name'],
  378. $newValue
  379. );
  380. $expectedResult = array_merge(
  381. self::$keys['set']['value'],
  382. [$newValue]
  383. );
  384. sort($expectedResult);
  385. $result = $this->module->driver->sMembers(self::$keys['set']['name']);
  386. sort($result);
  387. $this->assertSame($expectedResult, $result);
  388. }
  389. // *******************************
  390. // Test haveInRedis() with Sorted sets
  391. // *******************************
  392. public function testHaveInRedisZSetScalar()
  393. {
  394. $this->shouldFail('\Codeception\Exception\ModuleException');
  395. $this->module->haveInRedis(
  396. 'zset',
  397. 'test:zset-create-array',
  398. 'foobar'
  399. );
  400. }
  401. public function testHaveInRedisNonExistingZSetArrayArg()
  402. {
  403. $newKey = [
  404. 'name' => 'test:zset-create-array',
  405. 'value' => [
  406. 'testing' => 2,
  407. 'zset' => 1,
  408. 'creation' => 2,
  409. 'foo' => 3
  410. ]
  411. ];
  412. $this->module->haveInRedis(
  413. 'zset',
  414. $newKey['name'],
  415. $newKey['value']
  416. );
  417. $result = $this->module->driver->zrange($newKey['name'], 0, -1, 'WITHSCORES');
  418. $this->assertSame(
  419. ['zset' => 1.0, 'creation' => 2.0, 'testing' => 2.0, 'foo' => 3.0],
  420. $this->scoresToFloat($result)
  421. );
  422. }
  423. public function testHaveInRedisExistingZSetArrayArg()
  424. {
  425. $newValue = [
  426. 'testing' => 2,
  427. 'zset' => 1,
  428. 'creation' => 2,
  429. 'foo' => 3
  430. ];
  431. $this->module->haveInRedis(
  432. 'zset',
  433. self::$keys['zset']['name'],
  434. $newValue
  435. );
  436. $expected = array_merge(
  437. self::$keys['zset']['value'],
  438. $newValue
  439. );
  440. array_multisort(
  441. array_values($expected),
  442. SORT_ASC,
  443. array_keys($expected),
  444. SORT_ASC,
  445. $expected
  446. );
  447. $result = $this->module->driver->zRange(
  448. self::$keys['zset']['name'],
  449. 0,
  450. -1,
  451. 'WITHSCORES'
  452. );
  453. $this->assertSame(
  454. $this->scoresToFloat($expected),
  455. $this->scoresToFloat($result)
  456. );
  457. }
  458. // *******************************
  459. // Test haveInRedis() with Hashes
  460. // *******************************
  461. public function testHaveInRedisHashScalar()
  462. {
  463. $this->shouldFail('\Codeception\Exception\ModuleException');
  464. $this->module->haveInRedis(
  465. 'hash',
  466. 'test:hash-create-array',
  467. 'foobar'
  468. );
  469. }
  470. public function testHaveInRedisNonExistingHashArrayArg()
  471. {
  472. $newKey = [
  473. 'name' => 'test:hash-create-array',
  474. 'value' => [
  475. 'obladi' => 'oblada',
  476. 'nope' => false,
  477. 'zero' => 0
  478. ]
  479. ];
  480. $this->module->haveInRedis(
  481. 'hash',
  482. $newKey['name'],
  483. $this->boolToString($newKey['value'])
  484. );
  485. $this->assertEquals(
  486. $this->boolToString($newKey['value']),
  487. $this->module->driver->hGetAll($newKey['name'])
  488. );
  489. }
  490. public function testHaveInRedisExistingHashArrayArg()
  491. {
  492. $newValue = [
  493. 'obladi' => 'oblada',
  494. 'nope' => false,
  495. 'zero' => 0
  496. ];
  497. $this->module->haveInRedis(
  498. 'hash',
  499. self::$keys['hash']['name'],
  500. $newValue
  501. );
  502. $this->assertEquals(
  503. array_merge(
  504. self::$keys['hash']['value'],
  505. $newValue
  506. ),
  507. $this->module->driver->hGetAll(self::$keys['hash']['name'])
  508. );
  509. }
  510. // ****************************************
  511. // Test dontSeeInRedis() with non existing keys
  512. // ****************************************
  513. public function testDontSeeInRedisNonExistingKeyWithoutValue()
  514. {
  515. $this->module->dontSeeInRedis('doesnotexist');
  516. }
  517. public function testDontSeeInRedisNonExistingKeyWithValue()
  518. {
  519. $this->module->dontSeeInRedis(
  520. 'doesnotexist',
  521. 'some value'
  522. );
  523. }
  524. // *******************************
  525. // Test dontSeeInRedis() without value
  526. // *******************************
  527. public function testDontSeeInRedisExistingKeyWithoutValue()
  528. {
  529. $this->shouldFail();
  530. $this->module->dontSeeInRedis(
  531. self::$keys['string']['name']
  532. );
  533. }
  534. // *******************************
  535. // Test dontSeeInRedis() with Strings
  536. // *******************************
  537. public function testDontSeeInRedisExistingStringWithCorrectValue()
  538. {
  539. $this->shouldFail();
  540. $this->module->dontSeeInRedis(
  541. self::$keys['string']['name'],
  542. self::$keys['string']['value']
  543. );
  544. }
  545. public function testDontSeeInRedisExistingStringWithIncorrectValue()
  546. {
  547. $this->module->dontSeeInRedis(
  548. self::$keys['string']['name'],
  549. 'incorrect value'
  550. );
  551. }
  552. // *******************************
  553. // Test dontSeeInRedis() with Lists
  554. // *******************************
  555. public function testDontSeeInRedisExistingListWithCorrectValue()
  556. {
  557. $this->shouldFail();
  558. $this->module->dontSeeInRedis(
  559. self::$keys['list']['name'],
  560. self::$keys['list']['value']
  561. );
  562. }
  563. public function testDontSeeInRedisExistingListWithCorrectValueDifferentOrder()
  564. {
  565. $this->module->dontSeeInRedis(
  566. self::$keys['list']['name'],
  567. array_reverse(self::$keys['list']['value'])
  568. );
  569. }
  570. public function testDontSeeInRedisExistingListWithIncorrectValue()
  571. {
  572. $this->module->dontSeeInRedis(
  573. self::$keys['list']['name'],
  574. ['incorrect', 'value']
  575. );
  576. }
  577. // *******************************
  578. // Test dontSeeInRedis() with Sets
  579. // *******************************
  580. public function testDontSeeInRedisExistingSetWithCorrectValue()
  581. {
  582. $this->shouldFail();
  583. $this->module->dontSeeInRedis(
  584. self::$keys['set']['name'],
  585. self::$keys['set']['value']
  586. );
  587. }
  588. public function testDontSeeInRedisExistingSetWithCorrectValueDifferentOrder()
  589. {
  590. $this->shouldFail();
  591. $this->module->dontSeeInRedis(
  592. self::$keys['set']['name'],
  593. array_reverse(self::$keys['set']['value'])
  594. );
  595. }
  596. public function testDontSeeInRedisExistingSetWithIncorrectValue()
  597. {
  598. $this->module->dontSeeInRedis(
  599. self::$keys['set']['name'],
  600. ['incorrect', 'value']
  601. );
  602. }
  603. // *******************************
  604. // Test dontSeeInRedis() with Sorted Sets
  605. // *******************************
  606. public function testDontSeeInRedisExistingZSetWithCorrectValue()
  607. {
  608. $this->shouldFail();
  609. $this->module->dontSeeInRedis(
  610. self::$keys['zset']['name'],
  611. self::$keys['zset']['value']
  612. );
  613. }
  614. public function testDontSeeInRedisExistingZSetWithCorrectValueWithoutScores()
  615. {
  616. $this->module->dontSeeInRedis(
  617. self::$keys['zset']['name'],
  618. array_keys(self::$keys['zset']['value'])
  619. );
  620. }
  621. public function testDontSeeInRedisExistingZSetWithCorrectValueDifferentOrder()
  622. {
  623. $this->module->dontSeeInRedis(
  624. self::$keys['zset']['name'],
  625. array_reverse(self::$keys['zset']['value'])
  626. );
  627. }
  628. public function testDontSeeInRedisExistingZSetWithIncorrectValue()
  629. {
  630. $this->module->dontSeeInRedis(
  631. self::$keys['zset']['name'],
  632. ['incorrect' => 1, 'value' => 2]
  633. );
  634. }
  635. // *******************************
  636. // Test dontSeeInRedis() with Hashes
  637. // *******************************
  638. public function testDontSeeInRedisExistingHashWithCorrectValue()
  639. {
  640. $this->shouldFail();
  641. $this->module->dontSeeInRedis(
  642. self::$keys['hash']['name'],
  643. self::$keys['hash']['value']
  644. );
  645. }
  646. public function testDontSeeInRedisExistingHashWithCorrectValueDifferentOrder()
  647. {
  648. $this->shouldFail();
  649. $this->module->dontSeeInRedis(
  650. self::$keys['hash']['name'],
  651. array_reverse(self::$keys['hash']['value'])
  652. );
  653. }
  654. public function testDontSeeInRedisExistingHashWithIncorrectValue()
  655. {
  656. $this->module->dontSeeInRedis(
  657. self::$keys['hash']['name'],
  658. ['incorrect' => 'value']
  659. );
  660. }
  661. // ****************************************
  662. // Test dontSeeRedisKeyContains() with non existing keys
  663. // ****************************************
  664. public function testDontSeeRedisKeyContainsNonExistingKey()
  665. {
  666. $this->shouldFail('\Codeception\Exception\ModuleException');
  667. $this->module->dontSeeRedisKeyContains('doesnotexist', 'doesnotexist');
  668. }
  669. // ****************************************
  670. // Test dontSeeRedisKeyContains() with array args
  671. // ****************************************
  672. public function testDontSeeRedisKeyContainsWithArrayArgs()
  673. {
  674. $this->shouldFail('\Codeception\Exception\ModuleException');
  675. $this->module->dontSeeRedisKeyContains(
  676. self::$keys['hash']['name'],
  677. self::$keys['hash']['value']
  678. );
  679. }
  680. // *******************************
  681. // Test dontSeeRedisKeyContains() with Strings
  682. // *******************************
  683. public function testDontSeeRedisKeyContainsStringWithCorrectSubstring()
  684. {
  685. $this->shouldFail();
  686. $this->module->dontSeeRedisKeyContains(
  687. self::$keys['string']['name'],
  688. substr(self::$keys['string']['value'], 2, -2)
  689. );
  690. }
  691. public function testDontSeeRedisKeyContainsStringWithIncorrectValue()
  692. {
  693. $this->module->dontSeeRedisKeyContains(
  694. self::$keys['string']['name'],
  695. 'incorrect string'
  696. );
  697. }
  698. // *******************************
  699. // Test dontSeeRedisKeyContains() with Lists
  700. // *******************************
  701. public function testDontSeeRedisKeyContainsListWithCorrectItem()
  702. {
  703. $this->shouldFail();
  704. $this->module->dontSeeRedisKeyContains(
  705. self::$keys['list']['name'],
  706. self::$keys['list']['value'][1]
  707. );
  708. }
  709. public function testDontSeeRedisKeyContainsListWithIncorrectItem()
  710. {
  711. $this->module->dontSeeRedisKeyContains(
  712. self::$keys['list']['name'],
  713. 'incorrect'
  714. );
  715. }
  716. // *******************************
  717. // Test dontSeeRedisKeyContains() with Sets
  718. // *******************************
  719. public function testDontSeeRedisKeyContainsSetWithCorrectItem()
  720. {
  721. $this->shouldFail();
  722. $this->module->dontSeeRedisKeyContains(
  723. self::$keys['set']['name'],
  724. self::$keys['set']['value'][1]
  725. );
  726. }
  727. public function testDontSeeRedisKeyContainsSetWithIncorrectItem()
  728. {
  729. $this->module->dontSeeRedisKeyContains(
  730. self::$keys['set']['name'],
  731. 'incorrect'
  732. );
  733. }
  734. // *******************************
  735. // Test dontSeeRedisKeyContains() with Sorted sets
  736. // *******************************
  737. public function testDontSeeRedisKeyContainsZSetWithCorrectItemWithScore()
  738. {
  739. $this->shouldFail();
  740. $firstItem = array_slice(self::$keys['zset']['value'], 0, 1);
  741. $firstMember = key($firstItem);
  742. $this->module->dontSeeRedisKeyContains(
  743. self::$keys['zset']['name'],
  744. $firstMember,
  745. $firstItem[$firstMember]
  746. );
  747. }
  748. public function testDontSeeRedisKeyContainsZSetWithCorrectItemWithIncorrectScore()
  749. {
  750. $firstItem = array_slice(self::$keys['zset']['value'], 0, 1);
  751. $firstKey = key($firstItem);
  752. $this->module->dontSeeRedisKeyContains(
  753. self::$keys['zset']['name'],
  754. $firstKey,
  755. 'incorrect'
  756. );
  757. }
  758. public function testDontSeeRedisKeyContainsZSetWithCorrectItemWithoutScore()
  759. {
  760. $this->shouldFail();
  761. $arrayKeys = array_keys(self::$keys['zset']['value']);
  762. $this->module->dontSeeRedisKeyContains(
  763. self::$keys['zset']['name'],
  764. $arrayKeys[0]
  765. );
  766. }
  767. public function testDontSeeRedisKeyContainsZSetWithIncorrectItemWithoutScore()
  768. {
  769. $this->module->dontSeeRedisKeyContains(
  770. self::$keys['zset']['name'],
  771. 'incorrect'
  772. );
  773. }
  774. public function testDontSeeRedisKeyContainsZSetWithIncorrectItemWithScore()
  775. {
  776. $this->module->dontSeeRedisKeyContains(
  777. self::$keys['zset']['name'],
  778. 'incorrect',
  779. 34
  780. );
  781. }
  782. // *******************************
  783. // Test dontSeeRedisKeyContains() with Hashes
  784. // *******************************
  785. public function testDontSeeRedisKeyContainsHashWithCorrectFieldWithValue()
  786. {
  787. $this->shouldFail();
  788. $firstField = array_slice(self::$keys['hash']['value'], 0, 1);
  789. $firstKey = key($firstField);
  790. $this->module->dontSeeRedisKeyContains(
  791. self::$keys['hash']['name'],
  792. $firstKey,
  793. $firstField[$firstKey]
  794. );
  795. }
  796. public function testDontSeeRedisKeyContainsHashWithCorrectFieldWithIncorrectValue()
  797. {
  798. $firstField = array_slice(self::$keys['hash']['value'], 0, 1);
  799. $firstKey = key($firstField);
  800. $this->module->dontSeeRedisKeyContains(
  801. self::$keys['hash']['name'],
  802. $firstKey,
  803. 'incorrect'
  804. );
  805. }
  806. public function testDontSeeRedisKeyContainsHashWithCorrectFieldWithoutValue()
  807. {
  808. $this->shouldFail();
  809. $arrayKeys = array_keys(self::$keys['hash']['value']);
  810. $this->module->dontSeeRedisKeyContains(
  811. self::$keys['hash']['name'],
  812. $arrayKeys[0]
  813. );
  814. }
  815. public function testDontSeeRedisKeyContainsHashWithIncorrectFieldWithoutValue()
  816. {
  817. $this->module->dontSeeRedisKeyContains(
  818. self::$keys['hash']['name'],
  819. 'incorrect'
  820. );
  821. }
  822. public function testDontSeeRedisKeyContainsHashWithIncorrectFieldWithValue()
  823. {
  824. $this->module->dontSeeRedisKeyContains(
  825. self::$keys['hash']['name'],
  826. 'incorrect',
  827. 34
  828. );
  829. }
  830. // ****************************************
  831. // Test seeInRedis() with non existing keys
  832. // ****************************************
  833. public function testSeeInRedisNonExistingKeyWithoutValue()
  834. {
  835. $this->shouldFail();
  836. $this->module->seeInRedis('doesnotexist');
  837. }
  838. public function testSeeInRedisNonExistingKeyWithValue()
  839. {
  840. $this->shouldFail();
  841. $this->module->seeInRedis(
  842. 'doesnotexist',
  843. 'some value'
  844. );
  845. }
  846. // *******************************
  847. // Test seeInRedis() without value
  848. // *******************************
  849. public function testSeeInRedisExistingKeyWithoutValue()
  850. {
  851. $this->module->seeInRedis(
  852. self::$keys['string']['name']
  853. );
  854. }
  855. // *******************************
  856. // Test seeInRedis() with Strings
  857. // *******************************
  858. public function testSeeInRedisExistingStringWithCorrectValue()
  859. {
  860. $this->module->seeInRedis(
  861. self::$keys['string']['name'],
  862. self::$keys['string']['value']
  863. );
  864. }
  865. public function testSeeInRedisExistingStringWithIncorrectValue()
  866. {
  867. $this->shouldFail();
  868. $this->module->seeInRedis(
  869. self::$keys['string']['name'],
  870. 'incorrect value'
  871. );
  872. }
  873. // *******************************
  874. // Test seeInRedis() with Lists
  875. // *******************************
  876. public function testSeeInRedisExistingListWithCorrectValue()
  877. {
  878. $this->module->seeInRedis(
  879. self::$keys['list']['name'],
  880. self::$keys['list']['value']
  881. );
  882. }
  883. public function testSeeInRedisExistingListWithCorrectValueDifferentOrder()
  884. {
  885. $this->shouldFail();
  886. $this->module->seeInRedis(
  887. self::$keys['list']['name'],
  888. array_reverse(self::$keys['list']['value'])
  889. );
  890. }
  891. public function testSeeInRedisExistingListWithIncorrectValue()
  892. {
  893. $this->shouldFail();
  894. $this->module->seeInRedis(
  895. self::$keys['list']['name'],
  896. ['incorrect', 'value']
  897. );
  898. }
  899. // *******************************
  900. // Test seeInRedis() with Sets
  901. // *******************************
  902. public function testSeeInRedisExistingSetWithCorrectValue()
  903. {
  904. $this->module->seeInRedis(
  905. self::$keys['set']['name'],
  906. self::$keys['set']['value']
  907. );
  908. }
  909. public function testSeeInRedisExistingSetWithCorrectValueDifferentOrder()
  910. {
  911. $this->module->seeInRedis(
  912. self::$keys['set']['name'],
  913. array_reverse(self::$keys['set']['value'])
  914. );
  915. }
  916. public function testSeeInRedisExistingSetWithIncorrectValue()
  917. {
  918. $this->shouldFail();
  919. $this->module->seeInRedis(
  920. self::$keys['set']['name'],
  921. ['incorrect', 'value']
  922. );
  923. }
  924. // *******************************
  925. // Test seeInRedis() with Sorted Sets
  926. // *******************************
  927. public function testSeeInRedisExistingZSetWithCorrectValueWithScores()
  928. {
  929. $this->module->seeInRedis(
  930. self::$keys['zset']['name'],
  931. self::$keys['zset']['value']
  932. );
  933. }
  934. public function testSeeInRedisExistingZSetWithCorrectValueWithoutScores()
  935. {
  936. $this->shouldFail();
  937. $this->module->seeInRedis(
  938. self::$keys['zset']['name'],
  939. array_keys(self::$keys['zset']['value'])
  940. );
  941. }
  942. public function testSeeInRedisExistingZSetWithCorrectValueDifferentOrder()
  943. {
  944. $this->shouldFail();
  945. $this->module->seeInRedis(
  946. self::$keys['zset']['name'],
  947. array_reverse(self::$keys['zset']['value'])
  948. );
  949. }
  950. public function testSeeInRedisExistingZSetWithIncorrectValue()
  951. {
  952. $this->shouldFail();
  953. $this->module->seeInRedis(
  954. self::$keys['zset']['name'],
  955. ['incorrect' => 1, 'value' => 2]
  956. );
  957. }
  958. // *******************************
  959. // Test seeInRedis() with Hashes
  960. // *******************************
  961. public function testSeeInRedisExistingHashWithCorrectValue()
  962. {
  963. $this->module->seeInRedis(
  964. self::$keys['hash']['name'],
  965. self::$keys['hash']['value']
  966. );
  967. }
  968. public function testSeeInRedisExistingHashWithCorrectValueDifferentOrder()
  969. {
  970. $this->module->seeInRedis(
  971. self::$keys['hash']['name'],
  972. array_reverse(self::$keys['hash']['value'])
  973. );
  974. }
  975. public function testSeeInRedisExistingHashWithIncorrectValue()
  976. {
  977. $this->shouldFail();
  978. $this->module->seeInRedis(
  979. self::$keys['hash']['name'],
  980. ['incorrect' => 'value']
  981. );
  982. }
  983. // ****************************************
  984. // Test seeRedisKeyContains() with non existing keys
  985. // ****************************************
  986. public function testSeeRedisKeyContainsNonExistingKey()
  987. {
  988. $this->shouldFail('\Codeception\Exception\ModuleException');
  989. $this->module->seeRedisKeyContains('doesnotexist', 'doesnotexist');
  990. }
  991. // ****************************************
  992. // Test dontSeeRedisKeyContains() with array args
  993. // ****************************************
  994. public function testSeeRedisKeyContainsWithArrayArgs()
  995. {
  996. $this->shouldFail('\Codeception\Exception\ModuleException');
  997. $this->module->dontSeeRedisKeyContains(
  998. self::$keys['hash']['name'],
  999. self::$keys['hash']['value']
  1000. );
  1001. }
  1002. // *******************************
  1003. // Test seeRedisKeyContains() with Strings
  1004. // *******************************
  1005. public function testSeeRedisKeyContainsStringWithCorrectSubstring()
  1006. {
  1007. $this->module->seeRedisKeyContains(
  1008. self::$keys['string']['name'],
  1009. substr(self::$keys['string']['value'], 2, -2)
  1010. );
  1011. }
  1012. public function testSeeRedisKeyContainsStringWithIncorrectValue()
  1013. {
  1014. $this->shouldFail();
  1015. $this->module->seeRedisKeyContains(
  1016. self::$keys['string']['name'],
  1017. 'incorrect string'
  1018. );
  1019. }
  1020. // *******************************
  1021. // Test seeRedisKeyContains() with Lists
  1022. // *******************************
  1023. public function testSeeRedisKeyContainsListWithCorrectItem()
  1024. {
  1025. $this->module->seeRedisKeyContains(
  1026. self::$keys['list']['name'],
  1027. self::$keys['list']['value'][1]
  1028. );
  1029. }
  1030. public function testSeeRedisKeyContainsListWithIncorrectItem()
  1031. {
  1032. $this->shouldFail();
  1033. $this->module->seeRedisKeyContains(
  1034. self::$keys['list']['name'],
  1035. 'incorrect'
  1036. );
  1037. }
  1038. // *******************************
  1039. // Test seeRedisKeyContains() with Sets
  1040. // *******************************
  1041. public function testSeeRedisKeyContainsSetWithCorrectItem()
  1042. {
  1043. $this->module->seeRedisKeyContains(
  1044. self::$keys['set']['name'],
  1045. self::$keys['set']['value'][1]
  1046. );
  1047. }
  1048. public function testSeeRedisKeyContainsSetWithIncorrectItem()
  1049. {
  1050. $this->shouldFail();
  1051. $this->module->seeRedisKeyContains(
  1052. self::$keys['set']['name'],
  1053. 'incorrect'
  1054. );
  1055. }
  1056. // *******************************
  1057. // Test seeRedisKeyContains() with Sorted sets
  1058. // *******************************
  1059. public function testSeeRedisKeyContainsZSetWithCorrectItemWithScore()
  1060. {
  1061. $firstItem = array_slice(self::$keys['zset']['value'], 0, 1);
  1062. $firstKey = key($firstItem);
  1063. $this->module->seeRedisKeyContains(
  1064. self::$keys['zset']['name'],
  1065. $firstKey,
  1066. $firstItem[$firstKey]
  1067. );
  1068. }
  1069. public function testSeeRedisKeyContainsZSetWithCorrectItemWithIncorrectScore()
  1070. {
  1071. $this->shouldFail();
  1072. $firstItem = array_slice(self::$keys['zset']['value'], 0, 1);
  1073. $firstKey = key($firstItem);
  1074. $this->module->seeRedisKeyContains(
  1075. self::$keys['zset']['name'],
  1076. $firstKey,
  1077. 'incorrect'
  1078. );
  1079. }
  1080. public function testSeeRedisKeyContainsZSetWithCorrectItemWithoutScore()
  1081. {
  1082. $arrayKeys = array_keys(self::$keys['zset']['value']);
  1083. $this->module->seeRedisKeyContains(
  1084. self::$keys['zset']['name'],
  1085. $arrayKeys[0]
  1086. );
  1087. }
  1088. public function testSeeRedisKeyContainsZSetWithIncorrectItemWithoutScore()
  1089. {
  1090. $this->shouldFail();
  1091. $this->module->seeRedisKeyContains(
  1092. self::$keys['zset']['name'],
  1093. 'incorrect'
  1094. );
  1095. }
  1096. public function testSeeRedisKeyContainsZSetWithIncorrectItemWithScore()
  1097. {
  1098. $this->shouldFail();
  1099. $this->module->seeRedisKeyContains(
  1100. self::$keys['zset']['name'],
  1101. 'incorrect',
  1102. 34
  1103. );
  1104. }
  1105. // *******************************
  1106. // Test seeRedisKeyContains() with Hashes
  1107. // *******************************
  1108. public function testSeeRedisKeyContainsHashWithCorrectFieldWithValue()
  1109. {
  1110. $firstField = array_slice(self::$keys['hash']['value'], 0, 1);
  1111. $firstKey = key($firstField);
  1112. $this->module->seeRedisKeyContains(
  1113. self::$keys['hash']['name'],
  1114. $firstKey,
  1115. $firstField[$firstKey]
  1116. );
  1117. }
  1118. public function testSeeRedisKeyContainsHashWithCorrectFieldWithIncorrectValue()
  1119. {
  1120. $this->shouldFail();
  1121. $firstField = array_slice(self::$keys['hash']['value'], 0, 1);
  1122. $firstKey = key($firstField);
  1123. $this->module->seeRedisKeyContains(
  1124. self::$keys['hash']['name'],
  1125. $firstKey,
  1126. 'incorrect'
  1127. );
  1128. }
  1129. public function testSeeRedisKeyContainsHashWithCorrectFieldWithoutValue()
  1130. {
  1131. $arrayKeys = array_keys(self::$keys['hash']['value']);
  1132. $this->module->seeRedisKeyContains(
  1133. self::$keys['hash']['name'],
  1134. $arrayKeys[0]
  1135. );
  1136. }
  1137. public function testSeeRedisKeyContainsHashWithIncorrectFieldWithoutValue()
  1138. {
  1139. $this->shouldFail();
  1140. $this->module->seeRedisKeyContains(
  1141. self::$keys['hash']['name'],
  1142. 'incorrect'
  1143. );
  1144. }
  1145. public function testSeeRedisKeyContainsHashWithIncorrectFieldWithValue()
  1146. {
  1147. $this->shouldFail();
  1148. $this->module->seeRedisKeyContains(
  1149. self::$keys['hash']['name'],
  1150. 'incorrect',
  1151. 34
  1152. );
  1153. }
  1154. // *******************************
  1155. // Test sendCommandToRedis()
  1156. // *******************************
  1157. public function testSendCommandToRedis()
  1158. {
  1159. $this->module->sendCommandToRedis('hmset', 'myhash', 'f1', 4, 'f2', 'foo');
  1160. $this->module->sendCommandToRedis('hincrby', 'myhash', 'f1', 8);
  1161. $this->module->sendCommandToRedis('hDel', 'myhash', 'f2');
  1162. $result = $this->module->sendCommandToRedis('hGetAll', 'myhash');
  1163. $this->assertEquals(
  1164. ['f1' => 12],
  1165. $result
  1166. );
  1167. }
  1168. // *******************************
  1169. // Helper methods
  1170. // *******************************
  1171. /**
  1172. * Explicitely cast the scores of a Zset associative array as float/double
  1173. *
  1174. * @param array $arr The ZSet associative array
  1175. *
  1176. * @return array
  1177. */
  1178. private function scoresToFloat(array $arr)
  1179. {
  1180. foreach ($arr as $member => $score) {
  1181. $arr[$member] = (float) $score;
  1182. }
  1183. return $arr;
  1184. }
  1185. /**
  1186. * Converts boolean values to "0" and "1"
  1187. *
  1188. * @param mixed $var The variable
  1189. *
  1190. * @return mixed
  1191. */
  1192. private function boolToString($var)
  1193. {
  1194. $copy = is_array($var) ? $var : [$var];
  1195. foreach ($copy as $key => $value) {
  1196. if (is_bool($value)) {
  1197. $copy[$key] = $value ? '1' : '0';
  1198. }
  1199. }
  1200. return is_array($var) ? $copy : $copy[0];
  1201. }
  1202. }