Completed
Push — master ( 04f40e...f278b5 )
by Tobias
05:43
created

AllExtractorsTest::translationMissing()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
c 0
b 0
f 0
rs 9.2
cc 4
eloc 9
nc 6
nop 3
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Extractor\Tests\Smoke;
13
14
use PHPUnit\Framework\TestCase;
15
use Symfony\Component\Finder\Finder;
16
use Translation\Extractor\Extractor;
17
use Translation\Extractor\FileExtractor\PHPFileExtractor;
18
use Translation\Extractor\FileExtractor\TwigFileExtractor;
19
use Translation\Extractor\Model\SourceCollection;
20
use Translation\Extractor\Tests\Functional\Visitor\Twig\TwigEnvironmentFactory;
21
use Translation\Extractor\Visitor\Php\Symfony\ContainerAwareTrans;
22
use Translation\Extractor\Visitor\Php\Symfony\ContainerAwareTransChoice;
23
use Translation\Extractor\Visitor\Php\Symfony\FlashMessage;
24
use Translation\Extractor\Visitor\Php\Symfony\FormTypeChoices;
25
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelExplicit;
26
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelImplicit;
27
use Translation\Extractor\Visitor\Twig\TwigVisitorFactory;
28
29
/**
30
 * Smoke test to make sure no extractor throws exceptions.
31
 *
32
 * @author Tobias Nyholm <[email protected]>
33
 */
34
class AllExtractorsTest extends TestCase
35
{
36
    public function testNoException()
37
    {
38
        $extractor = new Extractor();
39
        $extractor->addFileExtractor($this->getPHPFileExtractor());
40
        $extractor->addFileExtractor($this->getTwigFileExtractor());
41
42
        $finder = new Finder();
43
        $finder->in(dirname(__DIR__));
44
45
        $sc = $extractor->extract($finder);
46
        $this->translationExists($sc, 'trans.issue_34');
47
        $this->translationMissing($sc, 'trans.issue_62');
48
    }
49
50
    /**
51
     * Assert that a translation key exists in source collection.
52
     *
53
     * @param SourceCollection $sc
54
     * @param $translationKey
55
     * @param string $message
56
     */
57 View Code Duplication
    private function translationExists(SourceCollection $sc, $translationKey, $message = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        if (empty($message)) {
60
            $message = sprintf('Tried to find "%s" but failed', $translationKey);
61
        }
62
63
        $found = false;
64
        foreach ($sc as $source) {
65
            if ($translationKey === $source->getMessage()) {
66
                $found = true;
67
68
                break;
69
            }
70
        }
71
72
        $this->assertTrue($found, $message);
73
    }
74
75
    /**
76
     * Assert that a translation key is missing in source collection.
77
     *
78
     * @param SourceCollection $sc
79
     * @param $translationKey
80
     * @param string $message
81
     */
82 View Code Duplication
    private function translationMissing(SourceCollection $sc, $translationKey, $message = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        if (empty($message)) {
85
            $message = sprintf('The translation key "%s" should not exist', $translationKey);
86
        }
87
88
        $found = false;
89
        foreach ($sc as $source) {
90
            if ($translationKey === $source->getMessage()) {
91
                $found = true;
92
93
                break;
94
            }
95
        }
96
97
        $this->assertFalse($found, $message);
98
    }
99
100
    /**
101
     * @return PHPFileExtractor
102
     */
103
    private function getPHPFileExtractor()
104
    {
105
        $file = new PHPFileExtractor();
106
        $file->addVisitor(new ContainerAwareTrans());
107
        $file->addVisitor(new ContainerAwareTransChoice());
108
        $file->addVisitor(new FlashMessage());
109
        $file->addVisitor(new FormTypeChoices());
110
        $file->addVisitor(new FormTypeLabelExplicit());
111
        $file->addVisitor(new FormTypeLabelImplicit());
112
113
        return $file;
114
    }
115
116
    /**
117
     * @return TwigFileExtractor
118
     */
119
    private function getTwigFileExtractor()
120
    {
121
        $file = new TwigFileExtractor(TwigEnvironmentFactory::create());
122
        $file->addVisitor(TwigVisitorFactory::create());
123
124
        return $file;
125
    }
126
}
127