BaseMethodTest.php 1.48 KB
Newer Older
1 2
<?php

3
namespace yiiunit\extensions\authclient\signature;
4 5 6 7 8 9 10

use yiiunit\extensions\authclient\TestCase;

class BaseMethodTest extends TestCase
{
	/**
	 * Creates test signature method instance.
11
	 * @return \yii\authclient\signature\BaseMethod
12 13 14
	 */
	protected function createTestSignatureMethod()
	{
15
		$signatureMethod = $this->getMock('\yii\authclient\signature\BaseMethod', ['getName', 'generateSignature']);
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
		$signatureMethod->expects($this->any())->method('getName')->will($this->returnValue('testMethodName'));
		$signatureMethod->expects($this->any())->method('generateSignature')->will($this->returnValue('testSignature'));
		return $signatureMethod;
	}

	// Tests :

	public function testGenerateSignature()
	{
		$signatureMethod = $this->createTestSignatureMethod();

		$baseString = 'test_base_string';
		$key = 'test_key';

		$signature = $signatureMethod->generateSignature($baseString, $key);

		$this->assertNotEmpty($signature, 'Unable to generate signature!');
	}

	/**
	 * @depends testGenerateSignature
	 */
	public function testVerify()
	{
		$signatureMethod = $this->createTestSignatureMethod();

		$baseString = 'test_base_string';
		$key = 'test_key';
		$signature = 'unsigned';
		$this->assertFalse($signatureMethod->verify($signature, $baseString, $key), 'Unsigned signature is valid!');

		$generatedSignature = $signatureMethod->generateSignature($baseString, $key);
		$this->assertTrue($signatureMethod->verify($generatedSignature, $baseString, $key), 'Generated signature is invalid!');
	}
}