Completed
Pull Request — master (#6706)
by Damian
09:02
created

i18nTestManifest   B

Complexity

Total Complexity 8

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 18

Importance

Changes 0
Metric Value
dl 0
loc 134
rs 7.3333
c 0
b 0
f 0
wmc 8
lcom 1
cbo 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtraDataObjects() 0 8 1
A setupManifest() 0 48 1
A tearDownManifest() 0 8 1
A pushManifest() 0 5 1
A pushModuleManifest() 0 5 1
A popManifests() 0 12 3
1
<?php
2
3
namespace SilverStripe\i18n\Tests;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Core\Manifest\ClassManifest;
8
use SilverStripe\Core\Manifest\ClassLoader;
9
use SilverStripe\Core\Manifest\ModuleLoader;
10
use SilverStripe\Core\Manifest\ModuleManifest;
11
use SilverStripe\i18n\i18n;
12
use SilverStripe\i18n\Messages\MessageProvider;
13
use SilverStripe\i18n\Messages\Symfony\ModuleYamlLoader;
14
use SilverStripe\i18n\Messages\Symfony\SymfonyMessageProvider;
15
use SilverStripe\i18n\Messages\YamlReader;
16
use SilverStripe\i18n\Tests\i18nTest\MyObject;
17
use SilverStripe\i18n\Tests\i18nTest\MySubObject;
18
use SilverStripe\i18n\Tests\i18nTest\TestDataObject;
19
use SilverStripe\View\SSViewer;
20
use SilverStripe\View\SSViewer_DataPresenter;
21
use SilverStripe\View\ThemeResourceLoader;
22
use SilverStripe\View\ThemeManifest;
23
use SilverStripe\View\ViewableData;
24
use Symfony\Component\Translation\Loader\ArrayLoader;
25
use Symfony\Component\Translation\Translator;
26
27
/**
28
 * Helper trait for bootstrapping test manifest for i18n tests
29
 */
30
trait i18nTestManifest
31
{
32
    /**
33
     * Fake webroot with a single module /i18ntestmodule which contains some files with _t() calls.
34
     *
35
     * @var string
36
     */
37
    protected $alternateBasePath;
38
39
    /**
40
     * Number of test manifests
41
     *
42
     * @var int
43
     */
44
    protected $manifests = 0;
45
46
    /**
47
     * Number of module manifests
48
     *
49
     * @var int
50
     */
51
    protected $moduleManifests = 0;
52
53
    protected function getExtraDataObjects()
54
    {
55
        return [
56
            TestDataObject::class,
57
            MyObject::class,
58
            MySubObject::class,
59
        ];
60
    }
61
62
    /**
63
     * @var ThemeResourceLoader
64
     */
65
    protected $oldThemeResourceLoader = null;
66
67
    /**
68
     * @var string
69
     */
70
    protected $originalLocale = null;
71
72
    public function setupManifest()
73
    {
74
        // force SSViewer_DataPresenter to cache global template vars before we switch to the
75
        // test-project class manifest (since it will lose visibility of core classes)
76
        $presenter = new SSViewer_DataPresenter(new ViewableData());
77
        unset($presenter);
78
79
        // Switch to test manifest
80
        $s = DIRECTORY_SEPARATOR;
81
        $this->alternateBasePath = __DIR__ . $s . 'i18nTest' . $s . "_fakewebroot";
82
        Director::config()->update('alternate_base_folder', $this->alternateBasePath);
83
84
        // New module manifest
85
        $moduleManifest = new ModuleManifest($this->alternateBasePath, false);
86
        $this->pushModuleManifest($moduleManifest);
87
88
        // Replace old template loader with new one with alternate base path
89
        $this->oldThemeResourceLoader = ThemeResourceLoader::instance();
90
        ThemeResourceLoader::set_instance($loader = new ThemeResourceLoader($this->alternateBasePath));
91
        $loader->addSet(
92
            '$default',
93
            new ThemeManifest($this->alternateBasePath, project(), false)
94
        );
95
96
        SSViewer::set_themes([
97
            'testtheme1',
98
            '$default',
99
        ]);
100
101
        $this->originalLocale = i18n::get_locale();
102
        i18n::set_locale('en_US');
103
104
        // Set new manifest against the root
105
        $classManifest = new ClassManifest($this->alternateBasePath, true);
106
        $this->pushManifest($classManifest);
107
108
        // Setup uncached translator
109
        // This should pull the module list from the above manifest
110
        $translator = new Translator('en');
111
        $translator->setFallbackLocales(['en']);
112
        $loader = new ModuleYamlLoader();
113
        $loader->setReader(new YamlReader());
114
        $translator->addLoader('ss', $loader); // Standard ss module loader
115
        $translator->addLoader('array', new ArrayLoader()); // Note: array loader isn't added by default
116
        $provider = new SymfonyMessageProvider();
117
        $provider->setTranslator($translator);
118
        Injector::inst()->registerService($provider, MessageProvider::class);
119
    }
120
121
    public function tearDownManifest()
122
    {
123
        ThemeResourceLoader::set_instance($this->oldThemeResourceLoader);
124
        i18n::set_locale($this->originalLocale);
125
126
        // Reset any manifests pushed during this test
127
        $this->popManifests();
128
    }
129
130
    /**
131
     * Safely push a new class manifest.
132
     * These will be cleaned up on tearDown()
133
     *
134
     * @param ClassManifest $manifest
135
     */
136
    protected function pushManifest(ClassManifest $manifest)
137
    {
138
        $this->manifests++;
139
        ClassLoader::instance()->pushManifest($manifest);
140
    }
141
142
    protected function pushModuleManifest(ModuleManifest $manifest)
143
    {
144
        $this->moduleManifests++;
145
        ModuleLoader::instance()->pushManifest($manifest);
146
    }
147
148
    /**
149
     * Pop off all extra manifests
150
     */
151
    protected function popManifests()
152
    {
153
        // Reset any manifests pushed during this test
154
        while ($this->manifests > 0) {
155
            ClassLoader::instance()->popManifest();
156
            $this->manifests--;
157
        }
158
        while ($this->moduleManifests > 0) {
159
            ModuleLoader::instance()->popManifest();
160
            $this->moduleManifests--;
161
        }
162
    }
163
}
164