Completed
Push — master ( 269a50...019c60 )
by Tobias
04:32
created

AllExtractorsTest::translationExists()   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
         * It is okey to increase the error count if you adding more fixtures/code.
51
         * We just need to be aware that it changes.
52
         */
53
        $this->assertCount(9, $sc->getErrors(), 'There was an unexpected number of errors. Please investigate.');
54
    }
55
56
    /**
57
     * Assert that a translation key exists in source collection.
58
     *
59
     * @param SourceCollection $sc
60
     * @param $translationKey
61
     * @param string $message
62
     */
63
    private function translationExists(SourceCollection $sc, $translationKey, $message = null)
64
    {
65
        if (empty($message)) {
66
            $message = sprintf('Tried to find "%s" but failed', $translationKey);
67
        }
68
69
        $found = false;
70
        foreach ($sc as $source) {
71
            if ($translationKey === $source->getMessage()) {
72
                $found = true;
73
74
                break;
75
            }
76
        }
77
78
        $this->assertTrue($found, $message);
79
    }
80
81
    /**
82
     * Assert that a translation key is missing in source collection.
83
     *
84
     * @param SourceCollection $sc
85
     * @param $translationKey
86
     * @param string $message
87
     */
88
    private function translationMissing(SourceCollection $sc, $translationKey, $message = null)
89
    {
90
        if (empty($message)) {
91
            $message = sprintf('The translation key "%s" should not exist', $translationKey);
92
        }
93
94
        $found = false;
95
        foreach ($sc as $source) {
96
            if ($translationKey === $source->getMessage()) {
97
                $found = true;
98
99
                break;
100
            }
101
        }
102
103
        $this->assertFalse($found, $message);
104
    }
105
106
    /**
107
     * @return PHPFileExtractor
108
     */
109
    private function getPHPFileExtractor()
110
    {
111
        $file = new PHPFileExtractor();
112
        $file->addVisitor(new ContainerAwareTrans());
113
        $file->addVisitor(new ContainerAwareTransChoice());
114
        $file->addVisitor(new FlashMessage());
115
        $file->addVisitor(new FormTypeChoices());
116
        $file->addVisitor(new FormTypeLabelExplicit());
117
        $file->addVisitor(new FormTypeLabelImplicit());
118
119
        return $file;
120
    }
121
122
    /**
123
     * @return TwigFileExtractor
124
     */
125
    private function getTwigFileExtractor()
126
    {
127
        $file = new TwigFileExtractor(TwigEnvironmentFactory::create());
128
        $file->addVisitor(TwigVisitorFactory::create());
129
130
        return $file;
131
    }
132
}
133