/src/rpc/tests/ContextTest.php

https://github.com/hyperf/hyperf · PHP · 73 lines · 50 code · 11 blank · 12 comment · 0 complexity · 2e91123e9fba2308c4ce5f84b6b1728f MD5 · raw file

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace HyperfTest\Rpc;
  12. use Hyperf\Rpc\Context;
  13. use Hyperf\Utils\Str;
  14. use PHPUnit\Framework\TestCase;
  15. /**
  16. * @internal
  17. * @coversNothing
  18. */
  19. class ContextTest extends TestCase
  20. {
  21. protected function tearDown()
  22. {
  23. (new Context())->clear();
  24. }
  25. public function testSetDataAndGetData()
  26. {
  27. $context = new Context();
  28. $context->setData([
  29. 'id' => $id = uniqid(),
  30. 'name' => $name = Str::random(8),
  31. ]);
  32. $context2 = new Context();
  33. $this->assertSame([
  34. 'id' => $id,
  35. 'name' => $name,
  36. ], $context->getData());
  37. $this->assertSame($context->getData(), $context2->getData());
  38. parallel([function () use ($context) {
  39. $context2 = new Context();
  40. $this->assertSame([], $context->getData());
  41. $this->assertSame([], $context2->getData());
  42. }]);
  43. }
  44. public function testSetAndGet()
  45. {
  46. $context = new Context();
  47. $context->setData([
  48. 'id' => $id = uniqid(),
  49. 'name' => $name = Str::random(8),
  50. ]);
  51. $context->set('gender', $gender = rand(0, 1));
  52. $this->assertSame([
  53. 'id' => $id,
  54. 'name' => $name,
  55. 'gender' => $gender,
  56. ], $context->getData());
  57. $this->assertSame($gender, $context->get('gender'));
  58. parallel([function () use ($context) {
  59. $this->assertSame(null, $context->get('gender'));
  60. }]);
  61. }
  62. }