Permalink
Browse files

Unit testing for cache api

  • Loading branch information...
1 parent 9195c3b commit f8b698d3b56e3f9defbfcb9a0c3d82912a56c1cb @iambrosi iambrosi committed Mar 2, 2011
View
31 src/ServerGrove/SGLiveChatBundle/Tests/Cache/Engine/APCCacheManagerTest.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace ServerGrove\SGLiveChatBundle\Tests\Cache;
+
+use ServerGrove\SGLiveChatBundle\Cache\Manager;
+use ServerGrove\SGLiveChatBundle\Cache\Engine\Apc;
+
+/**
+ * @author Ismael Ambrosi<ismael@servergrove.com>
+ */
+class APCCacheManagerTest extends ManagerTestAbstract
+{
+
+ protected function preSetup()
+ {
+ if (!function_exists('\apc_store')) {
+ $this->markTestSkipped('APC not found');
+ }
+
+ if (!ini_get('apc.enabled')) {
+ $this->markTestSkipped('APC not found');
+ }
+
+ $this->setCacheEngineName('apc');
+ }
+
+ protected function createManager()
+ {
+ return new Manager(new Apc());
+ }
+}
View
28 src/ServerGrove/SGLiveChatBundle/Tests/Cache/Engine/MongoCacheManagerTest.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace ServerGrove\SGLiveChatBundle\Tests\Cache;
+
+/**
+ * @author Ismael Ambrosi<ismael@servergrove.com>
+ */
+use ServerGrove\SGLiveChatBundle\Cache\Engine\Mongo;
+
+use ServerGrove\SGLiveChatBundle\Cache\Manager;
+
+class MongoCacheManagerTest extends ManagerTestAbstract
+{
+
+ protected function preSetup()
+ {
+ if (!class_exists('\Mongo')) {
+ $this->markTestSkipped('Mongo not found');
+ }
+
+ $this->setCacheEngineName('mongo');
+ }
+
+ protected function createManager()
+ {
+ return new Manager(new Mongo($this->getContainer()->get('doctrine.odm.mongodb.document_manager')));
+ }
+}
View
88 src/ServerGrove/SGLiveChatBundle/Tests/Cache/ManagerTestAbstract.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace ServerGrove\SGLiveChatBundle\Tests\Cache;
+use ServerGrove\SGLiveChatBundle\Tests\TestCase;
+
+/**
+ * Manager test case.
+ */
+abstract class ManagerTestAbstract extends TestCase
+{
+
+ /**
+ * @var Manager
+ */
+ private $Manager;
+
+ /**
+ * Prepares the environment before running a test.
+ */
+ protected function setUp()
+ {
+ parent::setUp();
+ $this->Manager = $this->createManager();
+ }
+
+ /**
+ * @return ServerGrove\SGLiveChatBundle\Cache\Manager
+ */
+ protected abstract function createManager();
+
+ /**
+ * Cleans up the environment after running a test.
+ */
+ protected function tearDown()
+ {
+ $this->Manager = null;
+ parent::tearDown();
+ }
+
+ /**
+ * Tests Manager->__construct()
+ */
+ public function testCorrectInstance()
+ {
+ $this->assertEquals('ServerGrove\\SGLiveChatBundle\\Cache\\Manager', get_class($this->Manager));
+ }
+
+ /**
+ * Tests Manager->set()
+ */
+ public function testSet()
+ {
+ $className = get_class($this);
+ $this->Manager->set(__NAMESPACE__ . __CLASS__ . __FUNCTION__ . 'MyVar', $className);
+ $this->assertEquals($className, $this->Manager->get(__NAMESPACE__ . __CLASS__ . __FUNCTION__ . 'MyVar'), $this->Manager->get(__NAMESPACE__ . __CLASS__ . __FUNCTION__ . 'MyVar'));
+ }
+
+ /**
+ * Tests Manager->get()
+ */
+ public function testGet()
+ {
+ $this->assertEquals(__NAMESPACE__ . __CLASS__ . __FUNCTION__ . 'MyVar', $this->Manager->get(md5(microtime()), __NAMESPACE__ . __CLASS__ . __FUNCTION__ . 'MyVar'));
+ }
+
+ /**
+ * Tests Manager->has()
+ */
+ public function testHas()
+ {
+ $this->assertFalse($this->Manager->has(md5(microtime())));
+ $this->Manager->set(__NAMESPACE__ . __CLASS__ . __FUNCTION__ . 'MyVar', true);
+ $this->assertTrue($this->Manager->has(__NAMESPACE__ . __CLASS__ . __FUNCTION__ . 'MyVar'));
+ }
+
+ /**
+ * Tests Manager->remove()
+ */
+ public function testRemove()
+ {
+ $this->Manager->set(__NAMESPACE__ . __CLASS__ . __FUNCTION__ . 'MyVar', true);
+ $this->assertTrue($this->Manager->has(__NAMESPACE__ . __CLASS__ . __FUNCTION__ . 'MyVar'));
+ $this->Manager->remove(__NAMESPACE__ . __CLASS__ . __FUNCTION__ . 'MyVar');
+ $this->assertFalse($this->Manager->has(__NAMESPACE__ . __CLASS__ . __FUNCTION__ . 'MyVar'));
+ }
+
+}
+
View
48 src/ServerGrove/SGLiveChatBundle/Tests/Resources/config/config.yml
@@ -0,0 +1,48 @@
+app.config:
+ charset: UTF-8
+ error_handler: null
+ csrf_protection:
+ enabled: true
+ secret: xxxxxxxxxx
+ router: { resource: "%kernel.root_dir%/config/routing.yml" }
+ validation: { enabled: true, annotations: true }
+# templating: { engines: ['twig'] } #assets_version: SomeVersionScheme
+ session:
+ default_locale: en
+ lifetime: 0 # Until browser is closed
+ auto_start: true
+
+doctrine_odm.mongodb:
+ server: mongodb://localhost:27017
+ default_database: livechat_%kernel.environment%
+ options:
+ connect: true
+ metadata_cache_driver: array # array, apc, xcache, memcache
+ auto_generate_hydrator_classes: true
+ auto_generate_proxy_classes: true
+ mappings:
+ SGLiveChatBundle: ~
+
+## Doctrine Configuration
+#doctrine:
+# dbal:
+# dbname: xxxxxxxx
+# user: xxxxxxxx
+# password: ~
+# logging: %kernel.debug%
+# orm:
+# auto_generate_proxy_classes: %kernel.debug%
+# mappings:
+# HelloBundle: ~
+
+## Swiftmailer Configuration
+swiftmailer.config: ~
+# transport: smtp
+# encryption: ssl
+# auth_mode: login
+# host: smtp.gmail.com
+# username: xxxxxxxx
+# password: xxxxxxxx
+
+sglivechat.config:
+ cache_engine: mongo
View
56 src/ServerGrove/SGLiveChatBundle/Tests/TestCase.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace ServerGrove\SGLiveChatBundle\Tests;
+
+use Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\DoctrineMongoDBExtension;
+
+use ServerGrove\SGLiveChatBundle\DependencyInjection\SGLiveChatExtension;
+
+use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
+
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+abstract class TestCase extends \PHPUnit_Framework_TestCase
+{
+
+ private $kernel, $cacheEngineName;
+
+ public function __construct($name = NULL, array $data = array(), $dataName = '')
+ {
+ parent::__construct($name, $data, $dataName);
+ $this->kernel = new TestKernel('test', false);
+ $this->kernel->boot();
+ }
+
+ protected function preSetup()
+ {
+ $this->setCacheEngineName('mongo');
+ }
+
+ /**
+ * @return \Symfony\Component\DependencyInjection\ContainerBuilder
+ */
+ protected function getContainer()
+ {
+ return $this->kernel->getContainer();
+ }
+
+ /**
+ * Prepares the environment before running a test.
+ */
+ protected function setUp()
+ {
+ $this->preSetup();
+ parent::setUp();
+ }
+
+ protected function setCacheEngineName($name)
+ {
+ $this->cacheEngineName = $name;
+ }
+
+ protected function getCacheEngineName()
+ {
+ return $this->cacheEngineName;
+ }
+}
View
39 src/ServerGrove/SGLiveChatBundle/Tests/TestKernel.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace ServerGrove\SGLiveChatBundle\Tests;
+
+use ServerGrove\SGLiveChatBundle\SGLiveChatBundle;
+use Symfony\Bundle\DoctrineMongoDBBundle\DoctrineMongoDBBundle;
+use Symfony\Bundle\DoctrineBundle\DoctrineBundle;
+use Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle;
+use Symfony\Bundle\ZendBundle\ZendBundle;
+use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
+use Symfony\Component\HttpKernel\Kernel;
+use Symfony\Component\DependencyInjection\Loader\LoaderInterface;
+
+class TestKernel extends Kernel
+{
+
+ public function registerBundles()
+ {
+ $bundles = array(
+ new FrameworkBundle(),
+ new ZendBundle(),
+ new SwiftmailerBundle(),
+ new DoctrineBundle(),
+ new DoctrineMongoDBBundle(),
+ new SGLiveChatBundle());
+
+ return $bundles;
+ }
+
+ public function registerRootDir()
+ {
+ return __DIR__;
+ }
+
+ public function registerContainerConfiguration(LoaderInterface $loader)
+ {
+ $loader->load(__DIR__ . '/Resources/config/config.yml');
+ }
+}

0 comments on commit f8b698d

Please sign in to comment.