diff --git a/.travis.yml b/.travis.yml index ed2e50c..92d4ec4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,19 @@ language: php +addons: + apt: + packages: + - php-pear + - libyaml-dev + php: - - "5.6" - "7.0" before_script: - composer self-update - composer install --no-scripts --no-interaction - composer dump-autoload -o + - yes '' | pecl install yaml-2.0.0 after_script: - php vendor/bin/coveralls -v diff --git a/Command/CheckTranslationProgressCommand.php b/Command/CheckTranslationProgressCommand.php index 7755c9f..4ccbd2f 100644 --- a/Command/CheckTranslationProgressCommand.php +++ b/Command/CheckTranslationProgressCommand.php @@ -2,6 +2,7 @@ namespace OpenClassrooms\Bundle\OneSkyBundle\Command; +use OpenClassrooms\Bundle\OneSkyBundle\Gateways\LanguageNotFoundException; use OpenClassrooms\Bundle\OneSkyBundle\Model\Language; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Helper\Table; @@ -22,13 +23,8 @@ protected function configure() { $this->setName($this->getCommandName()) ->setDescription($this->getCommandDescription()) - ->addOption( - 'locale', - null, - InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, - 'Requested locales', - [] - ); + ->addOption('projectId', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Requested projectsIds', []) + ->addOption('locale', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Source locale', []); } /** @@ -44,19 +40,54 @@ protected function getCommandDescription() return self::COMMAND_DESCRIPTION; } + private function getLocales($project, array $locales) + { + if(empty($locales)) + return $project["locales"]; + else + return $locales; + } + + private function getProjects(array $projects, array $projectsIds) + { + if(empty($projectsIds)) + return $projects; + else + { + foreach ($projects as $projectId => &$project) + if(!in_array($projectId, $projectsIds)) + unset($projects[$projectId]); + return $projects; + } + } + protected function execute(InputInterface $input, OutputInterface $output) { + $projects = $this->getProjects($this->getContainer()->getParameter('openclassrooms_onesky.projects'), $input->getOption('projectId')); $output->writeln('Check translations progress'); - $languages = $this->getContainer() - ->get('openclassrooms.onesky.services.language_service') - ->getLanguages($input->getOption('locale')); + $languages = []; + foreach ($projects as $projectId => &$project) { + try { + $projectLanguages = $this->getContainer() + ->get('openclassrooms.onesky.services.language_service') + ->getLanguages($project, $this->getLocales($project, $input->getOption('locale'))); + $languages = array_merge($languages, $projectLanguages); + } catch(LanguageNotFoundException $e) { + if(!empty($input->getOption('locale'))) + $output->writeln('Language '.$input->getOption('locale').' not found for project '.$projectId.''); + else + throw $e; + } + } + if(empty($languages)) + throw new LanguageNotFoundException(); $table = new Table($output); $table - ->setHeaders(['Locale', 'Progression']) + ->setHeaders(['Project', 'Locale', 'Progression']) ->setRows( array_map( function (Language $language) { - return [$language->getLocale(), $language->getTranslationProgress()]; + return [$language->getProjectId(), $language->getLocale(), $language->getTranslationProgress()]; }, $languages ) @@ -68,7 +99,6 @@ function (Language $language) { return 1; } } - return 0; } } diff --git a/Command/Command.php b/Command/Command.php index 0718f88..fe24df8 100644 --- a/Command/Command.php +++ b/Command/Command.php @@ -49,7 +49,7 @@ protected function handlePullDisplay(OutputInterface $output) $dispatcher->addListener( TranslationPrePullEvent::getEventName(), function (TranslationPrePullEvent $event) use ($output) { - $output->writeln('Pulling for project id '.$this->getProjectId()."\n"); + $output->writeln("Pulling files \n"); $this->progressBar = new ProgressBar($output, $event->getExportFilesCount()); $this->progressBar->setFormat(PROGRESS_BAR_FORMAT); $this->getProgressBar()->start(); @@ -73,11 +73,11 @@ function (TranslationPostPullEvent $event) use ($output) { $output->writeln(''.count($event->getDownloadedFiles()).' files downloaded. '); $table = new Table($output); $table - ->setHeaders(['File', 'Locale']) + ->setHeaders(['Project', 'File', 'Locale']) ->setRows( array_map( function (ExportFile $file) { - return [$file->getSourceFilePathRelativeToProject(), $file->getRequestedLocale()]; + return [$file->getProjectId(), $file->getSourceFilePathRelativeToProject(), $file->getRequestedLocale()]; }, $event->getDownloadedFiles() ) @@ -87,14 +87,6 @@ function (ExportFile $file) { ); } - /** - * @return string - */ - private function getProjectId() - { - return $this->getContainer()->getParameter('openclassrooms_onesky.project_id'); - } - /** * @return ProgressBar */ @@ -109,7 +101,7 @@ protected function handlePushDisplay(OutputInterface $output) $dispatcher->addListener( TranslationPrePushEvent::getEventName(), function (TranslationPrePushEvent $event) use ($output) { - $output->writeln('Pushing for project id '.$this->getProjectId()."\n"); + $output->writeln("Pushing files \n"); $this->progressBar = new ProgressBar($output, $event->getUploadFilesCount()); $this->progressBar->setFormat(PROGRESS_BAR_FORMAT); $this->getProgressBar()->start(); @@ -132,11 +124,11 @@ function (TranslationPostPushEvent $event) use ($output) { $output->writeln(''.count($event->getUploadedFiles()).' files downloaded. '); $table = new Table($output); $table - ->setHeaders(['File', 'Locale']) + ->setHeaders(['Project', 'File', 'Locale']) ->setRows( array_map( function (UploadFile $file) { - return [$file->getSourceFilePathRelativeToProject(), $file->getSourceLocale()]; + return [$file->getProjectId(), $file->getSourceFilePathRelativeToProject(), $file->getSourceLocale()]; }, $event->getUploadedFiles() ) diff --git a/Command/PullCommand.php b/Command/PullCommand.php index 63066d0..312c08c 100644 --- a/Command/PullCommand.php +++ b/Command/PullCommand.php @@ -19,14 +19,9 @@ protected function configure() { $this->setName($this->getCommandName()) ->setDescription($this->getCommandDescription()) + ->addOption('projectId', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Requested projectsIds', []) ->addOption('filePath', 'dir', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'File paths', []) - ->addOption( - 'locale', - null, - InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, - 'Requested requestedLocale', - [] - ); + ->addOption('locale', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Requested requestedLocale', []); } /** @@ -46,6 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $this->handlePullDisplay($output); $this->getContainer()->get('openclassrooms.onesky.services.translation_service')->pull( + $input->getOption('projectId'), $input->getOption('filePath'), $input->getOption('locale') ); diff --git a/Command/PushCommand.php b/Command/PushCommand.php index 92378e5..d26abb5 100644 --- a/Command/PushCommand.php +++ b/Command/PushCommand.php @@ -19,14 +19,9 @@ protected function configure() { $this->setName($this->getCommandName()) ->setDescription($this->getCommandDescription()) - ->addOption('filePath', 'dir', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'File path', []) - ->addOption( - 'locale', - null, - InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, - 'Source locale', - [] - ); + ->addOption('projectId', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Requested projectsIds', []) + ->addOption('filePath', 'dir', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'File paths', []) + ->addOption('locale', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Source locale', []); } /** @@ -46,6 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $this->handlePushDisplay($output); $this->getContainer()->get('openclassrooms.onesky.services.translation_service')->push( + $input->getOption('projectId'), $input->getOption('filePath'), $input->getOption('locale') ); diff --git a/Command/UpdateCommand.php b/Command/UpdateCommand.php index 79ecc73..d86c323 100644 --- a/Command/UpdateCommand.php +++ b/Command/UpdateCommand.php @@ -3,6 +3,7 @@ namespace OpenClassrooms\Bundle\OneSkyBundle\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** @@ -17,7 +18,10 @@ class UpdateCommand extends Command protected function configure() { $this->setName($this->getCommandName()) - ->setDescription($this->getCommandDescription()); + ->setDescription($this->getCommandDescription()) + ->addOption('projectId', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Requested projectsIds', []) + ->addOption('filePath', 'dir', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'File paths', []) + ->addOption('locale', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Source locale', []); } /** @@ -38,6 +42,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln("Updating translations\n"); $this->handlePullDisplay($output); $this->handlePushDisplay($output); - $this->getContainer()->get('openclassrooms.onesky.services.translation_service')->update(); + $this->getContainer()->get('openclassrooms.onesky.services.translation_service')->update( + $input->getOption('projectId'), + $input->getOption('filePath'), + $input->getOption('locale') + ); } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index d69afb8..d101179 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -20,13 +20,18 @@ public function getConfigTreeBuilder() $rootNode->children() ->scalarNode('api_key')->isRequired()->cannotBeEmpty()->end() ->scalarNode('api_secret')->isRequired()->cannotBeEmpty()->end() - ->scalarNode('project_id')->isRequired()->cannotBeEmpty()->end() - ->scalarNode('file_format')->cannotBeEmpty()->defaultValue('xliff')->end() - ->scalarNode('source_locale')->cannotBeEmpty()->defaultValue('en')->end() - ->arrayNode('locales')->isRequired()->cannotBeEmpty()->prototype('scalar')->end()->end() - ->arrayNode('file_paths')->isRequired()->cannotBeEmpty()->prototype('scalar')->end()->end() - ->scalarNode('keep_all_strings')->cannotBeEmpty()->defaultValue(true)->end() - ->end(); + ->arrayNode('projects') + ->prototype('array') + ->children() + ->scalarNode('file_format')->cannotBeEmpty()->defaultValue('xliff')->end() + ->scalarNode('source_locale')->cannotBeEmpty()->defaultValue('en')->end() + ->arrayNode('file_paths')->requiresAtLeastOneElement()->prototype('scalar')->end()->end() + ->arrayNode('locales')->requiresAtLeastOneElement()->prototype('scalar')->end()->end() + ->scalarNode('keep_all_strings')->cannotBeEmpty()->defaultValue(true)->end() + ->end() + ->end() + ->end() + ->end(); return $treeBuilder; } diff --git a/DependencyInjection/OpenClassroomsOneSkyExtension.php b/DependencyInjection/OpenClassroomsOneSkyExtension.php index d712834..43c8650 100644 --- a/DependencyInjection/OpenClassroomsOneSkyExtension.php +++ b/DependencyInjection/OpenClassroomsOneSkyExtension.php @@ -27,12 +27,7 @@ private function processParameters(ContainerBuilder $container, array $config) { $container->setParameter('openclassrooms_onesky.api_key', $config['api_key']); $container->setParameter('openclassrooms_onesky.api_secret', $config['api_secret']); - $container->setParameter('openclassrooms_onesky.project_id', $config['project_id']); - $container->setParameter('openclassrooms_onesky.source_locale', $config['source_locale']); - $container->setParameter('openclassrooms_onesky.locales', $config['locales']); - $container->setParameter('openclassrooms_onesky.file_format', $config['file_format']); - $container->setParameter('openclassrooms_onesky.file_paths', $config['file_paths']); - $container->setParameter('openclassrooms_onesky.keep_all_strings', $config['keep_all_strings']); + $container->setParameter('openclassrooms_onesky.projects', $config['projects']); } /** diff --git a/Gateways/Impl/FileGatewayImpl.php b/Gateways/Impl/FileGatewayImpl.php index 5891323..eb8fc00 100644 --- a/Gateways/Impl/FileGatewayImpl.php +++ b/Gateways/Impl/FileGatewayImpl.php @@ -2,7 +2,6 @@ namespace OpenClassrooms\Bundle\OneSkyBundle\Gateways\Impl; -use Guzzle\Http\Exception\ServerErrorResponseException; use Onesky\Api\Client; use OpenClassrooms\Bundle\OneSkyBundle\EventListener\TranslationDownloadTranslationEvent; use OpenClassrooms\Bundle\OneSkyBundle\EventListener\TranslationUploadTranslationEvent; @@ -12,6 +11,8 @@ use OpenClassrooms\Bundle\OneSkyBundle\Model\ExportFile; use OpenClassrooms\Bundle\OneSkyBundle\Model\UploadFile; use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\Yaml\Yaml as SFYaml; +use Symfony\Component\HttpKernel\Exception\HttpException; /** * @author Romain Kuzniak @@ -58,7 +59,16 @@ private function downloadTranslation(ExportFile $file) ); $downloadedContent = $this->client->translations(self::DOWNLOAD_METHOD, $file->format()); $this->checkTranslation($downloadedContent, $file); - file_put_contents($file->getTargetFilePath(), $downloadedContent); + if(!empty($downloadedContent)) { + if ($file->getFormat() != "yml") + file_put_contents($file->getTargetFilePath(), $downloadedContent); + else if (is_callable("yaml_parse")) + file_put_contents($file->getTargetFilePath(), SFYaml::dump(yaml_parse($downloadedContent))); + else + throw new HttpException(500, "PHP YAML extension is needed for YAML files ( https://pecl.php.net/package/yaml )"); + } + else + file_put_contents($file->getTargetFilePath(), $downloadedContent); return $file; } @@ -75,7 +85,7 @@ private function checkTranslation($downloadedContent, ExportFile $file) throw new NonExistingTranslationException($file->getTargetFilePath()); } if (500 === $json['meta']['status']) { - throw new ServerErrorResponseException($file->getTargetFilePath()); + throw new HttpException(500, "Got 500 error receiving ".$file->getTargetFilePath()); } throw new InvalidContentException($downloadedContent); } @@ -103,6 +113,7 @@ private function uploadTranslation(UploadFile $file) TranslationUploadTranslationEvent::getEventName(), new TranslationUploadTranslationEvent($file) ); + $this->client->files(self::UPLOAD_METHOD, $file->format()); return $file; diff --git a/Gateways/Impl/LanguageGatewayImpl.php b/Gateways/Impl/LanguageGatewayImpl.php index 45db21d..14957bb 100644 --- a/Gateways/Impl/LanguageGatewayImpl.php +++ b/Gateways/Impl/LanguageGatewayImpl.php @@ -24,25 +24,20 @@ class LanguageGatewayImpl implements LanguageGateway */ private $languageFactory; - /** - * @var int - */ - private $projectId; - /** * @return \OpenClassrooms\Bundle\OneSkyBundle\Model\Language[] * * @throws LanguageException * @throws LanguageNotFoundException */ - public function findLanguages(array $locales) + public function findLanguages(array $locales, array $project) { - $jsonResponse = $this->client->projects(self::LANGUAGES_METHOD, ['project_id' => $this->projectId]); + $jsonResponse = $this->client->projects(self::LANGUAGES_METHOD, ['project_id' => $project["id"]]); $response = json_decode($jsonResponse, true); $this->checkResponse($response, $jsonResponse); - $languages = $this->createLanguages($response); + $languages = $this->createLanguages($response, $project["id"]); $requestedLanguages = []; foreach ($locales as $locale) { if (isset($languages[$locale])) { @@ -51,7 +46,6 @@ public function findLanguages(array $locales) throw new LanguageNotFoundException($locale); } } - return $requestedLanguages; } @@ -68,9 +62,9 @@ private function checkResponse($response, $jsonResponse) /** * @return \OpenClassrooms\Bundle\OneSkyBundle\Model\Language[] */ - private function createLanguages($response) + private function createLanguages($response, $projectId) { - $languages = $this->languageFactory->createFromCollection($response['data']); + $languages = $this->languageFactory->createFromCollection($response['data'], $projectId); return $this->formatLanguages($languages); } @@ -99,9 +93,4 @@ public function setLanguageFactory(LanguageFactory $languageFactory) { $this->languageFactory = $languageFactory; } - - public function setProjectId($projectId) - { - $this->projectId = $projectId; - } } diff --git a/Gateways/LanguageGateway.php b/Gateways/LanguageGateway.php index dcae2a7..14ccf2e 100644 --- a/Gateways/LanguageGateway.php +++ b/Gateways/LanguageGateway.php @@ -14,5 +14,5 @@ interface LanguageGateway * * @throws LanguageException */ - public function findLanguages(array $locales); + public function findLanguages(array $locales, array $project); } diff --git a/Model/ExportFile.php b/Model/ExportFile.php index 23f17ea..24b3671 100644 --- a/Model/ExportFile.php +++ b/Model/ExportFile.php @@ -16,10 +16,9 @@ abstract class ExportFile extends File */ protected $requestedLocale; - public function __construct($projectId, $sourceFilePath, $projectDirectory, $requestedLocale) + public function __construct($project, $sourceFilePath, $projectDirectory, $requestedLocale) { - parent::__construct($projectId, $sourceFilePath, $projectDirectory); - $this->projectId = $projectId; + parent::__construct($project, $sourceFilePath, $projectDirectory); $this->requestedLocale = $requestedLocale; } @@ -59,9 +58,12 @@ public function getTargetFilePathRelativeToProject() public function format() { return [ + self::FILE_FORMAT => $this->fileFormat, self::PROJECT_ID => $this->projectId, self::REQUESTED_LOCALE => $this->requestedLocale, self::REQUESTED_SOURCE_FILE_NAME => $this->getEncodedSourceFileName(), ]; } + + } diff --git a/Model/File.php b/Model/File.php index 531a80b..d4ecb16 100644 --- a/Model/File.php +++ b/Model/File.php @@ -11,6 +11,8 @@ abstract class File const PROJECT_ID = 'project_id'; + const FILE_FORMAT = 'file_format'; + const SOURCE_FILE_PATH = 'file'; /** @@ -28,12 +30,19 @@ abstract class File */ protected $sourceFilePathRelativeToProject; + + /** + * @var string + */ + protected $fileFormat; + /** * {@inheritdoc} */ - public function __construct($projectId, $sourceFilePath, $projectDirectory) + public function __construct($project, $sourceFilePath, $projectDirectory) { - $this->projectId = $projectId; + $this->fileFormat = $project["file_format"]; + $this->projectId = $project["id"]; $this->sourceFilePath = realpath($sourceFilePath); $this->sourceFilePathRelativeToProject = str_replace(realpath($projectDirectory), '', $this->sourceFilePath); } @@ -58,4 +67,21 @@ public function getEncodedSourceFileName() { return str_replace(DIRECTORY_SEPARATOR, self::FILENAME_SEPARATOR, $this->sourceFilePathRelativeToProject); } + + + /** + * @return int + */ + public function getProjectId() + { + return $this->projectId; + } + + /** + * @return mixed + */ + public function getFormat() + { + return $this->fileFormat; + } } diff --git a/Model/FileFactory.php b/Model/FileFactory.php index af8b76e..506b0b6 100644 --- a/Model/FileFactory.php +++ b/Model/FileFactory.php @@ -10,10 +10,10 @@ interface FileFactory /** * @return ExportFile */ - public function createExportFile($sourceFilePath, $requestedLocale); + public function createExportFile($sourceFilePath, $project, $requestedLocale); /** * @return UploadFile */ - public function createUploadFile($filePath, $locale = null); + public function createUploadFile($filePath, $project, $locale); } diff --git a/Model/Impl/FileFactoryImpl.php b/Model/Impl/FileFactoryImpl.php index 9e96347..a7f4d45 100644 --- a/Model/Impl/FileFactoryImpl.php +++ b/Model/Impl/FileFactoryImpl.php @@ -10,37 +10,18 @@ */ class FileFactoryImpl implements FileFactory { - /** - * @var string - */ - private $fileFormat; /** * @var string */ private $kernelRootDir; - /** - * @var int - */ - private $projectId; - - /** - * @var string - */ - private $sourceLocale; - - /** - * @var bool - */ - private $isKeepingAllStrings; - /** * @return ExportFile */ - public function createExportFile($sourceFilePath, $requestedLocale) + public function createExportFile($sourceFilePath, $project, $requestedLocale) { - return new ExportFileImpl($this->projectId, $sourceFilePath, $this->getProjectDirectory(), $requestedLocale); + return new ExportFileImpl($project, $sourceFilePath, $this->getProjectDirectory(), $requestedLocale); } /** @@ -54,43 +35,21 @@ private function getProjectDirectory() /** * {@inheritdoc} */ - public function createUploadFile($filePath, $locale = null) + public function createUploadFile($filePath, $project, $locale) { $file = new UploadFileImpl( - $this->projectId, + $project, $filePath, $this->getProjectDirectory(), - $this->fileFormat, - empty($locale) ? $this->sourceLocale : $locale + $locale ); - $file->setKeepingAllStrings($this->isKeepingAllStrings); - return $file; } - public function setKeepingAllStrings($isKeepingAllStrings) - { - $this->isKeepingAllStrings = $isKeepingAllStrings; - } - - public function setFileFormat($fileFormat) - { - $this->fileFormat = $fileFormat; - } - public function setKernelRootDir($kernelRootDir) { $this->kernelRootDir = $kernelRootDir; } - public function setProjectId($projectId) - { - $this->projectId = $projectId; - } - - public function setSourceLocale($sourceLocale) - { - $this->sourceLocale = $sourceLocale; - } } diff --git a/Model/Impl/LanguageFactoryImpl.php b/Model/Impl/LanguageFactoryImpl.php index f4d40ad..c2191a5 100644 --- a/Model/Impl/LanguageFactoryImpl.php +++ b/Model/Impl/LanguageFactoryImpl.php @@ -12,11 +12,11 @@ class LanguageFactoryImpl implements LanguageFactory /** * {@inheritdoc} */ - public function createFromCollection(array $data) + public function createFromCollection(array $data, $projectId) { $languages = []; foreach ($data as $item) { - $languages[] = new LanguageImpl($item); + $languages[] = new LanguageImpl($item, $projectId); } return $languages; diff --git a/Model/Impl/LanguageImpl.php b/Model/Impl/LanguageImpl.php index 519d792..a09d2aa 100644 --- a/Model/Impl/LanguageImpl.php +++ b/Model/Impl/LanguageImpl.php @@ -9,9 +9,10 @@ */ class LanguageImpl extends Language { - public function __construct(array $data) + public function __construct(array $data, $projectId) { $this->locale = $data['locale']; $this->translationProgress = $data['translation_progress']; + $this->projectId = $projectId; } } diff --git a/Model/Language.php b/Model/Language.php index 5f1cd58..5f050b3 100644 --- a/Model/Language.php +++ b/Model/Language.php @@ -7,6 +7,11 @@ */ abstract class Language { + /** + * @var string + */ + protected $projectId; + /** * @var string */ @@ -40,4 +45,12 @@ public function getTranslationProgress() { return $this->translationProgress; } + + /** + * @return string + */ + public function getProjectId() + { + return $this->projectId; + } } diff --git a/Model/LanguageFactory.php b/Model/LanguageFactory.php index dcfbfc6..fef7ca0 100644 --- a/Model/LanguageFactory.php +++ b/Model/LanguageFactory.php @@ -10,5 +10,5 @@ interface LanguageFactory /** * @return Language[] */ - public function createFromCollection(array $data); + public function createFromCollection(array $data, $projectId); } diff --git a/Model/UploadFile.php b/Model/UploadFile.php index 10eec67..cb936c8 100644 --- a/Model/UploadFile.php +++ b/Model/UploadFile.php @@ -9,15 +9,8 @@ abstract class UploadFile extends File { const IS_KEEPING_ALL_STRINGS = 'is_keeping_all_strings'; - const FILE_FORMAT = 'file_format'; - const SOURCE_LOCALE = 'locale'; - /** - * @var string - */ - protected $fileFormat; - /** * @var string */ @@ -33,14 +26,13 @@ abstract class UploadFile extends File */ protected $isKeepingAllStrings = true; - public function __construct($projectId, $sourceFilePath, $projectDirectory, $fileFormat, $sourceLocale) + public function __construct($project, $sourceFilePath, $projectDirectory, $sourceLocale) { - parent::__construct($projectId, $sourceFilePath, $projectDirectory); + parent::__construct($project, $sourceFilePath, $projectDirectory); $this->formattedSourceFilePath = sys_get_temp_dir().'/'.$this->getEncodedSourceFileName(); copy($sourceFilePath, $this->formattedSourceFilePath); - - $this->fileFormat = $fileFormat; $this->sourceLocale = $sourceLocale; + $this->isKeepingAllStrings = $project["keep_all_strings"]; } /** @@ -65,8 +57,4 @@ public function getSourceLocale() return $this->sourceLocale; } - public function setKeepingAllStrings($isKeepingAllStrings) - { - $this->isKeepingAllStrings = $isKeepingAllStrings; - } } diff --git a/README.md b/README.md index 0d196d4..55d550f 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,24 @@ or by adding the package to the composer.json file directly: } ``` +If you want to use YAML files, you need the PECL YAML extension : https://pecl.php.net/package/yaml + +#### PHP 7 +``` +apt-get install php-pear libyaml-dev +pecl install yaml-2.0.0 +``` +Add "extension=yaml.so" to php.ini for CLI + +#### PHP 5 +``` +apt-get install php-pear libyaml-dev +pecl install yaml +``` +Add "extension=yaml.so" to php.ini for CLI + + + After the package has been installed, add the bundle to the AppKernel.php file: ```php // in AppKernel::registerBundles() @@ -36,17 +54,23 @@ $bundles = array( # app/config/config.yml openclassrooms_onesky: - api_key: %onesky.api_key% + api_key: %onesky.api_key% api_secret: %onesky.api_secret% - project_id: %onesky.project_id% - source_locale: %source_locale% #optional, default en - locales: - - fr - - es - file_format: %onesky.file_format% #optional, default xliff - file_paths: - - %path.to.translations.files.directory% - keep_all_strings: false # default true + projects: + 1: + file_format: yml + source_locale: en + locales: [en,fr,ja] + keep_all_strings: 0 + file_paths: + - "%kernel.root_dir%/../src/Bundle/DemoBundle/Resources/translations" + 2: + file_format: xliff + source_locale: fr + locales: [fr,es] + keep_all_strings: 1 + file_paths: + - "%kernel.root_dir%/../app/Resources/translations/" ``` @@ -72,6 +96,12 @@ Locale can be set as an option. php app/console openclassrooms:one-sky:pull --locale=fr php app/console openclassrooms:one-sky:pull --locale=fr --locale=es ``` +##### Project id + Project id can be set as an option. +```bash +php app/console openclassrooms:one-sky:pull --projectId=1 +php app/console openclassrooms:one-sky:pull --projectId=1 --projectId=2 +``` ### Push Push the translations from the OneSky API using the default configuration. @@ -94,6 +124,12 @@ Locale can be set as an option. php app/console openclassrooms:one-sky:push --locale=en php app/console openclassrooms:one-sky:push --locale=en --locale=fr ``` +##### Project id + Project id can be set as an option. +```bash +php app/console openclassrooms:one-sky:push --projectId=1 +php app/console openclassrooms:one-sky:push --projectId=1 --projectId=2 +``` ### Update Pull then push translations from the OneSky API using the default configuration. @@ -118,3 +154,10 @@ Locale can be set as an option. php app/console openclassrooms:one-sky:check-translation-progress --locale=en php app/console openclassrooms:one-sky:check-translation-progress --locale=en --locale=fr ``` +##### Project id + Project id can be set as an option. +```bash +php app/console openclassrooms:one-sky:check-translation-progress --projectId=1 +php app/console openclassrooms:one-sky:check-translation-progress --projectId=1 --projectId=2 +``` + diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 16894ec..45f18e2 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -24,26 +24,11 @@ - - %openclassrooms_onesky.project_id% - - - %openclassrooms_onesky.keep_all_strings% - - - %openclassrooms_onesky.file_format% - %kernel.root_dir% - - %openclassrooms_onesky.project_id% - - - %openclassrooms_onesky.source_locale% - @@ -55,9 +40,6 @@ - - %openclassrooms_onesky.locales% - @@ -66,21 +48,12 @@ - - %openclassrooms_onesky.file_format% - - - %openclassrooms_onesky.file_paths% + + %openclassrooms_onesky.projects% - - %openclassrooms_onesky.locales% - - - %openclassrooms_onesky.source_locale% - diff --git a/Services/Impl/LanguageServiceImpl.php b/Services/Impl/LanguageServiceImpl.php index 3e5b28d..b5bdf68 100644 --- a/Services/Impl/LanguageServiceImpl.php +++ b/Services/Impl/LanguageServiceImpl.php @@ -15,30 +15,16 @@ class LanguageServiceImpl implements LanguageService */ private $languageGateway; - /** - * @var string[] - */ - private $requestedLocales; - /** * @return \OpenClassrooms\Bundle\OneSkyBundle\Model\Language[] */ - public function getLanguages(array $locales = []) + public function getLanguages(array $project, array $locales) { - if (empty($locales)) { - $locales = $this->requestedLocales; - } - - return $this->languageGateway->findLanguages($locales); + return $this->languageGateway->findLanguages($locales, $project); } public function setLanguageGateway(LanguageGateway $languageGateway) { $this->languageGateway = $languageGateway; } - - public function setRequestedLocales(array $requestedLocales) - { - $this->requestedLocales = $requestedLocales; - } } diff --git a/Services/Impl/TranslationServiceImpl.php b/Services/Impl/TranslationServiceImpl.php index e0943b1..5b77fa5 100644 --- a/Services/Impl/TranslationServiceImpl.php +++ b/Services/Impl/TranslationServiceImpl.php @@ -27,56 +27,41 @@ class TranslationServiceImpl implements TranslationService /** * @var string[] */ - private $filePaths; + private $projects; /** * @var FileFactory */ private $fileFactory; - /** - * @var string - */ - private $fileFormat; - /** * @var FileService */ private $fileService; - /** - * @var string[] - */ - private $requestedLocales; - - /** - * @var string - */ - private $sourceLocale; - /** * {@inheritdoc} */ - public function update(array $filePaths = [], array $locales = []) + public function update(array $projectsIds, array $filePaths = [], array $locales = []) { $this->eventDispatcher->dispatch(TranslationUpdateEvent::getEventName(), new TranslationUpdateEvent()); - return [$this->pull($filePaths, $locales), $this->push($filePaths)]; + return [$this->push($projectsIds, $filePaths), $this->pull($projectsIds, $filePaths, $locales)]; } /** * {@inheritdoc} */ - public function pull(array $filePaths, array $locales = []) + public function pull(array $projectsIds, array $filePaths, array $locales = []) { $exportFiles = []; - /** @var SplFileInfo $file */ - foreach ($this->getFiles($filePaths, $this->getSourceLocales()) as $file) { - foreach ($this->getRequestedLocales($locales) as $locale) { - $exportFiles[] = $this->fileFactory->createExportFile($file->getRealPath(), $locale); + foreach ($this->getProjectsIds($projectsIds) as $projectId) { + foreach ($this->getFiles($this->getFilePaths($filePaths, $projectId), $this->projects[$projectId]["source_locale"], $projectId) as $file) { + foreach ($this->getRequestedLocales($locales, $projectId) as $locale) { + $exportFiles[] = $this->fileFactory->createExportFile($file->getRealPath(), $this->projects[$projectId], $locale); + } } } - $this->eventDispatcher->dispatch( TranslationPrePullEvent::getEventName(), new TranslationPrePullEvent($exportFiles) @@ -93,50 +78,80 @@ public function pull(array $filePaths, array $locales = []) } /** - * @return Finder + * @return */ - private function getFiles(array $filePaths, array $locales) + private function getFiles(array $paths, string $locale, $projectId) { - return Finder::create() - ->files() - ->in($this->getFilePaths($filePaths)) - ->name('*.{'.implode(',', $locales).'}.'.$this->fileFormat); + if(empty($paths)) + return array(); + else + return Finder::create() + ->files() + ->in($paths) + ->name('*.'.$locale.'.'.$this->projects[$projectId]["file_format"]); } /** * @return string[] */ - private function getFilePaths(array $filePaths) + private function getFilePaths(array $filePaths, $projectId) + { + if(empty($filePaths)) + return $this->projects[$projectId]["file_paths"]; + else { + foreach ($filePaths as $key => &$filePath) { + $pathFound = false; + foreach ($this->projects[$projectId]["file_paths"] as $projectFilePath) + if (strpos($filePath, $projectFilePath) === 0) + $pathFound = true; + if(!$pathFound) + unset($filePaths[$key]); + } + return $filePaths; + } + } + /** + * @return string[] + */ + private function getProjectsIds($projectsIds) { - return empty($filePaths) ? $this->filePaths : $filePaths; + if(empty($projectsIds)) + return array_keys($this->projects); + else + return $projectsIds; } /** * @return string[] */ - private function getSourceLocales(array $locales = []) + private function getSourceLocales(array $locales = [], $projectId) { - return empty($locales) ? [$this->sourceLocale] : $locales; + if(empty($locales)) + return [$this->projects[$projectId]["source_locale"]]; + else + return $locales; } /** * @return string[] */ - private function getRequestedLocales(array $locales) + private function getRequestedLocales(array $locales, $projectId) { - return empty($locales) ? $this->requestedLocales : $locales; + return empty($locales) ? $this->projects[$projectId]["locales"] : $locales; } /** * {@inheritdoc} */ - public function push(array $filePaths, array $locales = []) + public function push(array $projectsIds, array $filePaths, array $locales = []) { $uploadFiles = []; /* @var SplFileInfo $file */ - foreach ($this->getSourceLocales($locales) as $locale) { - foreach ($this->getFiles($filePaths, [$locale]) as $file) { - $uploadFiles[] = $this->fileFactory->createUploadFile($file->getRealPath(), $locale); + foreach ($this->getProjectsIds($projectsIds) as $projectId) { + foreach ($this->getSourceLocales($locales, $projectId) as $locale) { + foreach ($this->getFiles($this->getFilePaths($filePaths, $projectId), $locale, $projectId) as $file) { + $uploadFiles[] = $this->fileFactory->createUploadFile($file->getRealPath(), $this->projects[$projectId], $locale); + } } } @@ -165,28 +180,18 @@ public function setFileFactory(FileFactory $fileFactory) $this->fileFactory = $fileFactory; } - public function setFileFormat($fileFormat) - { - $this->fileFormat = $fileFormat; - } - - public function setFilePaths(array $filePaths) - { - $this->filePaths = $filePaths; - } public function setFileService(FileService $fileService) { $this->fileService = $fileService; } - public function setRequestedLocales(array $requestedLocales) + public function setProjects($projects) { - $this->requestedLocales = $requestedLocales; + foreach ($projects as $projectId => &$project) + $project["id"] = $projectId; + $this->projects = $projects; } - public function setSourceLocale($sourceLocale) - { - $this->sourceLocale = $sourceLocale; - } } + diff --git a/Services/LanguageService.php b/Services/LanguageService.php index 1f79183..5bc6a5c 100644 --- a/Services/LanguageService.php +++ b/Services/LanguageService.php @@ -10,5 +10,5 @@ interface LanguageService /** * @return \OpenClassrooms\Bundle\OneSkyBundle\Model\Language[] */ - public function getLanguages(array $locales = []); + public function getLanguages(array $project, array $locales); } diff --git a/Services/TranslationService.php b/Services/TranslationService.php index 5e829cc..0aa712c 100644 --- a/Services/TranslationService.php +++ b/Services/TranslationService.php @@ -11,24 +11,27 @@ interface TranslationService { /** + * @param integer $projectId * @param string[] $filePaths * @param string[] $locales * * @return ExportFile[] $files */ - public function pull(array $filePaths, array $locales = []); + public function pull(array $projectsIds, array $filePaths, array $locales = []); /** + * @param integer $projectId * @param string[] $filePaths * * @return UploadFile[] $files */ - public function push(array $filePaths, array $locales = []); + public function push(array $projectsIds, array $filePaths, array $locales = []); /** + * @param integer $projectId * @param string[] $filePaths * * @return [ExportFile[], UploadFile[]] $files */ - public function update(array $filePaths, array $locales = []); + public function update(array $projectsIds, array $filePaths, array $locales = []); } diff --git a/Tests/Command/CheckTranslationProgressCommandTest.php b/Tests/Command/CheckTranslationProgressCommandTest.php index 0d38e50..a092506 100644 --- a/Tests/Command/CheckTranslationProgressCommandTest.php +++ b/Tests/Command/CheckTranslationProgressCommandTest.php @@ -5,6 +5,8 @@ use OpenClassrooms\Bundle\OneSkyBundle\Command\CheckTranslationProgressCommand; use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Model\LanguageStub1; use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Model\LanguageStub2; +use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Model\LanguageStub3; +use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Model\ProjectsStub; use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Services\LanguageServiceMock; use Symfony\Component\Console\Application; use Symfony\Component\Console\Tester\CommandTester; @@ -26,8 +28,12 @@ class CheckTranslationProgressCommandTest extends \PHPUnit_Framework_TestCase */ public function without_locales_execute() { + LanguageServiceMock::$languages = [1 => [new LanguageStub1(), new LanguageStub2()], 2 => [new LanguageStub3()]]; $this->commandTester->execute(['command' => CheckTranslationProgressCommand::COMMAND_NAME]); - $this->assertEquals([], LanguageServiceMock::$locales); + $wantedLocales = array(); + foreach (ProjectsStub::$projects as $projectId => $project) + $wantedLocales[$projectId] = $project["locales"]; + $this->assertEquals($wantedLocales, LanguageServiceMock::$locales); $this->assertTrue(LanguageServiceMock::$calledGetLanguages); } @@ -36,11 +42,11 @@ public function without_locales_execute() */ public function with_locales_execute() { - LanguageServiceMock::$languages = [new LanguageStub1(), new LanguageStub2()]; + LanguageServiceMock::$languages = [1 => [new LanguageStub1(), new LanguageStub2()], 2 => [new LanguageStub3()]]; $exitCode = $this->commandTester->execute( ['command' => CheckTranslationProgressCommand::COMMAND_NAME, '--locale' => [LanguageStub2::LOCALE]] ); - $this->assertEquals([LanguageStub2::LOCALE], LanguageServiceMock::$locales); + $this->assertEquals([ 1 => [LanguageStub2::LOCALE], 2 => [LanguageStub3::LOCALE]], LanguageServiceMock::$locales); $this->assertTrue(LanguageServiceMock::$calledGetLanguages); $this->assertEquals(1, $exitCode); } @@ -50,11 +56,11 @@ public function with_locales_execute() */ public function WithFullProgression() { - LanguageServiceMock::$languages = [new LanguageStub1()]; + LanguageServiceMock::$languages = [ 1 => [new LanguageStub1()], 2 => [new LanguageStub3()]]; $exitCode = $this->commandTester->execute( ['command' => CheckTranslationProgressCommand::COMMAND_NAME, '--locale' => [LanguageStub1::LOCALE]] ); - $this->assertEquals([LanguageStub1::LOCALE], LanguageServiceMock::$locales); + $this->assertEquals([ 1 => [LanguageStub1::LOCALE]], LanguageServiceMock::$locales); $this->assertTrue(LanguageServiceMock::$calledGetLanguages); $this->assertEquals(0, $exitCode); } diff --git a/Tests/Command/CommandTestCase.php b/Tests/Command/CommandTestCase.php index 74ebdab..a513a94 100644 --- a/Tests/Command/CommandTestCase.php +++ b/Tests/Command/CommandTestCase.php @@ -3,6 +3,7 @@ namespace OpenClassrooms\Bundle\OneSkyBundle\Tests\Command; use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Framework\DependencyInjection\ContainerForTest; +use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Model\ProjectsStub; use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Services\LanguageServiceMock; use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Services\TranslationServiceMock; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -18,15 +19,6 @@ trait CommandTestCase */ public static $filePaths = 'Tests/Fixtures/Resources/translations'; - /** - * @var string[] - */ - public static $locales = ['fr']; - - /** - * @var int - */ - public static $projectId = 1; /** * @return ContainerInterface @@ -37,9 +29,7 @@ protected function getContainer() return new ContainerForTest( [ - 'openclassrooms_onesky.file_paths' => [self::$filePaths], - 'openclassrooms_onesky.requestedLocales' => self::$locales, - 'openclassrooms_onesky.project_id' => self::$projectId, + 'openclassrooms_onesky.projects' => ProjectsStub::$projects, 'kernel.root_dir' => __DIR__.'/../', ], [ diff --git a/Tests/Command/PullCommandTest.php b/Tests/Command/PullCommandTest.php index a93892b..c4ed232 100644 --- a/Tests/Command/PullCommandTest.php +++ b/Tests/Command/PullCommandTest.php @@ -33,8 +33,8 @@ public function without_locales_execute() */ public function with_locales_execute() { - $this->commandTester->execute(['command' => PullCommand::COMMAND_NAME, '--locale' => ['es']]); - $this->assertEquals(['es'], TranslationServiceMock::$locales); + $this->commandTester->execute(['command' => PullCommand::COMMAND_NAME, '--locale' => ['ja']]); + $this->assertEquals(['ja'], TranslationServiceMock::$locales); } /** @@ -51,7 +51,7 @@ public function without_filePaths_execute() */ public function with_filePath_execute() { - $this->commandTester->execute(['command' => PullCommand::COMMAND_NAME, '--filePath' => [self::$filePaths]]); + $this->commandTester->execute(['command' => PullCommand::COMMAND_NAME, '--projectId' => [1], '--filePath' => [self::$filePaths]]); $this->assertEquals([self::$filePaths], TranslationServiceMock::$pulledFilePaths); } diff --git a/Tests/Command/PushCommandTest.php b/Tests/Command/PushCommandTest.php index 2fec952..fe8d101 100644 --- a/Tests/Command/PushCommandTest.php +++ b/Tests/Command/PushCommandTest.php @@ -33,8 +33,8 @@ public function without_locales_execute() */ public function with_locales_execute() { - $this->commandTester->execute(['command' => PushCommand::COMMAND_NAME, '--locale' => ['es']]); - $this->assertEquals(['es'], TranslationServiceMock::$locales); + $this->commandTester->execute(['command' => PushCommand::COMMAND_NAME, '--locale' => ['ja']]); + $this->assertEquals(['ja'], TranslationServiceMock::$locales); } /** @@ -51,7 +51,7 @@ public function without_filePaths_execute() */ public function with_filePath_execute() { - $this->commandTester->execute(['command' => PushCommand::COMMAND_NAME, '--filePath' => [self::$filePaths]]); + $this->commandTester->execute(['command' => PushCommand::COMMAND_NAME, '--projectId' => [1], '--filePath' => [self::$filePaths]]); $this->assertEquals([self::$filePaths], TranslationServiceMock::$pushedFilePaths); } diff --git a/Tests/DependencyInjection/OpenClassroomsOneSkyExtensionTest.php b/Tests/DependencyInjection/OpenClassroomsOneSkyExtensionTest.php index 5e7010a..23df918 100644 --- a/Tests/DependencyInjection/OpenClassroomsOneSkyExtensionTest.php +++ b/Tests/DependencyInjection/OpenClassroomsOneSkyExtensionTest.php @@ -19,7 +19,8 @@ public function container_compile() { $container = new ContainerBuilder(); $bundle = new OpenClassroomsOneSkyBundle(); - $container->registerExtension($bundle->getContainerExtension()); + $extension = $bundle->getContainerExtension(); + $container->registerExtension($extension); $container->loadFromExtension('openclassrooms_onesky'); $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Fixtures/Resources/config')); diff --git a/Tests/Doubles/Framework/DependencyInjection/ContainerForTest.php b/Tests/Doubles/Framework/DependencyInjection/ContainerForTest.php index 20281a4..a431463 100644 --- a/Tests/Doubles/Framework/DependencyInjection/ContainerForTest.php +++ b/Tests/Doubles/Framework/DependencyInjection/ContainerForTest.php @@ -3,6 +3,7 @@ namespace OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Framework\DependencyInjection; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\DependencyInjection\ScopeInterface; /** * @author Romain Kuzniak @@ -28,7 +29,7 @@ public function __construct(array $parameters = [], array $services = []) /** * {@inheritdoc} */ - public function set($id, $service) + public function set($id, $service, $scope = self::SCOPE_CONTAINER) { return; } @@ -80,4 +81,45 @@ public function setParameter($name, $value) { return; } + + /** + * {@inheritdoc} + */ + public function enterScope($name) + { + return; + } + + /** + * {@inheritdoc} + */ + public function leaveScope($name) + { + return; + } + + /** + * {@inheritdoc} + */ + public function addScope(ScopeInterface $scope) + { + return; + } + + /** + * {@inheritdoc} + */ + public function hasScope($name) + { + return; + } + + /** + * {@inheritdoc} + */ + public function isScopeActive($name) + { + return; + } } + diff --git a/Tests/Doubles/Gateways/InMemoryLanguageGateway.php b/Tests/Doubles/Gateways/InMemoryLanguageGateway.php index 5f6a5ff..8b915fe 100644 --- a/Tests/Doubles/Gateways/InMemoryLanguageGateway.php +++ b/Tests/Doubles/Gateways/InMemoryLanguageGateway.php @@ -24,14 +24,14 @@ public function __construct(array $languages = []) /** * {@inheritdoc} */ - public function findLanguages(array $locales = []) + public function findLanguages(array $locales = [], array $project) { $languages = []; foreach ($locales as $locale) { - if (!isset(self::$languages[$locale])) { + if (!isset(self::$languages[$project["id"]][$locale])) { throw new LanguageNotFoundException(); } else { - $languages[] = self::$languages[$locale]; + $languages[$project["id"]][] = self::$languages[$project["id"]][$locale]; } } diff --git a/Tests/Doubles/Model/ExportFileStub.php b/Tests/Doubles/Model/ExportFileStub.php index d3e3310..674b2cd 100644 --- a/Tests/Doubles/Model/ExportFileStub.php +++ b/Tests/Doubles/Model/ExportFileStub.php @@ -9,7 +9,14 @@ */ abstract class ExportFileStub extends ExportFileImpl { - const PROJECT_ID = 1; - + const PROJECT = + [ + "id" => 1, + "file_format" => "yml", + "source_locale" => "en", + "locales" => ["en","fr","ja"], + "keep_all_strings" => 0, + "file_paths" => [ "__DIR__.'/../'Tests/Fixtures/Resources/translations/"] + ]; const PROJECT_DIRECTORY = __DIR__.'/../../../'; } diff --git a/Tests/Doubles/Model/ExportFileStub1.php b/Tests/Doubles/Model/ExportFileStub1.php index 781edd9..db15cb9 100644 --- a/Tests/Doubles/Model/ExportFileStub1.php +++ b/Tests/Doubles/Model/ExportFileStub1.php @@ -13,6 +13,6 @@ class ExportFileStub1 extends ExportFileStub public function __construct() { - parent::__construct(self::PROJECT_ID, self::SOURCE_FILE_PATH, self::PROJECT_DIRECTORY, self::REQUESTED_LOCALE); + parent::__construct(self::PROJECT, self::SOURCE_FILE_PATH, self::PROJECT_DIRECTORY, self::REQUESTED_LOCALE); } } diff --git a/Tests/Doubles/Model/ExportFileStub2.php b/Tests/Doubles/Model/ExportFileStub2.php index 0236b40..a9962c0 100644 --- a/Tests/Doubles/Model/ExportFileStub2.php +++ b/Tests/Doubles/Model/ExportFileStub2.php @@ -13,6 +13,6 @@ class ExportFileStub2 extends ExportFileStub public function __construct() { - parent::__construct(self::PROJECT_ID, self::SOURCE_FILE_PATH, self::PROJECT_DIRECTORY, self::REQUESTED_LOCALE); + parent::__construct(self::PROJECT, self::SOURCE_FILE_PATH, self::PROJECT_DIRECTORY, self::REQUESTED_LOCALE); } } diff --git a/Tests/Doubles/Model/LanguageStub1.php b/Tests/Doubles/Model/LanguageStub1.php index 3d23a49..0e38c89 100644 --- a/Tests/Doubles/Model/LanguageStub1.php +++ b/Tests/Doubles/Model/LanguageStub1.php @@ -17,5 +17,6 @@ class LanguageStub1 extends LanguageImpl public function __construct() { + $this->projectId = ProjectsStub::$projects[1]["id"]; } } diff --git a/Tests/Doubles/Model/LanguageStub2.php b/Tests/Doubles/Model/LanguageStub2.php index 4ca34ba..fa7fc90 100644 --- a/Tests/Doubles/Model/LanguageStub2.php +++ b/Tests/Doubles/Model/LanguageStub2.php @@ -17,5 +17,6 @@ class LanguageStub2 extends LanguageImpl public function __construct() { + $this->projectId = ProjectsStub::$projects[1]["id"]; } } diff --git a/Tests/Doubles/Model/LanguageStub3.php b/Tests/Doubles/Model/LanguageStub3.php new file mode 100644 index 0000000..d710697 --- /dev/null +++ b/Tests/Doubles/Model/LanguageStub3.php @@ -0,0 +1,22 @@ + + */ +class LanguageStub3 extends LanguageImpl +{ + const LOCALE = 'ja'; + + protected $locale = self::LOCALE; + + protected $translationProgress = '98%'; + + public function __construct() + { + $this->projectId = ProjectsStub::$projects[2]["id"]; + } +} diff --git a/Tests/Doubles/Model/ProjectsStub.php b/Tests/Doubles/Model/ProjectsStub.php new file mode 100644 index 0000000..6d076ca --- /dev/null +++ b/Tests/Doubles/Model/ProjectsStub.php @@ -0,0 +1,33 @@ + [ + "id" => 1, + "file_format" => "yml", + "source_locale" => "en", + "locales" => ["en","ja"], + "keep_all_strings" => 0, + "file_paths" => [ __DIR__.'/../../Fixtures/Resources/translations/'] + ], + 2 => [ + "id" => 2, + "file_format" => "yml", + "source_locale" => "ja", + "locales" => ["ja"], + "keep_all_strings" => 0, + "file_paths" => [ __DIR__.'/../../Fixtures/Resources/translations/subDirectory' ] + ] + ]; + +} \ No newline at end of file diff --git a/Tests/Doubles/Model/UploadFileStub.php b/Tests/Doubles/Model/UploadFileStub.php index c77c3ce..89384d5 100644 --- a/Tests/Doubles/Model/UploadFileStub.php +++ b/Tests/Doubles/Model/UploadFileStub.php @@ -9,13 +9,18 @@ */ abstract class UploadFileStub extends UploadFileImpl { - const FILE_FORMAT = 'yaml'; const PROJECT_DIRECTORY = __DIR__.'/../../../'; - const PROJECT_ID = 1; - - const SOURCE_LOCALE = 'en'; + const PROJECT = + [ + "id" => 1, + "file_format" => "yml", + "source_locale" => "en", + "locales" => ["en","fr","ja"], + "keep_all_strings" => 0, + "file_paths" => [ "__DIR__.'/../'Tests/Fixtures/Resources/translations/"] + ]; /** * @return string diff --git a/Tests/Doubles/Model/UploadFileStub1.php b/Tests/Doubles/Model/UploadFileStub1.php index 3bdd429..371c24d 100644 --- a/Tests/Doubles/Model/UploadFileStub1.php +++ b/Tests/Doubles/Model/UploadFileStub1.php @@ -7,19 +7,15 @@ */ class UploadFileStub1 extends UploadFileStub { - const IS_KEEPING_ALL_STRINGS = true; const SOURCE_FILE_PATH = __DIR__.'/../../Fixtures/Resources/translations/messages.en.yml'; - protected $isKeepingAllStrings = self::IS_KEEPING_ALL_STRINGS; - public function __construct() { parent::__construct( - self::PROJECT_ID, + self::PROJECT, self::SOURCE_FILE_PATH, self::PROJECT_DIRECTORY, - self::FILE_FORMAT, self::SOURCE_LOCALE ); } diff --git a/Tests/Doubles/Model/UploadFileStub2.php b/Tests/Doubles/Model/UploadFileStub2.php index 1c956c3..5feb8bc 100644 --- a/Tests/Doubles/Model/UploadFileStub2.php +++ b/Tests/Doubles/Model/UploadFileStub2.php @@ -7,19 +7,14 @@ */ class UploadFileStub2 extends UploadFileStub { - const IS_KEEPING_ALL_STRINGS = false; - const SOURCE_FILE_PATH = __DIR__.'/../../Fixtures/Resources/translations/subDirectory/messages.en.yml'; - protected $isKeepingAllStrings = self::IS_KEEPING_ALL_STRINGS; - public function __construct() { parent::__construct( - self::PROJECT_ID, + self::PROJECT, self::SOURCE_FILE_PATH, self::PROJECT_DIRECTORY, - self::FILE_FORMAT, self::SOURCE_LOCALE ); } diff --git a/Tests/Doubles/Services/LanguageServiceMock.php b/Tests/Doubles/Services/LanguageServiceMock.php index 0dcf533..bdcb8c4 100644 --- a/Tests/Doubles/Services/LanguageServiceMock.php +++ b/Tests/Doubles/Services/LanguageServiceMock.php @@ -35,11 +35,21 @@ public function __construct() /** * {@inheritdoc} */ - public function getLanguages(array $locales = []) + public function getLanguages(array $project, array $locales) { self::$calledGetLanguages = true; - self::$locales = $locales; - - return self::$languages; + foreach ($locales as $locale) { + if(in_array($locale, $project["locales"])) + self::$locales[$project["id"]][] = $locale; + } + if(isset(self::$languages[$project["id"]]) && isset(self::$locales[$project["id"]])) { + $languages = array(); + foreach (self::$languages[$project["id"]] as $language) + if(in_array($language->getLocale(), self::$locales[$project["id"]])) + $languages[] = $language; + return $languages; + } + else + return array(); } } diff --git a/Tests/Doubles/Services/TranslationServiceMock.php b/Tests/Doubles/Services/TranslationServiceMock.php index 88069f6..de5346e 100644 --- a/Tests/Doubles/Services/TranslationServiceMock.php +++ b/Tests/Doubles/Services/TranslationServiceMock.php @@ -73,7 +73,7 @@ public function __construct(EventDispatcherInterface $eventDispatcher) /** * {@inheritdoc} */ - public function pull(array $filePaths, array $locales = []) + public function pull(array $projectsIds, array $filePaths = [], array $locales = []) { $this->eventDispatcher->dispatch( TranslationPrePullEvent::getEventName(), @@ -95,7 +95,7 @@ public function pull(array $filePaths, array $locales = []) /** * {@inheritdoc} */ - public function push(array $filePaths, array $locales = []) + public function push(array $projectsIds, array $filePaths = [], array $locales = []) { $this->eventDispatcher->dispatch( TranslationPrePushEvent::getEventName(), @@ -117,7 +117,7 @@ public function push(array $filePaths, array $locales = []) /** * {@inheritdoc} */ - public function update(array $filePaths = [], array $locales = []) + public function update(array $projectsIds, array $filePaths = [], array $locales = []) { self::$updateCalled = true; self::$updatedFilePaths = $filePaths; diff --git a/Tests/Fixtures/Resources/config/config.yml b/Tests/Fixtures/Resources/config/config.yml index d8fc163..52de664 100644 --- a/Tests/Fixtures/Resources/config/config.yml +++ b/Tests/Fixtures/Resources/config/config.yml @@ -1,11 +1,18 @@ openclassrooms_onesky: api_key: api_key api_secret: api_secret - project_id: 1 - file_format: yml - source_locale: en - locales: - - fr - - es - file_paths: - - / + projects: + 1: + file_format: yml + source_locale: en + locales: [en,fr,ja] + keep_all_strings: 0 + file_paths: + - "%kernel.root_dir%/Tests/Fixtures/Resources/translations/" + 2: + file_format: yml + source_locale: en + locales: [en,fr,es] + keep_all_strings: 0 + file_paths: + - "%kernel.root_dir%/Tests/Fixtures/Resources/translations/subDirectory/" \ No newline at end of file diff --git a/Tests/Fixtures/Resources/translations/messages.ja.yml b/Tests/Fixtures/Resources/translations/messages.ja.yml new file mode 100644 index 0000000..e69de29 diff --git a/Tests/Fixtures/Resources/translations/subDirectory/messages.ja.yml b/Tests/Fixtures/Resources/translations/subDirectory/messages.ja.yml new file mode 100644 index 0000000..e69de29 diff --git a/Tests/Gateways/Impl/FileGatewayImplTest.php b/Tests/Gateways/Impl/FileGatewayImplTest.php index ccf4337..0f4b891 100644 --- a/Tests/Gateways/Impl/FileGatewayImplTest.php +++ b/Tests/Gateways/Impl/FileGatewayImplTest.php @@ -12,6 +12,7 @@ use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Model\UploadFileStub2; use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\OneSky\Api\ClientMock; use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\HttpKernel\Exception\HttpException; /** * @author Romain Kuzniak @@ -48,11 +49,11 @@ public function OneFile_upload() $this->assertEquals( [ [ - UploadFile::PROJECT_ID => UploadFileStub1::PROJECT_ID, + UploadFile::PROJECT_ID => UploadFileStub1::PROJECT["id"], UploadFile::SOURCE_FILE_PATH => $uploadFileStub1->getFormattedFilePath(), - UploadFile::FILE_FORMAT => UploadFileStub1::FILE_FORMAT, + UploadFile::FILE_FORMAT => UploadFileStub1::PROJECT["file_format"], UploadFile::SOURCE_LOCALE => UploadFileStub1::SOURCE_LOCALE, - UploadFile::IS_KEEPING_ALL_STRINGS => UploadFileStub1::IS_KEEPING_ALL_STRINGS, + UploadFile::IS_KEEPING_ALL_STRINGS => UploadFileStub1::PROJECT["keep_all_strings"], ], ], ClientMock::$parameters @@ -72,18 +73,18 @@ public function ManyFiles_upload_Upload() $this->assertEquals( [ [ - UploadFile::PROJECT_ID => UploadFileStub1::PROJECT_ID, + UploadFile::PROJECT_ID => UploadFileStub1::PROJECT["id"], UploadFile::SOURCE_FILE_PATH => $uploadFileStub1->getFormattedFilePath(), - UploadFile::FILE_FORMAT => UploadFileStub1::FILE_FORMAT, + UploadFile::FILE_FORMAT => UploadFileStub1::PROJECT["file_format"], UploadFile::SOURCE_LOCALE => UploadFileStub1::SOURCE_LOCALE, - UploadFile::IS_KEEPING_ALL_STRINGS => UploadFileStub1::IS_KEEPING_ALL_STRINGS, + UploadFile::IS_KEEPING_ALL_STRINGS => UploadFileStub1::PROJECT["keep_all_strings"], ], [ - UploadFile::PROJECT_ID => UploadFileStub2::PROJECT_ID, + UploadFile::PROJECT_ID => UploadFileStub1::PROJECT["id"], UploadFile::SOURCE_FILE_PATH => $uploadFileStub2->getFormattedFilePath(), - UploadFile::FILE_FORMAT => UploadFileStub2::FILE_FORMAT, + UploadFile::FILE_FORMAT => UploadFileStub1::PROJECT["file_format"], UploadFile::SOURCE_LOCALE => UploadFileStub2::SOURCE_LOCALE, - UploadFile::IS_KEEPING_ALL_STRINGS => UploadFileStub2::IS_KEEPING_ALL_STRINGS + UploadFile::IS_KEEPING_ALL_STRINGS => UploadFileStub1::PROJECT["keep_all_strings"], ], ], ClientMock::$parameters @@ -112,12 +113,19 @@ public function WithFileNotOnApiException_download_DoNothing() /** * @test - * @expectedException \Guzzle\Http\Exception\ServerErrorResponseException + * @expectedException \Symfony\Component\HttpKernel\Exception\HttpException; */ public function ApiServerError_download_ThrowException() { + $exception = null; ClientMock::$downloadedContent = '{"meta":{"status":500,"message":"Something went wrong. Please try again or contact us at support@oneskyapp.com"},"data":{}}'; - $this->gateway->downloadTranslations([new ExportFileStub1()]); + try { + $this->gateway->downloadTranslations([new ExportFileStub1()]); + } catch (HttpException $e) { + $exception = $e; + } + $this->assertNotNull($exception); + $this->assertEquals(500, $exception->getStatusCode()); } /** @@ -142,13 +150,14 @@ public function WithOneFile_download_Download() ClientMock::$parameters, [ [ - ExportFile::PROJECT_ID => ExportFileStub1::PROJECT_ID, + ExportFile::PROJECT_ID => ExportFileStub1::PROJECT["id"], ExportFile::REQUESTED_LOCALE => ExportFileStub1::REQUESTED_LOCALE, ExportFile::REQUESTED_SOURCE_FILE_NAME => $exportFileStub1->getEncodedSourceFileName(), + ExportFile::FILE_FORMAT => ExportFileStub1::PROJECT["file_format"], ], ] ); - $this->assertStringEqualsFile(self::EXPECTED_DOWNLOADED_FILE_1, 'Download : 1'); + $this->assertStringEqualsFile(self::EXPECTED_DOWNLOADED_FILE_1, "Download: 1\n"); $this->assertEquals([new ExportFileStub1()], $downloadedTranslations); } @@ -165,19 +174,21 @@ public function WithManyFiles_upload_Upload() ClientMock::$parameters, [ [ - ExportFile::PROJECT_ID => ExportFileStub1::PROJECT_ID, + ExportFile::PROJECT_ID => ExportFileStub1::PROJECT["id"], ExportFile::REQUESTED_LOCALE => ExportFileStub1::REQUESTED_LOCALE, ExportFile::REQUESTED_SOURCE_FILE_NAME => $exportFileStub1->getEncodedSourceFileName(), + ExportFile::FILE_FORMAT => ExportFileStub1::PROJECT["file_format"], ], [ - ExportFile::PROJECT_ID => ExportFileStub2::PROJECT_ID, + ExportFile::PROJECT_ID => ExportFileStub2::PROJECT["id"], ExportFile::REQUESTED_LOCALE => ExportFileStub2::REQUESTED_LOCALE, ExportFile::REQUESTED_SOURCE_FILE_NAME => $exportFileStub2->getEncodedSourceFileName(), + ExportFile::FILE_FORMAT => ExportFileStub1::PROJECT["file_format"], ], ] ); - $this->assertStringEqualsFile(self::EXPECTED_DOWNLOADED_FILE_1, 'Download : 1'); - $this->assertStringEqualsFile(self::EXPECTED_DOWNLOADED_FILE_2, 'Download : 2'); + $this->assertStringEqualsFile(self::EXPECTED_DOWNLOADED_FILE_1, "Download: 1\n"); + $this->assertStringEqualsFile(self::EXPECTED_DOWNLOADED_FILE_2, "Download: 2\n"); $this->assertEquals([new ExportFileStub1(), new ExportFileStub2()], $downloadedTranslations); } diff --git a/Tests/Gateways/Impl/LanguageGatewayImplTest.php b/Tests/Gateways/Impl/LanguageGatewayImplTest.php index 7f9702a..7231ab3 100644 --- a/Tests/Gateways/Impl/LanguageGatewayImplTest.php +++ b/Tests/Gateways/Impl/LanguageGatewayImplTest.php @@ -8,6 +8,7 @@ use OpenClassrooms\Bundle\OneSkyBundle\Model\Language; use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Model\LanguageStub1; use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Model\LanguageStub2; +use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Model\ProjectsStub; use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\OneSky\Api\ClientMock; /** @@ -27,7 +28,7 @@ class LanguageGatewayImplTest extends \PHPUnit_Framework_TestCase public function ApiException_findLanguage_ThrowException() { ClientMock::$languagesContent = '{"meta": {"status": 400}}'; - $this->gateway->findLanguages([]); + $this->gateway->findLanguages([], ProjectsStub::$projects[1]); } /** @@ -36,7 +37,7 @@ public function ApiException_findLanguage_ThrowException() */ public function NonExistingLanguage_findLanguages_ThrowException() { - $this->gateway->findLanguages(['fr']); + $this->gateway->findLanguages(['fr'], ProjectsStub::$projects[1]); } /** @@ -44,7 +45,7 @@ public function NonExistingLanguage_findLanguages_ThrowException() */ public function findLanguages() { - $actualLanguages = $this->gateway->findLanguages([LanguageStub1::LOCALE, LanguageStub2::LOCALE]); + $actualLanguages = $this->gateway->findLanguages([LanguageStub1::LOCALE, LanguageStub2::LOCALE], ProjectsStub::$projects[1]); $expectedLanguages = [new LanguageStub1(), new LanguageStub2()]; $this->assertEquals(LanguageGateway::LANGUAGES_METHOD, ClientMock::$action); $this->assertEquals(['project_id' => 1], ClientMock::$parameters); @@ -67,6 +68,5 @@ protected function setUp() $this->gateway->setClient(new ClientMock()); ClientMock::$languagesContent = '{"meta": {"status": 200,"record_count": 3},"data": [{"code": "en-US","english_name": "English (United States)","local_name": "English (United States)","locale": "en","region": "US","is_base_language": true,"is_ready_to_publish": true,"translation_progress": "100%","uploaded_at": "2013-10-07T15:27:10+0000","uploaded_at_timestamp": 1381159630},{"code": "ja-JP","english_name": "Japanese","local_name": "日本語","locale": "ja","region": "JP","is_base_language": false,"is_ready_to_publish": true,"translation_progress": "98%","uploaded_at": "2013-10-07T15:27:10+0000","uploaded_at_timestamp": 1381159630},{"code": "ko-KR","english_name": "Korean","local_name": "한국어","locale": "ko","region": "KR","is_base_language": false,"is_ready_to_publish": true,"translation_progress": "56%","uploaded_at": "2013-10-07T15:27:10+0000","uploaded_at_timestamp": 1381159630}]}'; $this->gateway->setLanguageFactory(new LanguageFactoryImpl()); - $this->gateway->setProjectId(1); } } diff --git a/Tests/Services/Impl/LanguageServiceImplTest.php b/Tests/Services/Impl/LanguageServiceImplTest.php index 66c5288..a4e72f7 100644 --- a/Tests/Services/Impl/LanguageServiceImplTest.php +++ b/Tests/Services/Impl/LanguageServiceImplTest.php @@ -7,6 +7,7 @@ use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Gateways\InMemoryLanguageGateway; use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Model\LanguageStub1; use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Model\LanguageStub2; +use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Model\ProjectsStub; /** * @author Romain Kuzniak @@ -23,8 +24,8 @@ class LanguageServiceImplTest extends \PHPUnit_Framework_TestCase */ public function WithoutLocales_getLanguage() { - $languages = $this->service->getLanguages(); - $this->assertEquals([new LanguageStub2()], $languages); + $languages = $this->service->getLanguages(ProjectsStub::$projects[1], ProjectsStub::$projects[1]['locales']); + $this->assertEquals([ProjectsStub::$projects[1]["id"] => [new LanguageStub1(), new LanguageStub2()]], $languages); } /** @@ -32,8 +33,8 @@ public function WithoutLocales_getLanguage() */ public function getLanguage() { - $languages = $this->service->getLanguages([LanguageStub1::LOCALE, LanguageStub2::LOCALE]); - $this->assertEquals([new LanguageStub1(), new LanguageStub2()], $languages); + $languages = $this->service->getLanguages(ProjectsStub::$projects[1], [LanguageStub1::LOCALE]); + $this->assertEquals([ProjectsStub::$projects[1]["id"] => [new LanguageStub1()]], $languages); } /** @@ -44,9 +45,8 @@ protected function setUp() $this->service = new LanguageServiceImpl(); $this->service->setLanguageGateway( new InMemoryLanguageGateway( - [LanguageStub1::LOCALE => new LanguageStub1(), LanguageStub2::LOCALE => new LanguageStub2()] + [ProjectsStub::$projects[1]["id"] => [LanguageStub1::LOCALE => new LanguageStub1(), LanguageStub2::LOCALE => new LanguageStub2()]] ) ); - $this->service->setRequestedLocales(['ja']); } } diff --git a/Tests/Services/Impl/TranslationServiceImplTest.php b/Tests/Services/Impl/TranslationServiceImplTest.php index e459d7a..1423397 100644 --- a/Tests/Services/Impl/TranslationServiceImplTest.php +++ b/Tests/Services/Impl/TranslationServiceImplTest.php @@ -7,6 +7,7 @@ use OpenClassrooms\Bundle\OneSkyBundle\Model\Impl\UploadFileImpl; use OpenClassrooms\Bundle\OneSkyBundle\Services\Impl\TranslationServiceImpl; use OpenClassrooms\Bundle\OneSkyBundle\Services\TranslationService; +use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Model\ProjectsStub; use OpenClassrooms\Bundle\OneSkyBundle\Tests\Doubles\Services\FileServiceMock; use Symfony\Component\EventDispatcher\EventDispatcher; @@ -15,12 +16,8 @@ */ class TranslationServiceImplTest extends \PHPUnit_Framework_TestCase { - const IS_KEEPING_ALL_STRINGS = false; - const FILE_FORMAT = 'yml'; const KERNEL_ROOT_DIR = __DIR__.'/../../'; const PROJECT_DIRECTORY = __DIR__.'/../../../'; - const PROJECT_ID = 1; - const SOURCE_LOCALE = 'en'; /** * @var TranslationService @@ -32,9 +29,21 @@ class TranslationServiceImplTest extends \PHPUnit_Framework_TestCase */ public function pull_with_locales() { - $this->service->pull([__DIR__.'/../../Fixtures/Resources/translations'], ['es']); + $this->service->pull([] , ProjectsStub::$projects[1]["file_paths"], ['ja']); $this->assertEquals( - [$this->buildExportFile1es(), $this->buildExportFile2es()], + [$this->buildExportFile1ja(), $this->buildExportFile2ja()], + FileServiceMock::$downloadedFiles + ); + } + + /** + * @test + */ + public function pull_with_locales_and_projects() + { + $this->service->pull([1] , ProjectsStub::$projects[1]["file_paths"], ['ja']); + $this->assertEquals( + [$this->buildExportFile1ja(), $this->buildExportFile2ja()], FileServiceMock::$downloadedFiles ); } @@ -42,24 +51,24 @@ public function pull_with_locales() /** * @return ExportFileImpl */ - private function buildExportFile1es() + private function buildExportFile1ja() { return new ExportFileImpl( - self::PROJECT_ID, __DIR__.'/../../Fixtures/Resources/translations/messages.en.yml', + ProjectsStub::$projects[1], ProjectsStub::$projects[1]["file_paths"][0].'messages.en.yml', self::PROJECT_DIRECTORY, - 'es' + 'ja' ); } /** * @return ExportFileImpl */ - private function buildExportFile2es() + private function buildExportFile2ja() { return new ExportFileImpl( - self::PROJECT_ID, __DIR__.'/../../Fixtures/Resources/translations/subDirectory/messages.en.yml', + ProjectsStub::$projects[1], ProjectsStub::$projects[1]["file_paths"][0].'subDirectory/messages.en.yml', self::PROJECT_DIRECTORY, - 'es' + 'ja' ); } @@ -68,13 +77,13 @@ private function buildExportFile2es() */ public function WithoutFilePaths_pull() { - $this->service->pull([]); + $this->service->pull([], []); $this->assertEquals( [ - $this->buildExportFile1fr(), - $this->buildExportFile1es(), - $this->buildExportFile2fr(), - $this->buildExportFile2es(), + $this->buildExportFile1en(), + $this->buildExportFile1ja(), + $this->buildExportFile2en(), + $this->buildExportFile2ja(), ], FileServiceMock::$downloadedFiles ); @@ -83,24 +92,24 @@ public function WithoutFilePaths_pull() /** * @return ExportFileImpl */ - private function buildExportFile1fr() + private function buildExportFile1en() { return new ExportFileImpl( - self::PROJECT_ID, __DIR__.'/../../Fixtures/Resources/translations/messages.en.yml', + ProjectsStub::$projects[1], ProjectsStub::$projects[1]["file_paths"][0].'messages.en.yml', self::PROJECT_DIRECTORY, - 'fr' + 'en' ); } /** * @return ExportFileImpl */ - private function buildExportFile2fr() + private function buildExportFile2en() { return new ExportFileImpl( - self::PROJECT_ID, __DIR__.'/../../Fixtures/Resources/translations/subDirectory/messages.en.yml', + ProjectsStub::$projects[1], ProjectsStub::$projects[1]["file_paths"][0].'subDirectory/messages.en.yml', self::PROJECT_DIRECTORY, - 'fr' + 'en' ); } @@ -109,11 +118,26 @@ private function buildExportFile2fr() */ public function pull() { - $this->service->pull([__DIR__.'/../../Fixtures/Resources/translations/subDirectory']); + $this->service->pull([], [ProjectsStub::$projects[1]["file_paths"][0].'subDirectory']); + $this->assertEquals( + [ + $this->buildExportFile2en(), + $this->buildExportFile2ja(), + ], + FileServiceMock::$downloadedFiles + ); + } + + /** + * @test + */ + public function pull_with_projects() + { + $this->service->pull([1], [ProjectsStub::$projects[1]["file_paths"][0].'subDirectory']); $this->assertEquals( [ - $this->buildExportFile2fr(), - $this->buildExportFile2es(), + $this->buildExportFile2en(), + $this->buildExportFile2ja(), ], FileServiceMock::$downloadedFiles ); @@ -124,7 +148,7 @@ public function pull() */ public function WithoutFilePath_push() { - $this->service->push([]); + $this->service->push([], []); $this->assertEquals([$this->buildUploadFile1(), $this->buildUploadFile2()], FileServiceMock::$uploadedFiles); } @@ -134,13 +158,11 @@ public function WithoutFilePath_push() private function buildUploadFile1() { $file = new UploadFileImpl( - self::PROJECT_ID, - __DIR__.'/../../Fixtures/Resources/translations/messages.en.yml', + ProjectsStub::$projects[1], + ProjectsStub::$projects[1]["file_paths"][0].'messages.en.yml', self::PROJECT_DIRECTORY, - self::FILE_FORMAT, - self::SOURCE_LOCALE + 'en' ); - $file->setKeepingAllStrings(self::IS_KEEPING_ALL_STRINGS); return $file; } @@ -151,13 +173,11 @@ private function buildUploadFile1() private function buildUploadFile2() { $file = new UploadFileImpl( - self::PROJECT_ID, - __DIR__.'/../../Fixtures/Resources/translations/subDirectory/messages.en.yml', + ProjectsStub::$projects[1], + ProjectsStub::$projects[1]["file_paths"][0].'subDirectory/messages.en.yml', self::PROJECT_DIRECTORY, - self::FILE_FORMAT, - self::SOURCE_LOCALE + 'en' ); - $file->setKeepingAllStrings(self::IS_KEEPING_ALL_STRINGS); return $file; } @@ -167,7 +187,34 @@ private function buildUploadFile2() */ public function push() { - $this->service->push([__DIR__.'/../../Fixtures/Resources/*']); + $this->service->push([], ProjectsStub::$projects[1]["file_paths"]); + $this->assertEquals([$this->buildUploadFile1(), $this->buildUploadFile2()], FileServiceMock::$uploadedFiles); + } + + /** + * @test + */ + public function push_with_projects() + { + $this->service->push([1], ProjectsStub::$projects[1]["file_paths"]); + $this->assertEquals([$this->buildUploadFile1(), $this->buildUploadFile2()], FileServiceMock::$uploadedFiles); + } + + /** + * @test + */ + public function push_with_locales() + { + $this->service->push([], ProjectsStub::$projects[1]["file_paths"], ["en"]); + $this->assertEquals([$this->buildUploadFile1(), $this->buildUploadFile2()], FileServiceMock::$uploadedFiles); + } + + /** + * @test + */ + public function push_with_projects_and_locales() + { + $this->service->push([1], ProjectsStub::$projects[1]["file_paths"], ["en"]); $this->assertEquals([$this->buildUploadFile1(), $this->buildUploadFile2()], FileServiceMock::$uploadedFiles); } @@ -176,9 +223,22 @@ public function push() */ public function WithLocales_update_Update() { - $this->service->update([__DIR__.'/../../Fixtures/Resources/'], ['es']); + $this->service->update([], ProjectsStub::$projects[1]["file_paths"], ['ja']); + $this->assertEquals( + [$this->buildExportFile1ja(), $this->buildExportFile2ja()], + FileServiceMock::$downloadedFiles + ); + $this->assertEquals([$this->buildUploadFile1(), $this->buildUploadFile2()], FileServiceMock::$uploadedFiles); + } + + /** + * @test + */ + public function WithLocales_and_projects_update_Update() + { + $this->service->update([1], ProjectsStub::$projects[1]["file_paths"], ['ja']); $this->assertEquals( - [$this->buildExportFile1es(), $this->buildExportFile2es()], + [$this->buildExportFile1ja(), $this->buildExportFile2ja()], FileServiceMock::$downloadedFiles ); $this->assertEquals([$this->buildUploadFile1(), $this->buildUploadFile2()], FileServiceMock::$uploadedFiles); @@ -191,17 +251,10 @@ protected function setUp() { $this->service = new TranslationServiceImpl(); $fileFactory = new FileFactoryImpl(); - $fileFactory->setKeepingAllStrings(self::IS_KEEPING_ALL_STRINGS); - $fileFactory->setFileFormat(self::FILE_FORMAT); $fileFactory->setKernelRootDir(self::KERNEL_ROOT_DIR); - $fileFactory->setProjectId(self::PROJECT_ID); - $fileFactory->setSourceLocale(self::SOURCE_LOCALE); $this->service->setEventDispatcher(new EventDispatcher()); $this->service->setFileFactory($fileFactory); - $this->service->setFileFormat(self::FILE_FORMAT); - $this->service->setFilePaths([__DIR__.'/../../Fixtures/Resources/*']); $this->service->setFileService(new FileServiceMock()); - $this->service->setRequestedLocales(['fr', 'es']); - $this->service->setSourceLocale(self::SOURCE_LOCALE); + $this->service->setProjects([1 => ProjectsStub::$projects[1]]); } }