Permalink
Browse files

Implement singleton based on the given tuple and add appropriate tests

Signed-off-by: David Soria Parra <sn_@gmx.net>
  • Loading branch information...
1 parent d70b2bf commit f576071203f45f624b35945c3da2853d9b16b792 @dsp committed Aug 12, 2009
Showing with 56 additions and 4 deletions.
  1. +5 −4 Gettext.php
  2. +2 −0 tests/AllTests.php
  3. +49 −0 tests/InstanceTest.php
View
@@ -75,14 +75,15 @@
*/
public static function getInstance($directory, $domain, $locale)
{
- if (!self::$instance) {
+ $key = $directory . $domain . $locale;
+ if (!isset(self::$instance[$key])) {
if (extension_loaded('gettext')) {
- self::$instance = new Gettext_Extension($directory, $domain, $locale);
+ self::$instance[$key] = new Gettext_Extension($directory, $domain, $locale);
} else {
- self::$instance = new Gettext_PHP($directory, $domain, $locale);
+ self::$instance[$key] = new Gettext_PHP($directory, $domain, $locale);
}
}
- return self::$instance;
+ return self::$instance[$key];
}
}
View
@@ -1,6 +1,7 @@
<?php
require_once 'PHPUnit/Framework.php';
+require_once 'InstanceTest.php';
require_once 'ExtensionTest.php';
require_once 'PHPTest.php';
@@ -10,6 +11,7 @@ public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('Project');
+ $suite->addTestSuite('InstanceTest');
$suite->addTestSuite('ExtensionTest');
$suite->addTestSuite('PHPTest');
View
@@ -0,0 +1,49 @@
+<?php
+require_once 'PHPUnit/Framework.php';
+require_once '../Gettext.php';
+
+class InstanceTest extends PHPUnit_Framework_TestCase
+{
+
+ public function testGettext()
+ {
+ $g = Gettext::getInstance('./', 'gettext', 'de');
+ $this->assertEquals('Datei existiert nicht', $g->gettext('File does not exist'));
+ $this->assertEquals('Datei ist zu klein', $g->gettext('File is too small'));
+ $this->assertEquals('Foobar', $g->gettext('Foobar'));
+ $this->assertContains('Last-Translator', $g->gettext(null));
+ }
+
+ public function testNonexistantFile()
+ {
+ $g = Gettext::getInstance('./', 'gettext', 'notexistent');
+ $this->assertEquals('Foobar', $g->gettext('Foobar'));
+ }
+
+ public function testGetInstance()
+ {
+ $this->assertFalse(Gettext::getInstance('./', 'gettext', '1') ===
+ Gettext::getInstance('./', 'gettext', '2'));
+ $this->assertTrue(Gettext::getInstance('./', 'gettext', '1') ===
+ Gettext::getInstance('./', 'gettext', '1'));
+ }
+
+ public function testNgettext()
+ {
+ $g = Gettext::getInstance('./', 'gettext', 'de');
+ $this->assertEquals('Datei existiert nicht', $g->gettext('File does not exist'));
+ $this->assertEquals('Datei existiert nicht', $g->ngettext('File does not exist', 'Files donnot exists', 1));
+ $this->assertEquals('Datei ist zu klein', $g->ngettext('File is too small', 'Files are too small', 1));
+ $this->assertEquals('Foobar', $g->ngettext('Foobar', 'Foobar', 1));
+
+ $this->assertEquals('Datei existiert nicht', $g->ngettext('File does not exist', 'Files donnot exists', 2));
+ $this->assertEquals('Dateien sind zu klein', $g->ngettext('File is too small', 'Files are too small', 2));
+ $this->assertEquals('Foobar', $g->ngettext('Foobar', 'Foobar', 2));
+
+ $this->assertContains('Last-Translator', $g->ngettext(null, null, 1));
+
+ $this->assertEquals('Dateien sind zu klein', $g->ngettext('File is too small', 'Files are too small', -1));
+ $this->assertEquals('Dateien sind zu klein', $g->ngettext('File is too small', 'Files are too small', 0));
+ }
+}
+

0 comments on commit f576071

Please sign in to comment.