Completed
Push — 5.3 ( 958546...1cc96e )
by Jeroen
14:02 queued 07:05
created

Tests/unit/Service/Importer/ImporterTest.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\TranslatorBundle\Tests\Service\Importer;
4
5
use Kunstmaan\TranslatorBundle\Tests\unit\WebTestCase;
6
use Symfony\Component\Finder\Finder;
7
8
class ImporterTest extends WebTestCase
9
{
10
    private $rootDir;
11
12
    private $importer;
13
14
    private $translationRepository;
15
16
    public function setUp()
17
    {
18
        static::bootKernel(['test_case' => 'TranslatorBundleTest', 'root_config' => 'config.yaml']);
19
        $container = static::$kernel->getContainer();
20
        static::loadFixtures($container);
0 ignored issues
show
It seems like $container defined by static::$kernel->getContainer() on line 19 can be null; however, Kunstmaan\TranslatorBund...estCase::loadFixtures() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
21
22
        $this->translationRepository = $container->get('kunstmaan_translator.repository.translation');
23
        $this->importer = $container->get('kunstmaan_translator.service.importer.importer');
24
        $this->rootDir = $container->getParameter('kernel.root_dir');
25
    }
26
27
    /**
28
     * @group importer
29
     */
30 View Code Duplication
    public function testImportNewDomainFileForced()
31
    {
32
        foreach ($this->getNewDomainTestFinder() as $file) {
33
            $this->importer->import($file, true);
34
        }
35
36
        $translation = $this->translationRepository->findOneBy(array('keyword' => 'newdomain.name', 'locale' => 'de'));
37
        $this->assertEquals('a new domain', $translation->getText());
38
    }
39
40
    /**
41
     * @group importer
42
     */
43 View Code Duplication
    public function testImportExistingDomainFileNonForced()
44
    {
45
        foreach ($this->getExistingDomainTestFinder() as $file) {
46
            $this->importer->import($file, false);
47
        }
48
49
        $translation = $this->translationRepository->findOneBy(array('keyword' => 'headers.frontpage', 'locale' => 'en'));
50
        $this->assertEquals('a not yet updated frontpage header', $translation->getText());
51
    }
52
53
    /**
54
     * @group importer-isolated
55
     */
56 View Code Duplication
    public function testImportExistingDomainFileForced()
57
    {
58
        foreach ($this->getExistingDomainTestFinder() as $file) {
59
            $this->importer->import($file, true);
60
        }
61
62
        $translation = $this->translationRepository->findOneBy(array('keyword' => 'headers.frontpage', 'locale' => 'en'));
63
        $this->assertEquals('FrontPage', $translation->getText());
64
    }
65
66
    public function getNewDomainTestFinder()
67
    {
68
        $finder = new Finder();
69
70
        $finder->files()
71
                ->name('newdomain.de.yml')
72
                ->in($this->rootDir.'/Resources/translations/');
73
74
        return $finder;
75
    }
76
77
    public function getExistingDomainTestFinder()
78
    {
79
        $finder = new Finder();
80
81
        $finder->files()
82
                ->name('messages.en.yml')
83
                ->in($this->rootDir.'/Resources/translations/');
84
85
        return $finder;
86
    }
87
}
88