Skip to content

Commit

Permalink
Merge pull request #25 from unikorp/feat/document-to-query-string
Browse files Browse the repository at this point in the history
feat(document): convert to query string
  • Loading branch information
VEBERArnaud committed Aug 14, 2017
2 parents d85cb75 + b5956e5 commit 21c7d76
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 12 deletions.
26 changes: 23 additions & 3 deletions src/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,35 @@ public function getCreatedAt(): int
*/
public function toJson(): string
{
$document = [];
return json_encode($this->toRequestParameters());
}

/**
* to query string
*
* @return string
*/
public function toQueryString(): string
{
return http_build_query($this->toRequestParameters());
}

/**
* to request parameters
*
* @return array
*/
protected function toRequestParameters(): array
{
$requestParameters = [];

foreach (array_merge($this->getFields(), self::DEFAULT_FIELDS) as $field) {
if (!is_null($value = $this->$field)) {
$document[$this->toSnakeCase($field)] = $value;
$requestParameters[$this->toSnakeCase($field)] = $value;
}
}

return json_encode($document);
return $requestParameters;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Document/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,28 +148,28 @@ protected function getFields(): array
}

/**
* to json
* to request parameters
*
* @return string
* @return array
*/
public function toJson(): string
public function toRequestParameters(): array
{
$document = [];
$requestParameters = [];

foreach (array_merge($this->getFields(), self::DEFAULT_FIELDS) as $field) {
if (!is_null($value = $this->$field)) {
if (is_array($value)) {
foreach (array_keys($value) as $key) {
$document[$this->toSnakeCase(sprintf('%1$s.%2$s', $field, $key))] = $value[$key];
$requestParameters[$this->toSnakeCase(sprintf('%1$s.%2$s', $field, $key))] = $value[$key];
}

continue;
}

$document[$this->toSnakeCase($field)] = $this->$field;
$requestParameters[$this->toSnakeCase($field)] = $this->$field;
}
}

return json_encode($document);
return $requestParameters;
}
}
7 changes: 7 additions & 0 deletions src/DocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ interface DocumentInterface
* @return string
*/
public function toJson(): string;

/**
* to query string
*
* @return string
*/
public function toQueryString(): string;
}
35 changes: 35 additions & 0 deletions tests/Unit/Document/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ public function testGetCreatedAt()
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toJson
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Api::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
Expand All @@ -573,4 +574,38 @@ public function testToJson()
$this->document->toJson()
);
}

/**
* test to query string
*
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toQueryString
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Api::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
public function testToQueryString()
{
$this->document
->setName('name')
->setHosts('hosts')
->setUris('uris')
->setMethods('methods')
->setUpstreamUrl('upstreamUrl')
->setStripUri(false)
->setPreserveHost(true)
->setRetries(10)
->setUpstreamConnectTimeout(10000)
->setUpstreamSendTimeout(10000)
->setUpstreamReadTimeout(10000)
->setHttpsOnly(true)
->setHttpIfTerminated(false)
->setCreatedAt(42);

$this->assertSame(
'name=name&hosts=hosts&uris=uris&methods=methods&upstream_url=upstreamUrl&strip_uri=0&preserve_host=1&retries=10&upstream_connect_timeout=10000&upstream_send_timeout=10000&upstream_read_timeout=10000&https_only=1&http_if_terminated=0&created_at=42',
$this->document->toQueryString()
);
}
}
25 changes: 25 additions & 0 deletions tests/Unit/Document/CertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public function testGetCreatedAt()
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toJson
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Certificate::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
Expand All @@ -213,4 +214,28 @@ public function testToJson()
$this->document->toJson()
);
}

/**
* test to query string
*
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toQueryString
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Certificate::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
public function testToQueryString()
{
$this->document
->setCert('cert')
->setKey('key')
->setSnis('snis')
->setCreatedAt(42);

$this->assertSame(
'cert=cert&key=key&snis=snis&created_at=42',
$this->document->toQueryString()
);
}
}
21 changes: 21 additions & 0 deletions tests/Unit/Document/ClusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public function testGetCreatedAt()
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toJson
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Cluster::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
Expand All @@ -174,4 +175,24 @@ public function testToJson()

$this->assertSame('{"name":"name","address":"address","created_at":42}', $this->document->toJson());
}

/**
* test to query string
*
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toQueryString
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Cluster::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
public function testToQueryString()
{
$this->document
->setName('name')
->setAddress('address')
->setCreatedAt(42);

$this->assertSame('name=name&address=address&created_at=42', $this->document->toQueryString());
}
}
22 changes: 21 additions & 1 deletion tests/Unit/Document/ConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public function testGetCreatedAt()
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toJson
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Consumer::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
Expand All @@ -178,7 +179,26 @@ public function testToJson()
->setUsername('username')
->setCustomId('customId')
->setCreatedAt(42);

$this->assertSame('{"username":"username","custom_id":"customId","created_at":42}', $this->document->toJson());
}

/**
* test to query string
*
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toQueryString
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Consumer::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
public function testToQueryString()
{
$this->document
->setUsername('username')
->setCustomId('customId')
->setCreatedAt(42);

$this->assertSame('username=username&custom_id=customId&created_at=42', $this->document->toQueryString());
}
}
17 changes: 17 additions & 0 deletions tests/Unit/Document/InformationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function testGetCreatedAt()
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toJson
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Information::getFields
*/
public function testToJson()
Expand All @@ -100,4 +101,20 @@ public function testToJson()

$this->assertSame('{"created_at":42}', $this->document->toJson());
}

/**
* test to query string
*
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toQueryString
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Information::getFields
*/
public function testToQueryString()
{
$this->document->setCreatedAt(42);

$this->assertSame('created_at=42', $this->document->toQueryString());
}
}
28 changes: 27 additions & 1 deletion tests/Unit/Document/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ public function testGetCreatedAt()
*
* @return void
*
* @covers \Unikorp\KongAdminApi\Document\Plugin::toJson
* @covers \Unikorp\KongAdminApi\AbstractDocument::toJson
* @covers \Unikorp\KongAdminApi\Document\Plugin::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Plugin::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
Expand All @@ -264,4 +265,29 @@ public function testToJson()
$this->document->toJson()
);
}

/**
* test to query string
*
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toQueryString
* @covers \Unikorp\KongAdminApi\Document\Plugin::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Plugin::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
public function testToQueryString()
{
$this->document
->setName('name')
->setConsumerId('consumerId')
->addConfig('test', true)
->addConfig('something_else', 'something_else')
->setCreatedAt(42);

$this->assertSame(
'name=name&consumer_id=consumerId&config.test=1&config.something_else=something_else&created_at=42',
$this->document->toQueryString()
);
}
}
24 changes: 24 additions & 0 deletions tests/Unit/Document/SniTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public function testGetCreatedAt()
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toJson
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Sni::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
Expand All @@ -177,4 +178,27 @@ public function testToJson()
$this->document->toJson()
);
}

/**
* test to query string
*
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toQueryString
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Sni::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
public function testToQueryString()
{
$this->document
->setName('name')
->setSslCertificateId('sslCertificateId')
->setCreatedAt(42);

$this->assertSame(
'name=name&ssl_certificate_id=sslCertificateId&created_at=42',
$this->document->toQueryString()
);
}
}
24 changes: 24 additions & 0 deletions tests/Unit/Document/TargetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public function testGetCreatedAt()
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toJson
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Target::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
Expand All @@ -177,4 +178,27 @@ public function testToJson()
$this->document->toJson()
);
}

/**
* test to query string
*
* @return void
*
* @covers \Unikorp\KongAdminApi\AbstractDocument::toQueryString
* @covers \Unikorp\KongAdminApi\AbstractDocument::toRequestParameters
* @covers \Unikorp\KongAdminApi\Document\Target::getFields
* @covers \Unikorp\KongAdminApi\AbstractDocument::toSnakeCase
*/
public function testToQueryString()
{
$this->document
->setTarget('target')
->setWeight(1000)
->setCreatedAt(42);

$this->assertSame(
'target=target&weight=1000&created_at=42',
$this->document->toQueryString()
);
}
}

0 comments on commit 21c7d76

Please sign in to comment.