ObjectTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace UCloud\Tests;
  3. use PHPUnit\Framework\TestCase;
  4. use UCloud\Core\Exception\UCloudException;
  5. use UCloud\Core\Logger\DefaultLogger;
  6. use UCloud\Core\Request\Request;
  7. use UCloud\Core\Response\Response;
  8. class ObjectTest extends TestCase
  9. {
  10. public function testRequest()
  11. {
  12. $args = [
  13. "Name" => "foo",
  14. "CPU" => 1,
  15. "Size" => 42.0,
  16. "UHostIds" => ["uhost-xxx", "uhost-yyy"],
  17. "Led" => [
  18. "Enabled" => true,
  19. ],
  20. "NetworkInterface" => [
  21. [
  22. "Bandwidth" => 42,
  23. ],
  24. ]
  25. ];
  26. $req = new Request($args);
  27. try {
  28. $req->validate();
  29. } catch (UCloudException $e) {
  30. $this->assertEmpty($e, "must no exception at here");
  31. }
  32. $req->markRequired("Region");
  33. $req->markRequired("Name");
  34. try {
  35. $req->validate();
  36. } catch (UCloudException $e) {
  37. $this->assertNotEmpty($e);
  38. }
  39. $this->assertEquals($args, $req->getAll());
  40. $this->assertEquals([
  41. "Name" => "foo",
  42. "CPU" => 1,
  43. "Size" => 42.0,
  44. "UHostIds.0" => "uhost-xxx",
  45. "UHostIds.1" => "uhost-yyy",
  46. "Led.Enabled" => true,
  47. "NetworkInterface.0.Bandwidth" => 42,
  48. ], $req->toArray());
  49. $req->set("foo", "bar");
  50. $this->assertEquals("bar", $req->get("foo"));
  51. }
  52. public function testResponse() {
  53. $resp = new Response([]);
  54. $resp->set("foo", 42);
  55. $this->assertEquals(42, $resp->get("foo"));
  56. }
  57. public function testLogger()
  58. {
  59. $logger = new DefaultLogger();
  60. $logger->info("foo");
  61. $logger->info("bar", ["foo" => "bar"]);
  62. $this->assertNotEmpty($logger);
  63. }
  64. }