<commit_before><?php

/**
 * This file is part of the DigitalOcean library.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace DigitalOcean\Tests\Domains;

use DigitalOcean\Tests\TestCase;
use DigitalOcean\Domains\Domains;
use DigitalOcean\Domains\DomainsActions;
use DigitalOcean\Domains\RecordsActions;

/**
 * @author <NAME> <<EMAIL>>
 */
class DomainsTest extends TestCase
{
    protected $domainId;
    protected $domainName;
    protected $recordId;
    protected $domains;
    protected $domainsBuildQueryMethod;

    protected function setUp()
    {
        $this->domainId   = 123;
        $this->domainName = 'foo.org';
        $this->recordId   = 456;

        $this->domains = new Domains($this->getMockCredentials(), $this->getMockAdapter($this->never()));
        $this->domainsBuildQueryMethod = new \ReflectionMethod(
            $this->domains, 'buildQuery'
        );
        $this->domainsBuildQueryMethod->setAccessible(true);

        $this->recordsBuildQueryMethod = new \ReflectionMethod(
            $this->domains, 'buildRecordsQuery'
        );
        $this->recordsBuildQueryMethod->setAccessible(true);
    }

    /**
     * @expectedException \RuntimeException
     * @expectedExceptionMEssage Impossible to process this query: https://api.digitalocean.com/domains/?client_id=foo&api_key=bar
     */
    public function testProcessQuery()
    {
        $sshKeys = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns(null));
        $sshKeys->getAll();
    }

    public function testGetAllUrl()
    {
        $this->assertEquals(
            'https://api.digitalocean.com/domains/?client_id=foo&api_key=bar',
            $this->domainsBuildQueryMethod->invoke($this->domains)
        );
    }

    public function testGetAllReturnsNoDomains()
    {
        $response = <<<JSON
{"status":"OK","domains":[]}
JSON
        ;

        $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
        $domains = $domains->getAll();

        $this->assertTrue(is_object($domains));
        $this->assertEquals('OK', $domains->status);
        $this->assertEmpty($domains->domains);
    }

    public function testGetAll()
    {
        $response = <<<'JSON'
{
  "status": "OK",
  "domains": [
    {
      "id": 100,
      "name": "example.com",
      "ttl": 1800,
      "live_zone_file": "$TTL\\t600\\n@\\t\\tIN\\tSOA\\tNS1.DIGITALOCEAN.COM.\\thostmaster.example.com. (\\n\\t\\t\\t1369261882 ; last update: 2013-05-22 22:31:22 UTC\\n\\t\\t\\t3600 ; refresh\\n\\t\\t\\t900 ; retry\\n\\t\\t\\t1209600 ; expire\\n\\t\\t\\t10800 ; 3 hours ttl\\n\\t\\t\\t)\\n             IN      NS      NS1.DIGITALOCEAN.COM.\\n @\\tIN A\\t8.8.8.8\\n",
      "error": null,
      "zone_file_with_error": null
    }
  ]
}
JSON
        ;

        $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
        $domains = $domains->getAll();

        $this->assertTrue(is_object($domains));
        $this->assertEquals('OK', $domains->status);
        $this->assertTrue(is_array($domains->domains));

        $domains = $domains->domains[0];
        $this->assertTrue(is_object($domains));
        $this->assertSame(100, $domains->id);
        $this->assertSame('example.com', $domains->name);
        $this->assertSame(1800, $domains->ttl);
        $this->assertSame('$TTL\\t600\\n@\\t\\tIN\\tSOA\\tNS1.DIGITALOCEAN.COM.\\thostmaster.example.com. (\\n\\t\\t\\t1369261882 ; last update: 2013-05-22 22:31:22 UTC\\n\\t\\t\\t3600 ; refresh\\n\\t\\t\\t900 ; retry\\n\\t\\t\\t1209600 ; expire\\n\\t\\t\\t10800 ; 3 hours ttl\\n\\t\\t\\t)\\n             IN      NS      NS1.DIGITALOCEAN.COM.\\n @\\tIN A\\t8.8.8.8\\n', $domains->live_zone_file);
        $this->assertNull($domains->error);
        $this->assertNull($domains->zone_file_with_error);
    }

    public function testShowUrlWithDomainId()
    {
        $this->assertEquals(
            'https://api.digitalocean.com/domains/123/?client_id=foo&api_key=bar',
            $this->domainsBuildQueryMethod->invoke($this->domains, $this->domainId)
        );
    }

    public function testShowUrlWithDomainName()
    {
        $this->assertEquals(
            'https://api.digitalocean.com/domains/foo.org/?client_id=foo&api_key=bar',
            $this->domainsBuildQueryMethod->invoke($this->domains, $this->domainName)
        );
    }

    public function testShow()
    {
        $response = <<<'JSON'
{
  "status": "OK",
  "domains": [
    {
      "id": 100,
      "name": "example.com",
      "ttl": 1800,
      "live_zone_file": "$TTL\\t600\\n@\\t\\tIN\\tSOA\\tNS1.DIGITALOCEAN.COM.\\thostmaster.example.com. (\\n\\t\\t\\t1369261882 ; last update: 2013-05-22 22:31:22 UTC\\n\\t\\t\\t3600 ; refresh\\n\\t\\t\\t900 ; retry\\n\\t\\t\\t1209600 ; expire\\n\\t\\t\\t10800 ; 3 hours ttl\\n\\t\\t\\t)\\n             IN      NS      NS1.DIGITALOCEAN.COM.\\n @\\tIN A\\t8.8.8.8\\n",
      "error": null,
      "zone_file_with_error": null
    }
  ]
}
JSON
        ;

        $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
        $domains = $domains->show($this->domainId);

        $this->assertTrue(is_object($domains));
        $this->assertEquals('OK', $domains->status);
        $this->assertTrue(is_array($domains->domains));

        $domains = $domains->domains[0];
        $this->assertTrue(is_object($domains));
        $this->assertSame(100, $domains->id);
        $this->assertSame('example.com', $domains->name);
        $this->assertSame(1800, $domains->ttl);
        $this->assertSame('$TTL\\t600\\n@\\t\\tIN\\tSOA\\tNS1.DIGITALOCEAN.COM.\\thostmaster.example.com. (\\n\\t\\t\\t1369261882 ; last update: 2013-05-22 22:31:22 UTC\\n\\t\\t\\t3600 ; refresh\\n\\t\\t\\t900 ; retry\\n\\t\\t\\t1209600 ; expire\\n\\t\\t\\t10800 ; 3 hours ttl\\n\\t\\t\\t)\\n             IN      NS      NS1.DIGITALOCEAN.COM.\\n @\\tIN A\\t8.8.8.8\\n', $domains->live_zone_file);
        $this->assertNull($domains->error);
        $this->assertNull($domains->zone_file_with_error);
    }

    public function testAddUrl()
    {
        $newDomain = array(
            'name'       => 'bar.org',
            'ip_address' => '127.0.0.1',<commit_msg>Fixed: Domains::show test<commit_after><?php

/**
 * This file is part of the DigitalOcean library.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace DigitalOcean\Tests\Domains;

use DigitalOcean\Tests\TestCase;
use DigitalOcean\Domains\Domains;
use DigitalOcean\Domains\DomainsActions;
use DigitalOcean\Domains\RecordsActions;

/**
 * @author <NAME> <<EMAIL>>
 */
class DomainsTest extends TestCase
{
    protected $domainId;
    protected $domainName;
    protected $recordId;
    protected $domains;
    protected $domainsBuildQueryMethod;

    protected function setUp()
    {
        $this->domainId   = 123;
        $this->domainName = 'foo.org';
        $this->recordId   = 456;

        $this->domains = new Domains($this->getMockCredentials(), $this->getMockAdapter($this->never()));
        $this->domainsBuildQueryMethod = new \ReflectionMethod(
            $this->domains, 'buildQuery'
        );
        $this->domainsBuildQueryMethod->setAccessible(true);

        $this->recordsBuildQueryMethod = new \ReflectionMethod(
            $this->domains, 'buildRecordsQuery'
        );
        $this->recordsBuildQueryMethod->setAccessible(true);
    }

    /**
     * @expectedException \RuntimeException
     * @expectedExceptionMEssage Impossible to process this query: https://api.digitalocean.com/domains/?client_id=foo&api_key=bar
     */
    public function testProcessQuery()
    {
        $sshKeys = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns(null));
        $sshKeys->getAll();
    }

    public function testGetAllUrl()
    {
        $this->assertEquals(
            'https://api.digitalocean.com/domains/?client_id=foo&api_key=bar',
            $this->domainsBuildQueryMethod->invoke($this->domains)
        );
    }

    public function testGetAllReturnsNoDomains()
    {
        $response = <<<JSON
{"status":"OK","domains":[]}
JSON
        ;

        $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
        $domains = $domains->getAll();

        $this->assertTrue(is_object($domains));
        $this->assertEquals('OK', $domains->status);
        $this->assertEmpty($domains->domains);
    }

    public function testGetAll()
    {
        $response = <<<'JSON'
{
  "status": "OK",
  "domains": [
    {
      "id": 100,
      "name": "example.com",
      "ttl": 1800,
      "live_zone_file": "$TTL\\t600\\n@\\t\\tIN\\tSOA\\tNS1.DIGITALOCEAN.COM.\\thostmaster.example.com. (\\n\\t\\t\\t1369261882 ; last update: 2013-05-22 22:31:22 UTC\\n\\t\\t\\t3600 ; refresh\\n\\t\\t\\t900 ; retry\\n\\t\\t\\t1209600 ; expire\\n\\t\\t\\t10800 ; 3 hours ttl\\n\\t\\t\\t)\\n             IN      NS      NS1.DIGITALOCEAN.COM.\\n @\\tIN A\\t8.8.8.8\\n",
      "error": null,
      "zone_file_with_error": null
    }
  ]
}
JSON
        ;

        $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
        $domains = $domains->getAll();

        $this->assertTrue(is_object($domains));
        $this->assertEquals('OK', $domains->status);
        $this->assertTrue(is_array($domains->domains));

        $domains = $domains->domains[0];
        $this->assertTrue(is_object($domains));
        $this->assertSame(100, $domains->id);
        $this->assertSame('example.com', $domains->name);
        $this->assertSame(1800, $domains->ttl);
        $this->assertSame('$TTL\\t600\\n@\\t\\tIN\\tSOA\\tNS1.DIGITALOCEAN.COM.\\thostmaster.example.com. (\\n\\t\\t\\t1369261882 ; last update: 2013-05-22 22:31:22 UTC\\n\\t\\t\\t3600 ; refresh\\n\\t\\t\\t900 ; retry\\n\\t\\t\\t1209600 ; expire\\n\\t\\t\\t10800 ; 3 hours ttl\\n\\t\\t\\t)\\n             IN      NS      NS1.DIGITALOCEAN.COM.\\n @\\tIN A\\t8.8.8.8\\n', $domains->live_zone_file);
        $this->assertNull($domains->error);
        $this->assertNull($domains->zone_file_with_error);
    }

    public function testShowUrlWithDomainId()
    {
        $this->assertEquals(
            'https://api.digitalocean.com/domains/123/?client_id=foo&api_key=bar',
            $this->domainsBuildQueryMethod->invoke($this->domains, $this->domainId)
        );
    }

    public function testShowUrlWithDomainName()
    {
        $this->assertEquals(
            'https://api.digitalocean.com/domains/foo.org/?client_id=foo&api_key=bar',
            $this->domainsBuildQueryMethod->invoke($this->domains, $this->domainName)
        );
    }

    public function testShow()
    {
        $response = <<<'JSON'
{
  "status": "OK",
  "domain": {
    "id": 100,
    "name": "example.com",
    "ttl": 1800,
    "live_zone_file": "$TTL\\t600\\n@\\t\\tIN\\tSOA\\tNS1.DIGITALOCEAN.COM.\\thostmaster.example.com. (\\n\\t\\t\\t1369261882 ; last update: 2013-05-22 22:31:22 UTC\\n\\t\\t\\t3600 ; refresh\\n\\t\\t\\t900 ; retry\\n\\t\\t\\t1209600 ; expire\\n\\t\\t\\t10800 ; 3 hours ttl\\n\\t\\t\\t)\\n             IN      NS      NS1.DIGITALOCEAN.COM.\\n @\\tIN A\\t8.8.8.8\\n",
    "error": null,
    "zone_file_with_error": null
  }
}
JSON
        ;

        $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
        $domain  = $domains->show($this->domainId);

        $this->assertTrue(is_object($domain));
        $this->assertEquals('OK', $domain->status);

        $domain = $domain->domain;
        $this->assertTrue(is_object($domain));
        $this->assertSame(100, $domain->id);
        $this->assertSame('example.com', $domain->name);
        $this->assertSame(1800, $domain->ttl);
        $this->assertSame('$TTL\\t600\\n@\\t\\tIN\\tSOA\\tNS1.DIGITALOCEAN.COM.\\thostmaster.example.com. (\\n\\t\\t\\t1369261882 ; last update: 2013-05-22 22:31:22 UTC\\n\\t\\t\\t3600 ; refresh\\n\\t\\t\\t900 ; retry\\n\\t\\t\\t1209600 ; expire\\n\\t\\t\\t10800 ; 3 hours ttl\\n\\t\\t\\t)\\n             IN      NS      NS1.DIGITALOCEAN.COM.\\n @\\tIN A\\t8.8.8.8\\n', $domain->live_zone_file);
        $this->assertNull($domain->error);
        $this->assertNull($domain->zone_file_with_error);
    }

    public function testAddUrl()
    {
        $newDomain = array(
            'name'       => 'bar.org',
            'ip_address' => '127.0.0.1',
{'fraction_non_alphanumeric': 0.1895321173671689, 'fraction_numerical': 0.04068199841395718, 'mean_word_length': 2.4885200553250346, 'pattern_counts': {'":': 36, '<': 35, '<?xml version=': 0, '>': 187, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 10, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '364570', 'n_tokens_mistral': 4739, 'n_tokens_neox': 4263, 'n_words': 604}