<commit_before><?php

/**
 * This file is part of the contentful-management.php package.
 *
 * @copyright 2015-2017 Contentful GmbH
 * @license   MIT
 */
declare(strict_types=1);

namespace Contentful\Tests\Unit\Management\Resource;

use Contentful\Management\Resource\Webhook;
use PHPUnit\Framework\TestCase;

class WebhookTest extends TestCase
{
    public function testGetSetData()
    {
        $webhook = new Webhook('Test Webhook', 'https://www.example.com/webhook');

        $this->assertEquals('Test Webhook', $webhook->getName());
        $this->assertEquals('https://www.example.com/webhook', $webhook->getUrl());
        $webhook->setName('Another name');
        $this->assertEquals('Another name', $webhook->getName());

        $sys = $webhook->getSystemProperties();
        $this->assertEquals('WebhookDefinition', $sys->getType());

        $webhook->setHttpBasicUsername('my_username');
        $this->assertEquals('my_username', $webhook->getHttpBasicUsername());
        $webhook->setHttpBasicPassword('<PASSWORD>');
        $this->assertEquals('<PASSWORD>', $webhook->getHttpBasicPassword());

        $webhook->setHeaders([
            'X-Test-Header' => 'Test value',
            'X-Second-Test' => 'Another value',
        ]);
        $this->assertCount(2, $webhook->getHeaders());
        $this->assertFalse($webhook->hasHeader('X-Another-Header'));
        $webhook->addHeader('X-Another-Header', 'Third test value');
        $this->assertCount(3, $webhook->getHeaders());
        $this->assertTrue($webhook->hasHeader('X-Another-Header'));

        $this->assertEquals('Third test value', $webhook->getHeader('X-Another-Header'));

        try {
            $webhook->getHeader('X-Not-Existing');
            $this->fail('Accessing an non-existing header should result in an exception being thrown.');
        } catch (\Throwable $e) {
            $this->assertInstanceOf(\InvalidArgumentException::class, $e);
            $this->assertEquals('Invalid header key "X-Not-Existing" provided.', $e->getMessage());
        }

        $webhook->removeHeader('X-Test-Header');
        $this->assertEquals([
            'X-Second-Test' => 'Another value',
            'X-Another-Header' => 'Third test value',
        ], $webhook->getHeaders());
        $this->assertEquals('Third test value', $webhook->getHeader('X-Another-Header'));

        try {
            $webhook->removeHeader('X-Not-Existing');
            $this->fail('Accessing an non-existing header should result in an exception being thrown.');
        } catch (\Throwable $e) {
            $this->assertInstanceOf(\InvalidArgumentException::class, $e);
            $this->assertEquals('Invalid header key "X-Not-Existing" provided.', $e->getMessage());
        }

        $webhook->setTopics(['Entry.create', '*.publish', 'invalid_key' => 'Asset.*']);
        $this->assertEquals(['Entry.create', '*.publish', 'Asset.*'], $webhook->getTopics());
        $this->assertFalse($webhook->hasTopic('Entry.publish'));
        $webhook->addTopic('Entry.publish');
        $this->assertTrue($webhook->hasTopic('Entry.create'));
        $this->assertTrue($webhook->hasTopic('Entry.publish'));

        try {
            $webhook->removeTopic('Entry.archive');
            $this->fail('Accessing an non-existing topic should result in an exception being thrown.');
        } catch (\Throwable $e) {
            $this->assertInstanceOf(\InvalidArgumentException::class, $e);
            $this->assertEquals('Invalid topic "Entry.archive" provided.', $e->getMessage());
        }
        $webhook->removeTopic('*.publish');
        $this->assertEquals(['Entry.create', 'Entry.publish', 'Asset.*'], $webhook->getTopics(), '', 0, 10, true);
    }

    public function testJsonSerialize()
    {
        $json = '{"sys":{"type":"WebhookDefinition"},"name":"My webhook","url":"https://www.example.com/webhooks","topics":["Entry.create","ContentType.create","*.publish","Asset.*"],"httpBasicUsername":"my_username","httpBasicPassword":"<PASSWORD>","headers":[{"key":"X-Header-1","value":"Value1"},{"key":"X-Header-2","value":"Value2"}]}';

        $webhook = (new Webhook('My webhook', 'https://www.example.com/webhooks', ['Entry.create', 'ContentType.create', '*.publish', 'Asset.*']))
            ->setHttpBasicUsername('my_username')
            ->setHttpBasicPassword('<PASSWORD>')
            ->addHeader('X-Header-1', 'Value1')
            ->addHeader('X-Header-2', 'Value2')
        ;

        $this->assertJsonStringEqualsJsonString($json, json_encode($webhook));
    }
}
<commit_msg>Adds missing test for Webhook<commit_after><?php

/**
 * This file is part of the contentful-management.php package.
 *
 * @copyright 2015-2017 Contentful GmbH
 * @license   MIT
 */
declare(strict_types=1);

namespace Contentful\Tests\Unit\Management\Resource;

use Contentful\Management\Resource\Webhook;
use PHPUnit\Framework\TestCase;

class WebhookTest extends TestCase
{
    public function testGetSetData()
    {
        $webhook = new Webhook('Test Webhook', 'https://www.example.com/webhook');

        $this->assertEquals('Test Webhook', $webhook->getName());
        $this->assertEquals('https://www.example.com/webhook', $webhook->getUrl());
        $webhook->setName('Another name');
        $this->assertEquals('Another name', $webhook->getName());

        $sys = $webhook->getSystemProperties();
        $this->assertEquals('WebhookDefinition', $sys->getType());

        $webhook->setHttpBasicUsername('my_username');
        $this->assertEquals('my_username', $webhook->getHttpBasicUsername());
        $webhook->setHttpBasicPassword('<PASSWORD>');
        $this->assertEquals('<PASSWORD>', $webhook->getHttpBasicPassword());

        $webhook->setHeaders([
            'X-Test-Header' => 'Test value',
            'X-Second-Test' => 'Another value',
        ]);
        $this->assertCount(2, $webhook->getHeaders());
        $this->assertFalse($webhook->hasHeader('X-Another-Header'));

        try {
            $webhook->setHeaders([
                1 => new \stdClass(),
            ]);
            $this->fail('Setting an array of headers where either keys or values are not strings should result in an exception being thrown.');
        } catch (\Throwable $e) {
            $this->assertInstanceOf(\InvalidArgumentException::class, $e);
            $this->assertEquals('Argument "$headers" of "Webhook::setHeaders()" must be an array where all keys and values are strings.', $e->getMessage());
        }

        $webhook->addHeader('X-Another-Header', 'Third test value');
        $this->assertCount(3, $webhook->getHeaders());
        $this->assertTrue($webhook->hasHeader('X-Another-Header'));

        $this->assertEquals('Third test value', $webhook->getHeader('X-Another-Header'));

        try {
            $webhook->getHeader('X-Not-Existing');
            $this->fail('Accessing an non-existing header should result in an exception being thrown.');
        } catch (\Throwable $e) {
            $this->assertInstanceOf(\InvalidArgumentException::class, $e);
            $this->assertEquals('Invalid header key "X-Not-Existing" provided.', $e->getMessage());
        }

        $webhook->removeHeader('X-Test-Header');
        $this->assertEquals([
            'X-Second-Test' => 'Another value',
            'X-Another-Header' => 'Third test value',
        ], $webhook->getHeaders());
        $this->assertEquals('Third test value', $webhook->getHeader('X-Another-Header'));

        try {
            $webhook->removeHeader('X-Not-Existing');
            $this->fail('Accessing an non-existing header should result in an exception being thrown.');
        } catch (\Throwable $e) {
            $this->assertInstanceOf(\InvalidArgumentException::class, $e);
            $this->assertEquals('Invalid header key "X-Not-Existing" provided.', $e->getMessage());
        }

        $webhook->setTopics(['Entry.create', '*.publish', 'invalid_key' => 'Asset.*']);
        $this->assertEquals(['Entry.create', '*.publish', 'Asset.*'], $webhook->getTopics());
        $this->assertFalse($webhook->hasTopic('Entry.publish'));
        $webhook->addTopic('Entry.publish');
        $this->assertTrue($webhook->hasTopic('Entry.create'));
        $this->assertTrue($webhook->hasTopic('Entry.publish'));

        try {
            $webhook->removeTopic('Entry.archive');
            $this->fail('Accessing an non-existing topic should result in an exception being thrown.');
        } catch (\Throwable $e) {
            $this->assertInstanceOf(\InvalidArgumentException::class, $e);
            $this->assertEquals('Invalid topic "Entry.archive" provided.', $e->getMessage());
        }
        $webhook->removeTopic('*.publish');
        $this->assertEquals(['Entry.create', 'Entry.publish', 'Asset.*'], $webhook->getTopics(), '', 0, 10, true);
    }

    public function testJsonSerialize()
    {
        $json = '{"sys":{"type":"WebhookDefinition"},"name":"My webhook","url":"https://www.example.com/webhooks","topics":["Entry.create","ContentType.create","*.publish","Asset.*"],"httpBasicUsername":"my_username","httpBasicPassword":"<PASSWORD>","headers":[{"key":"X-Header-1","value":"Value1"},{"key":"X-Header-2","value":"Value2"}]}';

        $webhook = (new Webhook('My webhook', 'https://www.example.com/webhooks', ['Entry.create', 'ContentType.create', '*.publish', 'Asset.*']))
            ->setHttpBasicUsername('my_username')
            ->setHttpBasicPassword('<PASSWORD>')
            ->addHeader('X-Header-1', 'Value1')
            ->addHeader('X-Header-2', 'Value2')
        ;

        $this->assertJsonStringEqualsJsonString($json, json_encode($webhook));
    }
}

{'fraction_non_alphanumeric': 0.18693717006521063, 'fraction_numerical': 0.004657902908601594, 'mean_word_length': 3.235861464270057, 'pattern_counts': {'":': 24, '<': 13, '<?xml version=': 0, '>': 159, 'https://': 8, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, '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': '5546254', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2668, 'n_words': 456}