Skip to content

Commit

Permalink
Add tests for exclude directories
Browse files Browse the repository at this point in the history
  • Loading branch information
sowbiba committed Aug 24, 2021
1 parent 84b852a commit 374a4a3
Show file tree
Hide file tree
Showing 20 changed files with 593 additions and 82 deletions.
3 changes: 1 addition & 2 deletions Tests/PhpUnit/TestCase.php
Expand Up @@ -70,10 +70,9 @@ protected function getResource($resourceName)
}

/**
* @param $messageCatalogue
* @param array[] $expected
*/
protected function verifyCatalogue(MessageCatalogue $messageCatalogue, $expected)
protected function verifyCatalogue(MessageCatalogue $messageCatalogue, array $expected)
{
$domains = $messageCatalogue->getDomains();

Expand Down
29 changes: 27 additions & 2 deletions Tests/Translation/Extractor/ChainExtractorTest.php
@@ -1,4 +1,29 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
declare(strict_types=1);

namespace PrestaShop\TranslationToolsBundle\Tests\Translation\Extractor;

Expand All @@ -20,15 +45,15 @@ public function tearDown(): void
$this->instance = null;
}

public function testAddExtractor()
public function testAddExtractor(): void
{
$extractor = $this->getMockBuilder('Symfony\Component\Translation\Extractor\ExtractorInterface')
->getMock()
;
$this->assertSame($this->instance, $this->instance->addExtractor('test', $extractor));
}

public function testExtract()
public function testExtract(): void
{
$directory = '/';
$catalog = new MessageCatalogue('en', []);
Expand Down
164 changes: 143 additions & 21 deletions Tests/Translation/Extractor/PhpExtractorTest.php
@@ -1,4 +1,29 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
declare(strict_types=1);

namespace PrestaShop\TranslationToolsBundle\Tests\Translation\Extractor;

Expand All @@ -21,36 +46,36 @@ public function setUp(): void
$this->phpExtractor = new PhpExtractor();
}

public function testItExtractsTransMethodWithSymfonyStyleParameters()
public function testItExtractsTransMethodWithSymfonyStyleParameters(): void
{
$messageCatalogue = $this->buildMessageCatalogue('fixtures/TestController.php');

$this->assertTrue($messageCatalogue->defines('Fingers', 'admin.product.help'));
}

public function testItExtractsTransMethodWithPrestashopStyleParameters()
public function testItExtractsTransMethodWithPrestashopStyleParameters(): void
{
$messageCatalogue = $this->buildMessageCatalogue('fixtures/TestController.php');

$this->assertTrue($messageCatalogue->defines('This is how PrestaShop does it', 'admin.product.help'));
}

public function testItWorksWithMultiLineTrans()
public function testItWorksWithMultiLineTrans(): void
{
$messageCatalogue = $this->buildMessageCatalogue('fixtures/TestController.php');

$this->assertTrue($messageCatalogue->defines('This is how symfony does it', 'admin.product.help'));
}

public function testItExtractsTransWithoutDomain()
public function testItExtractsTransWithoutDomain(): void
{
$messageCatalogue = $this->buildMessageCatalogue('fixtures/TestController.php');

$this->assertTrue($messageCatalogue->defines('Look, no domain', 'messages'));
$this->assertTrue($messageCatalogue->defines('It works with no domain and with parameters', 'messages'));
}

public function testItInterpolatesDomainVariables()
public function testItInterpolatesDomainVariables(): void
{
$this->markTestIncomplete("The extractor doesn't know how to interpolate variables yet");

Expand All @@ -59,7 +84,7 @@ public function testItInterpolatesDomainVariables()
$this->assertTrue($messageCatalogue->defines('Bar', 'admin.product.plop'));
}

public function testItExtractsArrays()
public function testItExtractsArrays(): void
{
$messageCatalogue = $this->buildMessageCatalogue('fixtures/TestController.php');

Expand All @@ -70,7 +95,7 @@ public function testItExtractsArrays()
$this->assertTrue($messageCatalogue->defines('This text is coming back somewhere', 'Admin.Notifications.Error'));
}

public function testItDoesNotExtractArraysWhenItShouldNot()
public function testItDoesNotExtractArraysWhenItShouldNot(): void
{
$messageCatalogue = $this->buildMessageCatalogue('fixtures/TestController.php');

Expand All @@ -82,42 +107,39 @@ public function testItDoesNotExtractArraysWhenItShouldNot()
$this->assertFalse($messageCatalogue->defines("I'm with foo, which spoils any party, even with parameters", 'Admin.Notifications.Error'));
}

public function testExtractWithoutNamespace()
public function testExtractWithoutNamespace(): void
{
$messageCatalogue = $this->buildMessageCatalogue('fixtures/TestWithoutNamespaceController.php');

$this->assertArrayHasKey('Shop', $messageCatalogue->all('messages'));
}

public function testExtractLegacyController()
public function testExtractLegacyController(): void
{
$messageCatalogue = $this->buildMessageCatalogue('fixtures/LegacyController.php');

$this->assertTrue($messageCatalogue->has('Successful deletion'));
$this->assertArrayHasKey('Prestashop', $messageCatalogue->all('Domain'));
}

public function testExtractEmails()
public function testExtractEmails(): void
{
$messageCatalogue = $this->buildMessageCatalogue('fixtures/TestExtractEmails.php');

$this->assertTrue($messageCatalogue->defines('Always keep your account details safe.', 'Emails.Body'));
}

/**
* @param $file
* @param $expected
*
* @dataProvider provideFormTranslationFixtures
*/
public function testExtractFormTranslations($file, $expected)
public function testExtractFormTranslations(string $file, array $expected): void
{
$messageCatalogue = $this->buildMessageCatalogue($file);

$this->verifyCatalogue($messageCatalogue, $expected);
}

public function provideFormTranslationFixtures()
public function provideFormTranslationFixtures(): array
{
return [
'TestFormChoices.php' => [
Expand Down Expand Up @@ -182,12 +204,112 @@ public function provideFormTranslationFixtures()
];
}

/**
* @param $fixtureResource
*
* @return MessageCatalogue
*/
private function buildMessageCatalogue($fixtureResource)
public function testExtractFromDirectory(): void
{
$messageCatalogue = $this->buildMessageCatalogue('directory/');

$catalogue = $messageCatalogue->all();
$this->assertCount(2, array_keys($catalogue));
$this->assertCount(3, $catalogue['messages']);
$this->assertCount(2, $catalogue['admin.product.help']);

$this->verifyCatalogue($messageCatalogue, [
'messages' => [
'SecondSubdirShop' => 'SecondSubdirShop',
'SubdirShop' => 'SubdirShop',
'Shop' => 'Shop',
],
'admin.product.help' => [
'SubdirFingers' => 'SubdirFingers',
'Fingers' => 'Fingers',
],
]);

$messageCatalogue = new MessageCatalogue('en');
$this->phpExtractor
->excludedDirectories(['subdirectory'])
->extract($this->getResource('directory/'), $messageCatalogue);

$catalogue = $messageCatalogue->all();
$this->assertCount(2, array_keys($catalogue));
$this->assertCount(2, $catalogue['messages']);
$this->assertCount(1, $catalogue['admin.product.help']);

$this->verifyCatalogue($messageCatalogue, [
'messages' => [
'SecondSubdirShop' => 'SecondSubdirShop',
'Shop' => 'Shop',
],
'admin.product.help' => [
'Fingers' => 'Fingers',
],
]);

$messageCatalogue = new MessageCatalogue('en');
$this->phpExtractor
->excludedDirectories(['subdirectory', 'subdirectory2'])
->extract($this->getResource('directory/'), $messageCatalogue);

$catalogue = $messageCatalogue->all();
$this->assertCount(2, array_keys($catalogue));
$this->assertCount(1, $catalogue['messages']);
$this->assertCount(1, $catalogue['admin.product.help']);

$this->verifyCatalogue($messageCatalogue, [
'messages' => [
'Shop' => 'Shop',
],
'admin.product.help' => [
'Fingers' => 'Fingers',
],
]);
}

public function testExtractFromDirectoryExcludingSubDirectories(): void
{
// Exclude one directory
$messageCatalogue = new MessageCatalogue('en');
$this->phpExtractor
->excludedDirectories(['subdirectory'])
->extract($this->getResource('directory/'), $messageCatalogue);

$catalogue = $messageCatalogue->all();
$this->assertCount(2, array_keys($catalogue));
$this->assertCount(2, $catalogue['messages']);
$this->assertCount(1, $catalogue['admin.product.help']);

$this->verifyCatalogue($messageCatalogue, [
'messages' => [
'SecondSubdirShop' => 'SecondSubdirShop',
'Shop' => 'Shop',
],
'admin.product.help' => [
'Fingers' => 'Fingers',
],
]);

// Exclude multiple directories
$messageCatalogue = new MessageCatalogue('en');
$this->phpExtractor
->excludedDirectories(['subdirectory', 'subdirectory2'])
->extract($this->getResource('directory/'), $messageCatalogue);

$catalogue = $messageCatalogue->all();
$this->assertCount(2, array_keys($catalogue));
$this->assertCount(1, $catalogue['messages']);
$this->assertCount(1, $catalogue['admin.product.help']);

$this->verifyCatalogue($messageCatalogue, [
'messages' => [
'Shop' => 'Shop',
],
'admin.product.help' => [
'Fingers' => 'Fingers',
],
]);
}

private function buildMessageCatalogue(string $fixtureResource): MessageCatalogue
{
$messageCatalogue = new MessageCatalogue('en');
$this->phpExtractor->extract($this->getResource($fixtureResource), $messageCatalogue);
Expand Down

0 comments on commit 374a4a3

Please sign in to comment.