Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
Added/updated tests for new encoding and encryption cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ralouphie committed Aug 25, 2017
1 parent 0667be0 commit 5e0d202
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 3 deletions.
7 changes: 4 additions & 3 deletions tests/src/Cache/OffloadCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract class OffloadCacheTest extends \PHPUnit_Framework_TestCase

public function testNoValue()
{
$this->assertNull($this->cache->get(__METHOD__));
self::assertNull($this->cache->get(__METHOD__));
}

/**
Expand All @@ -18,8 +18,9 @@ public function testNoValue()
public function testCache($key, $value)
{
$key = __METHOD__ . $key;
$this->assertTrue($this->cache->set($key, $value, 5));
$this->assertEquals($value, $this->cache->get($key));
$val = serialize($value);
self::assertTrue($this->cache->set($key, $val, 5));
self::assertEquals($val, $this->cache->get($key));
}

public function cacheValues()
Expand Down
90 changes: 90 additions & 0 deletions tests/src/Encoders/OffloadEncoderEncryptedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Aol\Offload\Encoders;

class CacheLinkEncoderEncryptedTest extends OffloadEncoderTest
{
public function setUp()
{
parent::setUp();
$this->encoder = new OffloadEncoderEncryptedAes256($this->encoder, 'foo', [
'foo' => 'fookey',
'bar' => 'barkey'
]);
}

/**
* @expectedException \Aol\Offload\Exceptions\OffloadConfigurationException
* @expectedExceptionMessage Invalid $encryption_key_id
*/
public function testEncryptionIdCannotHaveColon()
{
new OffloadEncoderEncryptedAes256(new OffloadEncoderStandard(), 'foo:bar', []);
}

/**
* @expectedException \Aol\Offload\Exceptions\OffloadConfigurationException
* @expectedExceptionMessage Missing $encryption_key_id
*/
public function testEncryptionIdMustBePresentInKeys()
{
new OffloadEncoderEncryptedAes256(new OffloadEncoderStandard(), 'foo', ['bar'=>'baz']);
}

/**
* @expectedException \Aol\Offload\Exceptions\OffloadEncoderException
* @expectedExceptionMessage missing key ID
*/
public function testDecodeMustReceiveKeyWithColon()
{
$this->encoder->decode('not_a_valid_key');
}

/**
* @expectedException \Aol\Offload\Exceptions\OffloadEncoderException
* @expectedExceptionMessage unknown key ID
*/
public function testDecodeMustReceiveValidKey()
{
$this->encoder->decode('bad_key:foobar');
}

/**
* @expectedException \Aol\Offload\Exceptions\OffloadEncoderException
* @expectedExceptionMessage could not unserialize data
*/
public function testDecodeFailsWithBadData()
{
$this->encoder->decode('foo:' . base64_encode('$%^&*('));
}

public function testDecodeFailureUsesBaseWhenFlagSet()
{
$base = self::getMockBuilder('Aol\Offload\Encoders\OffloadEncoderStandard')
->setMethods(['decode'])->getMock();
$base->expects(self::once())->method('decode')->willReturn('test');
$encoder = new OffloadEncoderEncryptedAes256($base, 'foo', ['foo' => 'bar'], true);
self::assertEquals('test', $encoder->decode('foo:' . base64_encode('$%^&*(')));
}

public function testDecodesStandardIfNotEncrypted()
{
$base = new OffloadEncoderStandard();
$encoder = new OffloadEncoderEncryptedAes256($base, 'foo', ['foo' => 'bar'], true);
$encoded = $base->encode('hello world');
self::assertEquals('hello world', $encoder->decode($encoded));
}

/**
* @expectedException \Aol\Offload\Exceptions\OffloadEncoderException
* @expectedExceptionMessage could not serialize data
*/
public function testEncodeRethrowsException()
{
$base = self::getMockBuilder('Aol\Offload\Encoders\OffloadEncoderStandard')
->setMethods(['encode'])->getMock();
$base->expects(self::once())->method('encode')->willThrowException(new \RuntimeException('oh noes'));
$encoder = new OffloadEncoderEncryptedAes256($base, 'foo', ['foo' => 'bar'], true);
$encoder->encode('testing');
}
}
46 changes: 46 additions & 0 deletions tests/src/Encoders/OffloadEncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Aol\Offload\Encoders;

class OffloadEncoderTest extends \PHPUnit_Framework_TestCase
{
/** @var OffloadEncoderInterface */
protected $encoder;

public function setUp()
{
$this->encoder = new OffloadEncoderStandard();
}

/**
* @dataProvider dataWorks
*/
public function testWorks($data)
{
$encoded = $this->encoder->encode($data);
self::assertTrue(is_string($encoded));
self::assertNotEquals($data, $encoded);
$decoded = $this->encoder->decode($encoded);
self::assertEquals($data, $decoded);
}

public function dataWorks()
{
return [
['string'],
[123],
[123.22223],
[true],
[false],
[[1,2,3]],
[['1',2,false,true]],
[['assoc'=>1,'two'=>true,'three'=>[1,2,true]]],
[new \stdClass()],
[call_user_func(function () {
$obj = new \stdClass();
$obj->x = ['hello',1,2,3];
return $obj;
})]
];
}
}
20 changes: 20 additions & 0 deletions tests/src/OffloadManagerMemcachedEncryptedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Aol\Offload;

use Aol\Offload\Encoders\OffloadEncoderEncryptedAes256;

class OffloadManagerMemcachedEncryptedTest extends OffloadManagerMemcachedTest
{
protected function setUp()
{
parent::setUp();
$this->manager->getCache()->setEncoder(
new OffloadEncoderEncryptedAes256(
$this->manager->getCache()->getEncoder(),
'foo',
['foo' => 'bar']
)
);
}
}
33 changes: 33 additions & 0 deletions tests/src/OffloadManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Aol\Offload\Cache\OffloadCacheInterface;
use Aol\Offload\Deferred\OffloadDeferred;
use Aol\Offload\Encoders\OffloadEncoderStandard;
use Aol\Offload\Exceptions\OffloadDrainException;

abstract class OffloadManagerTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -232,4 +233,36 @@ public function testDrainExceptions()
}
$this->fail('Expected OffloadDrainException');
}

public function testCacheUsesSameEncoder()
{
$encoder = $this->manager->getCache()->getEncoder();
self::assertSame($encoder, $this->manager->getCache()->getEncoder());
}

public function testCacheEncoderCanBeSet()
{
$encoder1 = $this->manager->getCache()->getEncoder();
$encoder2 = new OffloadEncoderStandard();
$this->manager->getCache()->setEncoder($encoder2);
self::assertSame($encoder2, $this->manager->getCache()->getEncoder());
self::assertNotSame($encoder1, $this->manager->getCache()->getEncoder());
}

public function testCacheDecoderCanBeSet()
{
$decoder1 = $this->manager->getCache()->getDecoder();
$decoder2 = new OffloadEncoderStandard();
$this->manager->getCache()->setDecoder($decoder2);
self::assertSame($decoder2, $this->manager->getCache()->getDecoder());
self::assertNotSame($decoder1, $this->manager->getCache()->getDecoder());
}

public function testCacheDecoderDefaultsToEncoder()
{
$encoder = new OffloadEncoderStandard();
$this->manager->getCache()->setEncoder($encoder);
self::assertSame($encoder, $this->manager->getCache()->getEncoder());
self::assertSame($encoder, $this->manager->getCache()->getDecoder());
}
}

0 comments on commit 5e0d202

Please sign in to comment.