From 72593f8746240c912e1751a1e055b805330fff5c Mon Sep 17 00:00:00 2001 From: Zemistr Date: Fri, 27 Mar 2015 02:10:08 +0100 Subject: [PATCH] Added support and tests for Storage. --- src/l10n.php | 1 + src/l10n/Translator/IStorage.php | 8 ++++++++ src/l10n/Translator/Translator.php | 22 ++++++++++++++++++---- tests/InterfacesTest.php | 8 ++++++++ tests/TranslatorTest.php | 23 ++++++++++++++++++++++- 5 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/l10n/Translator/IStorage.php diff --git a/src/l10n.php b/src/l10n.php index 4550d06..b036c16 100644 --- a/src/l10n.php +++ b/src/l10n.php @@ -135,6 +135,7 @@ function ($class_name) { 'l10n\\Plural\\PluralRule8' => 'Plural/PluralRule8.php', 'l10n\\Plural\\PluralRule9' => 'Plural/PluralRule9.php', + 'l10n\\Translator\\IStorage' => 'Translator/IStorage.php', 'l10n\\Translator\\Translator' => 'Translator/Translator.php' ); diff --git a/src/l10n/Translator/IStorage.php b/src/l10n/Translator/IStorage.php new file mode 100644 index 0000000..a78fd77 --- /dev/null +++ b/src/l10n/Translator/IStorage.php @@ -0,0 +1,8 @@ +plural = $plural; + + if ($loader) { + $this->storage = $loader; + $this->storage->load($this); + } + } + + public function __destruct() { + if ($this->storage instanceof IStorage) { + $this->storage->save($this); + } } /** diff --git a/tests/InterfacesTest.php b/tests/InterfacesTest.php index 36de560..be881fa 100644 --- a/tests/InterfacesTest.php +++ b/tests/InterfacesTest.php @@ -20,4 +20,12 @@ public function testIPlural() { $this->assertSame(true, method_exists($mock, 'getPluralForm'), 'Method "getPluralForm" must exists'); $this->assertSame(true, method_exists($mock, 'getPluralsCount'), 'Method "getPluralsCount" must exists'); } + + public function testIStorage() { + $mock = $this->getMock('l10n\Translator\IStorage'); + + $this->assertInstanceOf('l10n\Translator\IStorage', $mock); + $this->assertSame(true, method_exists($mock, 'load'), 'Method "load" must exists'); + $this->assertSame(true, method_exists($mock, 'save'), 'Method "save" must exists'); + } } diff --git a/tests/TranslatorTest.php b/tests/TranslatorTest.php index 32985e2..02d603b 100644 --- a/tests/TranslatorTest.php +++ b/tests/TranslatorTest.php @@ -23,13 +23,34 @@ function ($n) { return $mock; } - public function testConstructor() { + private function createStorageMock() { + /** @var \l10n\Translator\IStorage|PHPUnit_Framework_MockObject_MockObject $mock */ + $mock = $this->getMock('l10n\Translator\IStorage'); + + $mock->expects($this->once()) + ->method("load") + ->with($this->isInstanceOf('l10n\Translator\Translator')); + + $mock->expects($this->once()) + ->method("save") + ->with($this->isInstanceOf('l10n\Translator\Translator')); + + return $mock; + } + + public function testConstructorAndDestruct() { $plural = $this->createPluralMock(); $translator = new Translator($plural); $this->assertInstanceOf('l10n\Plural\IPlural', $plural); $this->assertSame($plural, $translator->getPlural()); + $translator->__destruct(); + + $loader = $this->createStorageMock(); + + $translator = new Translator($plural, $loader); + $translator->__destruct(); } public function testSetGetRemoveText() {