Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Account Id To Zone Creation #195

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Endpoints/Zones.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ public function __construct(Adapter $adapter)
*
* @param string $name
* @param bool $jumpStart
* @param string $organizationID
* @param string $accountId
* @return \stdClass
*/
public function addZone(string $name, bool $jumpStart = false, string $organizationID = ''): \stdClass
public function addZone(string $name, bool $jumpStart = false, string $accountId = ''): \stdClass
{
$options = [
'name' => $name,
'jump_start' => $jumpStart
];

if (!empty($organizationID)) {
$options['organization'] = ['id' => $organizationID];
if (!empty($accountId)) {
$options['account'] = [
'id' => $accountId,
];
}

$user = $this->adapter->post('zones', $options);
Expand Down
78 changes: 78 additions & 0 deletions tests/Endpoints/ZoneCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

class ZoneCacheTest extends TestCase
{
public function testCachePurgeEverything()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeEverything.json');

$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('post')->willReturn($response);

$mock->expects($this->once())
->method('post')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
$this->equalTo(['purge_everything' => true])
);

$zones = new \Cloudflare\API\Endpoints\Zones($mock);
$result = $zones->cachePurgeEverything('c2547eb745079dac9320b638f5e225cf483cc5cfdda41');

$this->assertTrue($result);
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
}

public function testCachePurgeHost()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeHost.json');

$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('post')->willReturn($response);

$mock->expects($this->once())
->method('post')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
$this->equalTo(
[
'files' => [],
'tags' => [],
'hosts' => ['dash.cloudflare.com']
]
)
);

$zones = new \Cloudflare\API\Endpoints\Zones($mock);
$result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [], [], ['dash.cloudflare.com']);

$this->assertTrue($result);
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
}

public function testCachePurge()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurge.json');

$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('post')->willReturn($response);

$mock->expects($this->once())
->method('post')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
$this->equalTo(['files' => [
'https://example.com/file.jpg',
]
])
);

$zones = new \Cloudflare\API\Endpoints\Zones($mock);
$result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [
'https://example.com/file.jpg',
]);

$this->assertTrue($result);
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
}
}
105 changes: 30 additions & 75 deletions tests/Endpoints/ZonesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public function testAddZone()
$this->equalTo([
'name' => 'example.com',
'jump_start' => true,
'organization' => ['id' => '01a7362d577a6c3019a474fd6f485823']
'account' => [
'id' => '01a7362d577a6c3019a474fd6f485823',
],
])
);

Expand All @@ -49,6 +51,33 @@ public function testAddZone()
$this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $zones->getBody()->result->id);
}

public function testAddZoneWithAccountId()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/addZone.json');

$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('post')->willReturn($response);

$mock->expects($this->once())
->method('post')
->with(
$this->equalTo('zones'),
$this->equalTo([
'name' => 'example.com',
'jump_start' => false,
'account' => [
'id' => '023e105f4ecef8ad9ca31a8372d0c353',
],
])
);

$zones = new \Cloudflare\API\Endpoints\Zones($mock);
$result = $zones->addZone('example.com', false, '023e105f4ecef8ad9ca31a8372d0c353');

$this->assertObjectHasAttribute('id', $result);
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $result->account->id);
}

public function testActivationTest()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/activationTest.json');
Expand Down Expand Up @@ -188,78 +217,4 @@ public function testChangeDevelopmentMode()
$this->assertTrue($result);
$this->assertEquals('development_mode', $zones->getBody()->result->id);
}

public function testCachePurgeEverything()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeEverything.json');

$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('post')->willReturn($response);

$mock->expects($this->once())
->method('post')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
$this->equalTo(['purge_everything' => true])
);

$zones = new \Cloudflare\API\Endpoints\Zones($mock);
$result = $zones->cachePurgeEverything('c2547eb745079dac9320b638f5e225cf483cc5cfdda41');

$this->assertTrue($result);
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
}

public function testCachePurgeHost()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeHost.json');

$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('post')->willReturn($response);

$mock->expects($this->once())
->method('post')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
$this->equalTo(
[
'files' => [],
'tags' => [],
'hosts' => ['dash.cloudflare.com']
]
)
);

$zones = new \Cloudflare\API\Endpoints\Zones($mock);
$result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [], [], ['dash.cloudflare.com']);

$this->assertTrue($result);
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
}

public function testCachePurge()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurge.json');

$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('post')->willReturn($response);

$mock->expects($this->once())
->method('post')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
$this->equalTo(['files' => [
'https://example.com/file.jpg',
]
])
);

$zones = new \Cloudflare\API\Endpoints\Zones($mock);
$result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [
'https://example.com/file.jpg',
]);

$this->assertTrue($result);
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
}
}
4 changes: 4 additions & 0 deletions tests/Fixtures/Endpoints/addZone.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"email": "[email protected]",
"owner_type": "user"
},
"account": {
"id": "023e105f4ecef8ad9ca31a8372d0c353",
"name": "Demo Account"
},
"permissions": [
"#zone:read",
"#zone:edit"
Expand Down