Completed
Push — master ( c95386...a423a5 )
by Tobias
02:31
created

AllExtractorsTest::testNoException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 1
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 Symfony\Component\Finder\Finder;
15
use Translation\Extractor\Extractor;
16
use Translation\Extractor\FileExtractor\PHPFileExtractor;
17
use Translation\Extractor\FileExtractor\TwigFileExtractor;
18
use Translation\Extractor\Model\SourceCollection;
19
use Translation\Extractor\Tests\Functional\Visitor\Twig\TwigEnvironmentFactory;
20
use Translation\Extractor\Visitor\Php\Symfony\ContainerAwareTrans;
21
use Translation\Extractor\Visitor\Php\Symfony\ContainerAwareTransChoice;
22
use Translation\Extractor\Visitor\Php\Symfony\FlashMessage;
23
use Translation\Extractor\Visitor\Php\Symfony\FormTypeChoices;
24
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelExplicit;
25
use Translation\Extractor\Visitor\Php\Symfony\FormTypeLabelImplicit;
26
use Translation\Extractor\Visitor\Twig\Twig1Visitor;
27
use Translation\Extractor\Visitor\Twig\Twig2Visitor;
28
29
/**
30
 * Smoke test to make sure no extractor throws exceptions.
31
 *
32
 * @author Tobias Nyholm <[email protected]>
33
 */
34
class AllExtractorsTest extends \PHPUnit_Framework_TestCase
35
{
36 1
    public function testNoException()
37
    {
38 1
        $extractor = new Extractor();
39 1
        $extractor->addFileExtractor($this->getPHPFileExtractor());
40 1
        $extractor->addFileExtractor($this->getTwigFileExtractor());
41
42 1
        $finder = new Finder();
43 1
        $finder->in(dirname(__DIR__));
44
45 1
        $sc = $extractor->extract($finder);
46 1
        $this->translationExists($sc, 'trans.issue_34');
47 1
    }
48
49
    /**
50
     * Assert that a translation key exists in source collection.
51
     *
52
     * @param SourceCollection $sc
53
     * @param $translationKey
54
     * @param string $message
55
     */
56 1
    private function translationExists(SourceCollection $sc, $translationKey, $message = null)
57
    {
58 1
        if (empty($message)) {
59 1
            $message = sprintf('Tried to find "%s" but failed', $translationKey);
60 1
        }
61
62 1
        $found = false;
63 1
        foreach ($sc as $source) {
64 1
            if ($translationKey === $source->getMessage()) {
65 1
                $found = true;
66 1
                break;
67
            }
68 1
        }
69
70 1
        $this->assertTrue($found, $message);
71 1
    }
72
73
    /**
74
     * @return PHPFileExtractor
75
     */
76 1
    private function getPHPFileExtractor()
77
    {
78 1
        $file = new PHPFileExtractor();
79 1
        $file->addVisitor(new ContainerAwareTrans());
80 1
        $file->addVisitor(new ContainerAwareTransChoice());
81 1
        $file->addVisitor(new FlashMessage());
82 1
        $file->addVisitor(new FormTypeChoices());
83 1
        $file->addVisitor(new FormTypeLabelExplicit());
84 1
        $file->addVisitor(new FormTypeLabelImplicit());
85
86 1
        return $file;
87
    }
88
89
    /**
90
     * @return TwigFileExtractor
91
     */
92 1
    private function getTwigFileExtractor()
93
    {
94 1
        $file = new TwigFileExtractor(TwigEnvironmentFactory::create());
95
96 1
        if (\Twig_Environment::MAJOR_VERSION === 1) {
97 1
            $file->addVisitor(new Twig1Visitor());
98 1
        } else {
99
            $file->addVisitor(new Twig2Visitor());
100
        }
101
102 1
        return $file;
103
    }
104
}
105