summaryrefslogtreecommitdiff
path: root/tests/UnitTests/framework/Data
diff options
context:
space:
mode:
Diffstat (limited to 'tests/UnitTests/framework/Data')
-rw-r--r--tests/UnitTests/framework/Data/CacheTestCase.php98
-rw-r--r--tests/UnitTests/framework/Data/utMemCache.php66
-rw-r--r--tests/UnitTests/framework/Data/utSqliteCache.php59
-rw-r--r--tests/UnitTests/framework/Data/utXmlDocument.php68
-rw-r--r--tests/UnitTests/framework/Data/xml/data1.xml45
-rw-r--r--tests/UnitTests/framework/Data/xml/data1.xml.out41
-rw-r--r--tests/UnitTests/framework/Data/xml/data2.xml41
-rw-r--r--tests/UnitTests/framework/Data/xml/data2.xml.out40
-rw-r--r--tests/UnitTests/framework/Data/xml/data3.xml46
-rw-r--r--tests/UnitTests/framework/Data/xml/data3.xml.out45
10 files changed, 549 insertions, 0 deletions
diff --git a/tests/UnitTests/framework/Data/CacheTestCase.php b/tests/UnitTests/framework/Data/CacheTestCase.php
new file mode 100644
index 00000000..68a7b067
--- /dev/null
+++ b/tests/UnitTests/framework/Data/CacheTestCase.php
@@ -0,0 +1,98 @@
+<?php
+
+require_once(dirname(__FILE__).'/../common.php');
+
+class CacheTestCase extends UnitTestCase
+{
+ private $_cache;
+
+ public function getCache()
+ {
+ return $this->_cache;
+ }
+
+ public function setCache($cache)
+ {
+ $this->_cache=$cache;
+ }
+
+ public function basicOperations()
+ {
+ $object=new TComponent;
+ $number=12345;
+ $string='12345\'"';
+ $array=array('123'=>123,'abc'=>'def');
+
+ // test set (first time)
+ $this->assertFalse($this->_cache->get('object'));
+ $this->assertTrue($this->_cache->set('object',$object));
+ $this->assertTrue($this->_cache->get('object') instanceof TComponent);
+ $this->assertFalse($this->_cache->get('number'));
+ $this->assertTrue($this->_cache->set('number',$number));
+ $this->assertTrue($this->_cache->get('number')===$number);
+ $this->assertFalse($this->_cache->get('string'));
+ $this->assertTrue($this->_cache->set('string',$string));
+ $this->assertTrue($this->_cache->get('string')===$string);
+ $this->assertFalse($this->_cache->get('array'));
+ $this->assertTrue($this->_cache->set('array',$array));
+ $this->assertTrue($this->_cache->get('array')===$array);
+
+ // test set (second time)
+ $this->assertTrue($this->_cache->set('object',$array));
+ $this->assertTrue($this->_cache->get('object')===$array);
+
+ // test delete
+ $this->assertTrue($this->_cache->delete('object'));
+ $this->assertFalse($this->_cache->get('object'));
+ $this->assertTrue($this->_cache->delete('number'));
+ $this->assertFalse($this->_cache->get('number'));
+ $this->assertTrue($this->_cache->delete('string'));
+ $this->assertFalse($this->_cache->get('string'));
+ $this->assertTrue($this->_cache->delete('array'));
+ $this->assertFalse($this->_cache->get('array'));
+
+ // test add (first time)
+ $this->assertFalse($this->_cache->get('object'));
+ $this->assertTrue($this->_cache->add('object',$object));
+ $this->assertTrue($this->_cache->get('object') instanceof TComponent);
+ $this->assertFalse($this->_cache->get('number'));
+ $this->assertTrue($this->_cache->add('number',$number));
+ $this->assertTrue($this->_cache->get('number')===$number);
+ $this->assertFalse($this->_cache->get('string'));
+ $this->assertTrue($this->_cache->add('string',$string));
+ $this->assertTrue($this->_cache->get('string')===$string);
+ $this->assertFalse($this->_cache->get('array'));
+ $this->assertTrue($this->_cache->add('array',$array));
+ $this->assertTrue($this->_cache->get('array')===$array);
+
+ // test add (second time)
+ $this->assertFalse($this->_cache->add('object',$array));
+ $this->assertTrue($this->_cache->get('object') instanceof TComponent);
+
+ // test replace
+ $this->assertTrue($this->_cache->replace('object',$array));
+ $this->assertTrue($this->_cache->get('object')===$array);
+ $this->assertFalse($this->_cache->replace('object2',$array));
+ $this->assertFalse($this->_cache->get('object2'));
+
+ // test flush
+ $this->assertTrue($this->_cache->set('number',$number));
+ $this->assertTrue($this->_cache->get('number')===$number);
+ $this->assertTrue($this->_cache->flush());
+ $this->assertFalse($this->_cache->get('number'));
+
+ // test expiring
+ // set a value with 5sec valid time
+ $this->_cache->set('expiring',123,3);
+ $this->assertTrue($this->_cache->get('expiring')===123);
+ $this->_cache->set('nonexpiring',456);
+ $this->assertTrue($this->_cache->get('nonexpiring')===456);
+
+ // wait 6sec to see if the value still exists
+ sleep(4);
+ $this->assertFalse($this->_cache->get('expiring'));
+ $this->assertTrue($this->_cache->get('nonexpiring')===456);
+ }
+}
+
+?> \ No newline at end of file
diff --git a/tests/UnitTests/framework/Data/utMemCache.php b/tests/UnitTests/framework/Data/utMemCache.php
new file mode 100644
index 00000000..26981f63
--- /dev/null
+++ b/tests/UnitTests/framework/Data/utMemCache.php
@@ -0,0 +1,66 @@
+<?php
+
+require_once(dirname(__FILE__).'/../common.php');
+require_once(dirname(__FILE__).'/CacheTestCase.php');
+Prado::using('System.Data.TMemCache');
+
+class utMemCache extends UnitTestCase
+{
+ private $_prefix='';
+ private $_server='localhost';
+ private $_port=11211;
+
+ public function testInit()
+ {
+ if(!extension_loaded('memcache'))
+ {
+ $this->fail('TMemCache is not tested. PHP extension "memcache" is required by TMemCache.');
+ return;
+ }
+ $cache=new TMemCache;
+
+ $this->assertTrue($cache->getHost()==='localhost');
+ $cache->setHost('localhost2');
+ $this->assertTrue($cache->getHost()==='localhost2');
+
+ $this->assertTrue($cache->getPort()===11211);
+ $cache->setPort(1000);
+ $this->assertTrue($cache->getPort()===1000);
+
+ $cache->init(null,null);
+ try
+ {
+ $cache->setHost('newhost');
+ $this->fail('exception not raised when setting Server after init');
+ }
+ catch(TInvalidOperationException $e)
+ {
+ $this->pass();
+ }
+ try
+ {
+ $cache->setPort(10000);
+ $this->fail('exception not raised when setting Port after init');
+ }
+ catch(TInvalidOperationException $e)
+ {
+ $this->pass();
+ }
+ }
+
+ public function testBasicOperations()
+ {
+ if(!extension_loaded('memcache'))
+ {
+ $this->fail('TMemCache is not tested. PHP extension "memcache" is required by TMemCache.');
+ return;
+ }
+ $cache=new TMemCache;
+ $cache->init(null,null);
+ $this->setCache($cache);
+ $this->basicOperations();
+ $this->setCache(null);
+ }
+}
+
+?> \ No newline at end of file
diff --git a/tests/UnitTests/framework/Data/utSqliteCache.php b/tests/UnitTests/framework/Data/utSqliteCache.php
new file mode 100644
index 00000000..726a5c08
--- /dev/null
+++ b/tests/UnitTests/framework/Data/utSqliteCache.php
@@ -0,0 +1,59 @@
+<?php
+
+require_once(dirname(__FILE__).'/../common.php');
+require_once(dirname(__FILE__).'/CacheTestCase.php');
+Prado::using('System.Data.TSqliteCache');
+
+class utSqliteCache extends CacheTestCase
+{
+ private $dbFile;
+
+ public function __construct()
+ {
+ parent::__construct();
+ if(Prado::getPathOfAlias('utSqliteCache')===null)
+ Prado::setPathOfAlias('utSqliteCache',dirname(__FILE__));
+ $this->dbFile='utSqliteCache.test';
+ }
+
+ public function tearDown()
+ {
+ $file=Prado::getPathOfNamespace('utSqliteCache.test',TSqliteCache::DB_FILE_EXT);
+ if(is_file($file))
+ unlink($file);
+ else
+ $this->fail("Unable to clean up db file: '".$file."'.");
+ }
+
+ public function testInit()
+ {
+ $cache=new TSqliteCache;
+
+ $this->assertTrue($cache->getDbFile()===null);
+ $cache->setDbFile($this->dbFile);
+ $this->assertTrue($cache->getDbFile()===$this->dbFile);
+
+ $cache->init(null,null);
+ try
+ {
+ $cache->setDbFile('newfile.db');
+ $this->fail('exception not raised when setting DbFile after init');
+ }
+ catch(TInvalidOperationException $e)
+ {
+ $this->pass();
+ }
+ }
+
+ public function testBasicOperations()
+ {
+ $cache=new TSqliteCache;
+ $cache->setDbFile($this->dbFile);
+ $cache->init(null,null);
+ $this->setCache($cache);
+ $this->basicOperations();
+ $this->setCache(null);
+ }
+}
+
+?> \ No newline at end of file
diff --git a/tests/UnitTests/framework/Data/utXmlDocument.php b/tests/UnitTests/framework/Data/utXmlDocument.php
new file mode 100644
index 00000000..f66a4f2d
--- /dev/null
+++ b/tests/UnitTests/framework/Data/utXmlDocument.php
@@ -0,0 +1,68 @@
+<?php
+
+require_once(dirname(__FILE__).'/../common.php');
+
+class utXmlDocument extends UnitTestCase
+{
+ public function setUp()
+ {
+ }
+
+ public function tearDown()
+ {
+ }
+
+ public function testLoadAndSave()
+ {
+ $dir=dirname(__FILE__).'/xml';
+
+ // a regular XML file
+ $doc=new TXmlDocument;
+ $doc->loadFromFile($dir.'/data1.xml');
+ $doc->saveToFile($dir.'/data1.xml.tmp');
+ $this->assertTrue($this->compareFiles($dir.'/data1.xml.tmp',$dir.'/data1.xml.out'));
+ @unlink($dir.'/data1.xml.tmp');
+
+ // an XML file with Chinese characters
+ $doc->loadFromFile($dir.'/data2.xml');
+ $doc->saveToFile($dir.'/data2.xml.tmp');
+ $this->assertTrue($this->compareFiles($dir.'/data2.xml.tmp',$dir.'/data2.xml.out'));
+ @unlink($dir.'/data2.xml.tmp');
+
+ // a typical Prado Application configuration file
+ $doc=new TXmlDocument;
+ $doc->loadFromFile($dir.'/data3.xml');
+ $doc->saveToFile($dir.'/data3.xml.tmp');
+ $this->assertTrue($this->compareFiles($dir.'/data3.xml.tmp',$dir.'/data3.xml.out'));
+ @unlink($dir.'/data3.xml.tmp');
+ }
+
+ protected function compareFiles($file1,$file2)
+ {
+ return file_get_contents($file1)===file_get_contents($file2);
+ }
+
+ public function testAccessDomTree()
+ {
+ $dir=dirname(__FILE__).'/xml';
+ $doc=new TXmlDocument;
+ $doc->loadFromFile($dir.'/data1.xml');
+ $this->assertTrue($doc->getVersion()==='1.0');
+ $this->assertTrue($doc->getEncoding()==='utf-8');
+ $this->assertTrue($doc->getElements()->getCount()===2);
+ $this->assertTrue($doc->getElements()->itemAt(0)->getTagName()==='title');
+ $this->assertTrue($doc->getElements()->itemAt(0)->getValue()==='My lists');
+ $this->assertTrue($doc->getElements()->itemAt(1)->getTagName()==='chapter');
+ $this->assertTrue($doc->getElements()->itemAt(1)->getAttribute('id')==='books');
+ }
+
+ public function testUpdateDomTree()
+ {
+ }
+
+ public function testComposeDomTree()
+ {
+ }
+}
+
+?> \ No newline at end of file
diff --git a/tests/UnitTests/framework/Data/xml/data1.xml b/tests/UnitTests/framework/Data/xml/data1.xml
new file mode 100644
index 00000000..d865680a
--- /dev/null
+++ b/tests/UnitTests/framework/Data/xml/data1.xml
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
+ "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
+]>
+<book id="listing">
+ <title>My lists</title>
+ <chapter id="books">
+ <title>My books</title>
+ <para>
+ <informaltable>
+ <tgroup cols="4">
+ <thead>
+ <row>
+ <entry>Title</entry>
+ <entry>Author</entry>
+ <entry>Language</entry>
+ <entry>ISBN</entry>
+ </row>
+ </thead>
+ <tbody>
+ <row>
+ <entry>The Grapes of Wrath</entry>
+ <entry>John Steinbeck</entry>
+ <entry>en</entry>
+ <entry>0140186409</entry>
+ </row>
+ <row>
+ <entry>The Pearl</entry>
+ <entry>John Steinbeck</entry>
+ <entry>en</entry>
+ <entry>014017737X</entry>
+ </row>
+ <row>
+ <entry>Samarcande</entry>
+ <entry>Amine Maalouf</entry>
+ <entry>fr</entry>
+ <entry>2253051209</entry>
+ </row>
+ <!-- TODO: I have a lot of remaining books to add.. -->
+ </tbody>
+ </tgroup>
+ </informaltable>
+ </para>
+ </chapter>
+</book>
diff --git a/tests/UnitTests/framework/Data/xml/data1.xml.out b/tests/UnitTests/framework/Data/xml/data1.xml.out
new file mode 100644
index 00000000..de491e4c
--- /dev/null
+++ b/tests/UnitTests/framework/Data/xml/data1.xml.out
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<book id="listing">
+ <title>My lists</title>
+ <chapter id="books">
+ <title>My books</title>
+ <para>
+ <informaltable>
+ <tgroup cols="4">
+ <thead>
+ <row>
+ <entry>Title</entry>
+ <entry>Author</entry>
+ <entry>Language</entry>
+ <entry>ISBN</entry>
+ </row>
+ </thead>
+ <tbody>
+ <row>
+ <entry>The Grapes of Wrath</entry>
+ <entry>John Steinbeck</entry>
+ <entry>en</entry>
+ <entry>0140186409</entry>
+ </row>
+ <row>
+ <entry>The Pearl</entry>
+ <entry>John Steinbeck</entry>
+ <entry>en</entry>
+ <entry>014017737X</entry>
+ </row>
+ <row>
+ <entry>Samarcande</entry>
+ <entry>Amine Maalouf</entry>
+ <entry>fr</entry>
+ <entry>2253051209</entry>
+ </row>
+ </tbody>
+ </tgroup>
+ </informaltable>
+ </para>
+ </chapter>
+</book> \ No newline at end of file
diff --git a/tests/UnitTests/framework/Data/xml/data2.xml b/tests/UnitTests/framework/Data/xml/data2.xml
new file mode 100644
index 00000000..9231ef7b
--- /dev/null
+++ b/tests/UnitTests/framework/Data/xml/data2.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<book id="listing" title="我的书单">
+ <chapter id="books">
+ <title>我的书</title>
+ <para>
+ <informaltable>
+ <tgroup cols="4">
+ <thead>
+ <row>
+ <entry>标题</entry>
+ <entry>作者</entry>
+ <entry>语言</entry>
+ <entry>ISBN</entry>
+ </row>
+ </thead>
+ <tbody>
+ <row>
+ <entry>The Grapes of Wrath</entry>
+ <entry>John Steinbeck</entry>
+ <entry>en</entry>
+ <entry>0140186409</entry>
+ </row>
+ <row>
+ <entry>The Pearl</entry>
+ <entry>John Steinbeck</entry>
+ <entry>en</entry>
+ <entry>014017737X</entry>
+ </row>
+ <row>
+ <entry>Samarcande</entry>
+ <entry>Amine Maalouf</entry>
+ <entry>fr</entry>
+ <entry>2253051209</entry>
+ </row>
+ <!-- TODO: I have a lot of remaining books to add.. -->
+ </tbody>
+ </tgroup>
+ </informaltable>
+ </para>
+ </chapter>
+</book>
diff --git a/tests/UnitTests/framework/Data/xml/data2.xml.out b/tests/UnitTests/framework/Data/xml/data2.xml.out
new file mode 100644
index 00000000..d688884c
--- /dev/null
+++ b/tests/UnitTests/framework/Data/xml/data2.xml.out
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<book id="listing" title="我的书单">
+ <chapter id="books">
+ <title>我的书</title>
+ <para>
+ <informaltable>
+ <tgroup cols="4">
+ <thead>
+ <row>
+ <entry>标题</entry>
+ <entry>作者</entry>
+ <entry>语言</entry>
+ <entry>ISBN</entry>
+ </row>
+ </thead>
+ <tbody>
+ <row>
+ <entry>The Grapes of Wrath</entry>
+ <entry>John Steinbeck</entry>
+ <entry>en</entry>
+ <entry>0140186409</entry>
+ </row>
+ <row>
+ <entry>The Pearl</entry>
+ <entry>John Steinbeck</entry>
+ <entry>en</entry>
+ <entry>014017737X</entry>
+ </row>
+ <row>
+ <entry>Samarcande</entry>
+ <entry>Amine Maalouf</entry>
+ <entry>fr</entry>
+ <entry>2253051209</entry>
+ </row>
+ </tbody>
+ </tgroup>
+ </informaltable>
+ </para>
+ </chapter>
+</book> \ No newline at end of file
diff --git a/tests/UnitTests/framework/Data/xml/data3.xml b/tests/UnitTests/framework/Data/xml/data3.xml
new file mode 100644
index 00000000..9e66a7f0
--- /dev/null
+++ b/tests/UnitTests/framework/Data/xml/data3.xml
@@ -0,0 +1,46 @@
+<?xml version="1.0"?>
+<application id="test">
+ <modules>
+ <module id="request" />
+ <module id="response" />
+ <module id="cache" type="System.Modules.TSqliteCache"
+ DbFile="protected/cache.db" />
+ <module id="error">
+ <case id="exception" handler="processException" />
+ <default handler="processHttpError" />
+ </module>
+ </modules>
+ <services default="page">
+ <service id="page" RootPath="protected/pages">
+ <modules>
+ <module id="template" type="System.Modules.TTemplateManager" />
+ <module id="session" type="System.Modules.TSession" />
+ </modules>
+ <pages default="home">
+ <page id="home" type="HomePage" />
+ <page id="about" type="AboutPage" />
+ </pages>
+ <location path="users">
+ <security>
+ <allow page="register,login" />
+ <deny page="profile" user="?" />
+ <allow page="admin" role="admin" />
+ <deny page="admin" />
+ </security>
+ <pages>
+ <page id="register" type="RegisterPage" />
+ <page id="login" type="LoginPage" />
+ <page id="admin" type="AdminPage" />
+ <page id="profile" type="ProfilePage" />
+ </pages>
+ </location>
+ <parameters>
+ </parameters>
+ </service>
+ <service id="asset" type="System.Services.TAssetService" />
+ </services>
+ <parameters>
+ <parameter id="AdminEmail">qiang.xue@gmail.com</parameter>
+ <parameter id="NetShow" type="Demo.NetShow" Host="localhost" />
+ </parameters>
+</application>
diff --git a/tests/UnitTests/framework/Data/xml/data3.xml.out b/tests/UnitTests/framework/Data/xml/data3.xml.out
new file mode 100644
index 00000000..a87d0558
--- /dev/null
+++ b/tests/UnitTests/framework/Data/xml/data3.xml.out
@@ -0,0 +1,45 @@
+<?xml version="1.0"?>
+<application id="test">
+ <modules>
+ <module id="request" />
+ <module id="response" />
+ <module id="cache" type="System.Modules.TSqliteCache" DbFile="protected/cache.db" />
+ <module id="error">
+ <case id="exception" handler="processException" />
+ <default handler="processHttpError" />
+ </module>
+ </modules>
+ <services default="page">
+ <service id="page" RootPath="protected/pages">
+ <modules>
+ <module id="template" type="System.Modules.TTemplateManager" />
+ <module id="session" type="System.Modules.TSession" />
+ </modules>
+ <pages default="home">
+ <page id="home" type="HomePage" />
+ <page id="about" type="AboutPage" />
+ </pages>
+ <location path="users">
+ <security>
+ <allow page="register,login" />
+ <deny page="profile" user="?" />
+ <allow page="admin" role="admin" />
+ <deny page="admin" />
+ </security>
+ <pages>
+ <page id="register" type="RegisterPage" />
+ <page id="login" type="LoginPage" />
+ <page id="admin" type="AdminPage" />
+ <page id="profile" type="ProfilePage" />
+ </pages>
+ </location>
+ <parameters>
+ </parameters>
+ </service>
+ <service id="asset" type="System.Services.TAssetService" />
+ </services>
+ <parameters>
+ <parameter id="AdminEmail">qiang.xue@gmail.com</parameter>
+ <parameter id="NetShow" type="Demo.NetShow" Host="localhost" />
+ </parameters>
+</application> \ No newline at end of file