RedisTest.php 37 KB

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