Skip to content

Commit

Permalink
added support for php definitions translations
Browse files Browse the repository at this point in the history
  • Loading branch information
everzet committed Jan 15, 2012
1 parent 47254eb commit 8b3a0bb
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
88 changes: 88 additions & 0 deletions features/annotations/definitions_translations.feature
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,91 @@ Feature: Definitions translations
1 scenario (1 passed)
5 steps (5 passed)
"""

Scenario: In place PHP translations
Given a file named "features/calc_ru.feature" with:
"""
# language: ru
Функция: Базовая калькуляция
Сценарий:
Допустим Я набрал число 10 на калькуляторе
И Я набрал число 4 на калькуляторе
И Я нажал "+"
То Я должен увидеть на экране 14
И пользователь "everzet" должен иметь имя "everzet"
"""
And a file named "features/bootstrap/FeatureContext.php" with:
"""
<?php
require_once 'PHPUnit/Autoload.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';
use Behat\Behat\Context\TranslatedContextInterface,
Behat\Behat\Context\BehatContext,
Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
class FeatureContext extends BehatContext implements TranslatedContextInterface
{
private $numbers = array();
private $result = 0;
/**
* @Given /^I have entered (\d+) into calculator$/
*/
public function iHaveEnteredIntoCalculator($number) {
$this->numbers[] = intval($number);
}
/**
* @Given /^I have clicked "+"$/
*/
public function iHaveClickedPlus() {
$this->result = array_sum($this->numbers);
}
/**
* @Then /^I should see (\d+) on the screen$/
*/
public function iShouldSeeOnTheScreen($result) {
assertEquals(intval($result), $this->result);
}
/** @Transform /"([^"]+)" user/ */
public static function createUserFromUsername($username) {
return (Object) array('name' => $username);
}
/**
* @Then /^the ("[^"]+" user) name should be "([^"]*)"$/
*/
public function theUserUsername($user, $username) {
assertEquals($username, $user->name);
}
public function getTranslationResources() {
return array(__DIR__ . DIRECTORY_SEPARATOR . 'i18n' . DIRECTORY_SEPARATOR . 'ru.php');
}
}
"""
And a file named "features/bootstrap/i18n/ru.php" with:
"""
<?php return array(
'/^I have entered (\d+) into calculator$/' => '/^Я набрал число (\d+) на калькуляторе$/',
'/^I have clicked "+"$/' => '/^Я нажал "([^"]*)"$/',
'/^I should see (\d+) on the screen$/' => '/^Я должен увидеть на экране (\d+)$/',
'/"([^"]+)" user/' => '/пользователь "([^"]+)"/',
'/^the ("[^"]+" user) name should be "([^"]*)"$/' => '/^(пользователь "[^"]+") должен иметь имя "([^"]*)"$/',
);
"""
When I run "behat -f progress features/calc_ru.feature"
Then it should pass with:
"""
.....
1 scenario (1 passed)
5 steps (5 passed)
"""
9 changes: 9 additions & 0 deletions src/Behat/Behat/Context/Loader/TranslatedContextLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public function load(ContextInterface $context)
$this->translator->addResource(
'xliff', $path, basename($path, '.xliff'), 'behat.definitions'
);
} elseif ('php' === $extension) {
$this->translator->addResource(
'php', $path, basename($path, '.php'), 'behat.definitions'
);
} else {
throw new \InvalidArgumentException(sprintf(
'Can not read definitions translations from file "%s". File is not supported',
$path
));
}
}
}
Expand Down

0 comments on commit 8b3a0bb

Please sign in to comment.