<commit_before><?php
namespace NamelessCoder\Gizzle\Tests\Unit;

use NamelessCoder\Gizzle\Commit;
use NamelessCoder\Gizzle\Entity;
use NamelessCoder\Gizzle\Payload;
use NamelessCoder\Gizzle\Repository;
use NamelessCoder\Gizzle\Tests\Fixtures\GizzlePlugins\ErrorPlugin;
use NamelessCoder\Gizzle\Tests\Fixtures\GizzlePlugins\Plugin;

/**
 * Class PayloadTest
 */
class PayloadTest extends \PHPUnit_Framework_TestCase {

	public function testLoadPlugins() {
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('validate', 'loadPluginsFromPackage'), array('{}', ''));
		$payload->expects($this->once())->method('loadPluginsFromPackage')
			->with('NamelessCoder\\Gizzle')
			->will($this->returnValue(array()));
		$payload->loadPlugins('NamelessCoder\\Gizzle');
	}

	public function testLoadPluginsSupportsArray() {
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('validate', 'loadPluginsFromPackage'), array('{}', ''));
		$payload->expects($this->exactly(2))->method('loadPluginsFromPackage')
			->with('NamelessCoder\\Gizzle')
			->will($this->returnValue(array()));
		$payload->loadPlugins(array('NamelessCoder\\Gizzle', 'NamelessCoder\\Gizzle'));
	}

	public function testLoadPluginsLoadsExpectedPlugins() {
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('validate'), array('{}', ''));
		$payload->loadPlugins('NamelessCoder\\Gizzle\\Tests\\Fixtures');
		$this->assertAttributeEquals(array(new Plugin()), 'plugins', $payload);
	}

	public function testLoadsAndMapsPayloadData() {
		$data = file_get_contents('tests/fixtures/sample-payload.json');
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('validate'), array($data, ''));
		$payload->loadPlugins('NamelessCoder\\Gizzle\\Tests\\Fixtures');
		$result = $payload->process();
		$this->assertInstanceOf('NamelessCoder\\Gizzle\\Response', $result);
		$this->assertEquals(0, $result->getCode());
	}

	public function testValidate() {
		$data = file_get_contents('tests/fixtures/sample-payload.json');
		$secret = 'dummysecret';
		$hash = hash_hmac('sha1', $data, $secret);
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('readSignatureHeader'), array($data, $secret), '', FALSE);
		$payload->expects($this->once())->method('readSignatureHeader')->will($this->returnValue('sha1=' . $hash));
		$payload->__construct($data, $secret);
	}

	public function testValidateThrowsRuntimeExceptionOnHashMismatch() {
		$data = file_get_contents('tests/fixtures/sample-payload.json');
		$secret = 'dummysecret';
		$hash = hash_hmac('sha1', $data . 'appendforinvalidchecksum', $secret);
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('readSignatureHeader'), array($data, $secret), '', FALSE);
		$payload->expects($this->once())->method('readSignatureHeader')->will($this->returnValue('sha1=' . $hash));
		$this->setExpectedException('RuntimeException', '', 1411225210);
		$payload->__construct($data, $secret);
	}

	public function testReadSignatureHeaderReadsFromServerVariable() {
		$data = file_get_contents('tests/fixtures/sample-payload.json');
		$secret = 'dummysecret';
		$hash = hash_hmac('sha1', $data, $secret);
		$_SERVER['HTTP_X_HUB_SIGNATURE'] = 'sha1=' . $hash;
		$payload = new Payload($data, $secret);
		unset($_SERVER['HTTP_X_HUB_SIGNATURE']);
	}

	public function testResponseContainsErrorCodeAndErrorsWhenPluginsCauseErrors() {
		$errorPlugin = new ErrorPlugin();
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('loadPluginsFromPackage', 'validate'), array('{}', ''));
		$payload->expects($this->once())->method('loadPluginsFromPackage')
			->with('NamelessCoder\\Gizzle')
			->will($this->returnValue(array($errorPlugin)));
		$payload->loadPlugins('NamelessCoder\\Gizzle');
		$result = $payload->process();
		$this->assertInstanceOf('NamelessCoder\\Gizzle\\Response', $result);
		$this->assertEquals(1, $result->getCode());
		$error = reset($result->getErrors());
		$this->assertInstanceOf('RuntimeException', $error);
		$this->assertEquals(1411238763, $error->getCode());
	}

	/**
	 * @dataProvider getPropertyValueDataSets
	 * @param string $property
	 * @param mixed $value<commit_msg>[TASK] Fix broken test<commit_after><?php
namespace NamelessCoder\Gizzle\Tests\Unit;

use NamelessCoder\Gizzle\Commit;
use NamelessCoder\Gizzle\Entity;
use NamelessCoder\Gizzle\Payload;
use NamelessCoder\Gizzle\Repository;
use NamelessCoder\Gizzle\Tests\Fixtures\GizzlePlugins\ErrorPlugin;
use NamelessCoder\Gizzle\Tests\Fixtures\GizzlePlugins\Plugin;

/**
 * Class PayloadTest
 */
class PayloadTest extends \PHPUnit_Framework_TestCase {

	public function testLoadPlugins() {
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('validate', 'loadPluginsFromPackage'), array('{}', ''));
		$payload->expects($this->once())->method('loadPluginsFromPackage')
			->with('NamelessCoder\\Gizzle')
			->will($this->returnValue(array()));
		$payload->loadPlugins('NamelessCoder\\Gizzle');
	}

	public function testLoadPluginsSupportsArray() {
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('validate', 'loadPluginsFromPackage'), array('{}', ''));
		$payload->expects($this->exactly(2))->method('loadPluginsFromPackage')
			->with('NamelessCoder\\Gizzle')
			->will($this->returnValue(array()));
		$payload->loadPlugins(array('NamelessCoder\\Gizzle', 'NamelessCoder\\Gizzle'));
	}

	public function testLoadPluginsLoadsExpectedPlugins() {
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('validate'), array('{}', ''));
		$payload->loadPlugins('NamelessCoder\\Gizzle\\Tests\\Fixtures');
		$this->assertAttributeEquals(array(new Plugin()), 'plugins', $payload);
	}

	public function testLoadsAndMapsPayloadData() {
		$data = file_get_contents('tests/fixtures/sample-payload.json');
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('validate'), array($data, ''));
		$payload->loadPlugins('NamelessCoder\\Gizzle\\Tests\\Fixtures');
		$result = $payload->process();
		$this->assertInstanceOf('NamelessCoder\\Gizzle\\Response', $result);
		$this->assertEquals(0, $result->getCode());
	}

	public function testValidate() {
		$data = file_get_contents('tests/fixtures/sample-payload.json');
		$secret = 'dummysecret';
		$hash = hash_hmac('sha1', $data, $secret);
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('readSignatureHeader'), array($data, $secret), '', FALSE);
		$payload->expects($this->once())->method('readSignatureHeader')->will($this->returnValue('sha1=' . $hash));
		$payload->__construct($data, $secret);
	}

	public function testValidateThrowsRuntimeExceptionOnHashMismatch() {
		$data = file_get_contents('tests/fixtures/sample-payload.json');
		$secret = 'dummysecret';
		$hash = hash_hmac('sha1', $data . 'appendforinvalidchecksum', $secret);
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('readSignatureHeader'), array($data, $secret), '', FALSE);
		$payload->expects($this->once())->method('readSignatureHeader')->will($this->returnValue('sha1=' . $hash));
		$this->setExpectedException('RuntimeException', '', 1411225210);
		$payload->__construct($data, $secret);
	}

	public function testReadSignatureHeaderReadsFromServerVariable() {
		$data = file_get_contents('tests/fixtures/sample-payload.json');
		$secret = 'dummysecret';
		$hash = hash_hmac('sha1', $data, $secret);
		$_SERVER['HTTP_X_HUB_SIGNATURE'] = 'sha1=' . $hash;
		$payload = new Payload($data, $secret);
		unset($_SERVER['HTTP_X_HUB_SIGNATURE']);
	}

	public function testResponseContainsErrorCodeAndErrorsWhenPluginsCauseErrors() {
		$errorPlugin = new ErrorPlugin();
		$payload = $this->getMock('NamelessCoder\\Gizzle\\Payload', array('loadPluginsFromPackage', 'validate'), array('{}', ''));
		$payload->expects($this->once())->method('loadPluginsFromPackage')
			->with('NamelessCoder\\Gizzle')
			->will($this->returnValue(array($errorPlugin)));
		$payload->loadPlugins('NamelessCoder\\Gizzle');
		$result = $payload->process();
		$this->assertInstanceOf('NamelessCoder\\Gizzle\\Response', $result);
		$this->assertEquals(1, $result->getCode());
		$errors = $result->getErrors();
		$error = $errors[0];
		$this->assertInstanceOf('RuntimeException', $error);
		$this->assertEquals(1411238763, $error->getCode());
	}

	/**
	 * @dataProvider getPropertyValueDataSets
	 * @param string $property
	 * @param mixed $value
{'fraction_non_alphanumeric': 0.19198664440734559, 'fraction_numerical': 0.007035535416169807, 'mean_word_length': 6.758556891766882, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1554577', 'n_tokens_mistral': 2903, 'n_tokens_neox': 2748, 'n_words': 350}