QueryBuilderTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use Limen\Redisun\QueryBuilder;
  4. /**
  5. * Class QueryBuilderTest
  6. *
  7. * @author LI Mengxiang <limengxiang876@gmail.com>
  8. */
  9. class QueryBuilderTest extends TestCase
  10. {
  11. public function testBuilder()
  12. {
  13. $key = 'school:{schoolId}:class:{classId}:students';
  14. $builder = new QueryBuilder($key);
  15. $builder->setFieldNeedle('schoolId', '{schoolId}');
  16. $builder->setFieldNeedle('classId', '{classId}');
  17. $keys = $builder->whereEqual('schoolId', 1)->whereEqual('classId', 2)->getQueryKeys();
  18. $this->assertEquals($keys, [
  19. 'school:1:class:2:students',
  20. ]);
  21. $key = $builder->refresh()->whereEqual('schoolId', 1)->whereEqual('classId', 2)->firstQueryKey();
  22. $this->assertEquals($key, 'school:1:class:2:students');
  23. $keys = $builder->refresh()->whereIn('schoolId', [1,2])->getQueryKeys();
  24. $this->assertEquals($keys, [
  25. 'school:1:class:{classId}:students',
  26. 'school:2:class:{classId}:students',
  27. ]);
  28. $keys = $builder->refresh()->whereBetween('schoolId', [1,5])->getQueryKeys();
  29. $this->assertEquals($keys, [
  30. 'school:1:class:{classId}:students',
  31. 'school:2:class:{classId}:students',
  32. 'school:3:class:{classId}:students',
  33. 'school:4:class:{classId}:students',
  34. 'school:5:class:{classId}:students',
  35. ]);
  36. $keys = $builder->refresh()->whereIn('schoolId', [1,2])->whereIn('classId', [2,3])->getQueryKeys();
  37. $this->assertEquals($keys, [
  38. 'school:1:class:2:students',
  39. 'school:1:class:3:students',
  40. 'school:2:class:2:students',
  41. 'school:2:class:3:students',
  42. ]);
  43. $keys = $builder->refresh()->getQueryKeys();
  44. $this->assertEquals($keys, []);
  45. }
  46. }