Skip to content

Commit

Permalink
Added support and tests for Storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zemistr committed Mar 27, 2015
1 parent cefce0d commit 72593f8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/l10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);

Expand Down
8 changes: 8 additions & 0 deletions src/l10n/Translator/IStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace l10n\Translator;

interface IStorage {
public function load(Translator $translator);

public function save(Translator $translator);
}
22 changes: 18 additions & 4 deletions src/l10n/Translator/Translator.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
<?php

namespace l10n\Translator;

use l10n\Plural\IPlural;

class Translator {
/** @var \l10n\Plural\IPlural */
private $plural;
protected $plural;

/** @var array */
private $translated = array();

/** @var array */
private $untranslated = array();

/** @var \l10n\Translator\IStorage */
protected $storage;

/**
* @param \l10n\Plural\IPlural $plural
* @param \l10n\Plural\IPlural $plural
* @param \l10n\Translator\IStorage $loader
*/
public function __construct(IPlural $plural) {
public function __construct(IPlural $plural, IStorage $loader = null) {
$this->plural = $plural;

if ($loader) {
$this->storage = $loader;
$this->storage->load($this);
}
}

public function __destruct() {
if ($this->storage instanceof IStorage) {
$this->storage->save($this);
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/InterfacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
23 changes: 22 additions & 1 deletion tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 72593f8

Please sign in to comment.