Skip to content

Commit

Permalink
add specs for SamanTranslator
Browse files Browse the repository at this point in the history
  • Loading branch information
alibo committed Aug 14, 2015
1 parent 1276257 commit f9024ca
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
6 changes: 6 additions & 0 deletions spec/Nikapps/NikPay/NikPaySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ function is_should_use_default_config_when_no_config_is_provided(SamanConfig $co
$this->bank('saman')
->shouldBeAnInstanceOf('Nikapps\NikPay\PaymentProviders\Saman\Saman');
}

function it_should_return_an_instance_of_saman_translator()
{
$this->translator('saman')
->shouldBeAnInstanceOf('\Nikapps\NikPay\PaymentProviders\Saman\SamanTranslator');
}
}
39 changes: 39 additions & 0 deletions spec/Nikapps/NikPay/PaymentProviders/Saman/SamanTranslatorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace spec\Nikapps\NikPay\PaymentProviders\Saman;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class SamanTranslatorSpec extends ObjectBehavior
{

function it_is_initializable()
{
$this->shouldHaveType('Nikapps\NikPay\PaymentProviders\Saman\SamanTranslator');
}

function it_should_return_error_description()
{
$this->describe(-3)
->shouldBe('ورودیها حاوی کارکترهای ریرمجاز میباشند.');
}

function it_should_return_error_code_when_description_is_not_available()
{
$this->describe(-2000)->shouldBe(-2000);
}

function it_should_return_state_description()
{
$this->describe('Invalid Amount')
->shouldBe('مبلغ سند برگشتی، از مبلغ تراکنش اصلی بیشتر است.');
}

function it_should_return_state_code_when_description_is_not_available()
{
$this->describe('Not Available State Code')
->shouldBe('Not Available State Code');
}

}
29 changes: 18 additions & 11 deletions src/PaymentProviders/Saman/SamanTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ class SamanTranslator implements Translator
protected $states;
protected $errors;

function __construct()
{
$this->states = require_once __DIR__ . '/lang/states.php';
$this->errors = require_once __DIR__ . '/lang/errors.php';
}

/**
* Translate/Describe error or state code
*
Expand All @@ -24,15 +18,14 @@ function __construct()
*/
public function translate($code)
{
if ((is_string($code))) {
$this->load();

if (is_string($code)) {
return isset($this->states[$code]) ? $this->states[$code] : $code;
}

if (is_int($code) && $code < 0) {
return isset($this->errors[$code]) ? $this->errors[$code] : $code;
}
return isset($this->errors[$code]) ? $this->errors[$code] : $code;

return $code;
}

/**
Expand All @@ -58,4 +51,18 @@ public function describe($code)
{
return $this->translate($code);
}

/**
* Load languages
*/
protected function load()
{
if (!$this->states) {
$this->states = require __DIR__ . '/lang/states.php';
}

if (!$this->errors) {
$this->errors = require __DIR__ . '/lang/errors.php';
}
}
}

0 comments on commit f9024ca

Please sign in to comment.