-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAbstractFormatterTest.php
74 lines (64 loc) · 2.18 KB
/
AbstractFormatterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Nmure\Encryptor\Tests\Formatter;
use Nmure\Encryptor\Encryptor;
use Nmure\Encryptor\Formatter\FormatterInterface;
use PHPUnit\Framework\TestCase;
/**
* Class asserting that a FormatterInterface fulfills the minimum
* requirements of what it should accomplish.
* The formatter test classes should extend this class.
*/
abstract class AbstractFormatterTest extends TestCase
{
protected $iv;
protected $data = 'data';
protected $secret = '452F93C1A737722D8B4ED8DD58766D99';
protected $cipher = 'AES-256-CBC';
protected $formatter;
/**
* Initialises the IV and the concrete FormatterInterface to test.
*/
protected function setUp()
{
$this->iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->cipher));
$this->formatter = $this->getFormatter();
}
/**
* Free the concrete FormatterInterface to test.
*/
protected function tearDown()
{
unset($this->formatter);
}
/**
* Assert that the concrete FormatterInterface can produce a correct
* output and read it back as expected.
*/
public function testFormatAndParse()
{
$formatted = $this->formatter->format($this->iv, $this->data);
$parsed = $this->formatter->parse($formatted, openssl_cipher_iv_length($this->cipher));
$this->assertEquals($this->iv, $parsed[FormatterInterface::KEY_IV]);
$this->assertEquals($this->data, $parsed[FormatterInterface::KEY_DATA]);
}
/**
* Asserts that the concrete FormatterInterface is working
* with the Encryptor.
*/
public function testFormatterWithEncryptor()
{
$encryptor = new Encryptor($this->secret, $this->cipher);
$encryptor->setFormatter($this->formatter);
$output = $encryptor->encrypt($this->data);
$this->assertEquals($this->data, $encryptor->decrypt($output));
}
/**
* Assert that the concrete FormatterInterface is producing
* expected output.
*/
abstract public function testFormat();
/**
* @return Nmure\Encryptor\Formatter\FormatterInterface The concrete FormatterInterface to test.
*/
abstract protected function getFormatter();
}