FileLoaderTestAbstract   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 11
eloc 88
c 1
b 1
f 0
dl 0
loc 120
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetLang() 0 4 1
A testNoFolder() 0 6 1
A testGetAvailableLanguages() 0 11 1
A testGetFolders() 0 6 1
A testGetTexts() 0 27 2
A testSetFolders() 0 4 1
A testGetResources() 0 34 2
A testGetResolverName() 0 4 1
A testSetLang() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Translation\Loaders;
5
6
use Tester\Assert;
7
use Nexendrie\Translation\InvalidFolderException;
8
use Nexendrie\Translation\FolderNotSetException;
9
10
/**
11
 * General test suit for file loaders
12
 *
13
 * @author Jakub Konečný
14
 */
15
abstract class FileLoaderTestAbstract extends \Tester\TestCase {
16
  protected FileLoader $loader;
17
  
18
  public function testGetLang(): void {
19
    $lang = $this->loader->lang;
20
    Assert::type("string", $lang);
21
    Assert::same("en", $lang);
22
  }
23
  
24
  public function testSetLang(): void {
25
    $this->loader->lang = "cs";
26
    $lang = $this->loader->lang;
27
    Assert::type("string", $lang);
28
    Assert::same("cs", $lang);
29
  }
30
  
31
  public function testGetFolders(): void {
32
    $folders = $this->loader->folders;
33
    Assert::type("array", $folders);
34
    Assert::count(2, $folders);
35
    Assert::same(__DIR__ . "/../../../lang", $folders[0]);
36
    Assert::same(__DIR__ . "/../../../lang2", $folders[1]);
37
  }
38
  
39
  public function testSetFolders(): void {
40
    Assert::exception(function(): void {
41
      $this->loader->folders = [""];
42
    }, InvalidFolderException::class, "Folder  does not exist.");
43
  }
44
  
45
  public function testGetResources(): void {
46
    // texts were not loaded yet so there are no resources
47
    $resources = $this->loader->resources;
48
    Assert::type("array", $resources);
49
    Assert::count(0, $resources);
50
    // english texts are loaded, there is 1 resource for each domain
51
    $this->loader->getTexts();
0 ignored issues
show
Deprecated Code introduced by
The function Nexendrie\Translation\Lo...\FileLoader::getTexts() has been deprecated: Access the property directly ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

51
    /** @scrutinizer ignore-deprecated */ $this->loader->getTexts();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
52
    $resources = $this->loader->resources;
53
    Assert::type("array", $resources);
54
    Assert::count(3, $resources);
55
    Assert::count(1, $resources["messages"]);
56
    Assert::count(1, $resources["book"]);
57
    Assert::count(1, $resources["abc"]);
58
    // czech and english texts are loaded, there are 2 resources for each domain
59
    $this->loader->lang = "cs";
60
    $this->loader->getTexts();
0 ignored issues
show
Deprecated Code introduced by
The function Nexendrie\Translation\Lo...\FileLoader::getTexts() has been deprecated: Access the property directly ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

60
    /** @scrutinizer ignore-deprecated */ $this->loader->getTexts();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
61
    $resources = $this->loader->resources;
62
    Assert::type("array", $resources);
63
    Assert::count(3, $resources);
64
    Assert::count(2, $resources["messages"]);
65
    Assert::count(2, $resources["book"]);
66
    Assert::count(2, $resources["abc"]);
67
    // the language does not exist, 1 (default) resource for each domain
68
    if($this->loader instanceof MessagesCatalogue) {
69
      return; // the following tests for some reason fail with MessagesCatalogue
70
    }
71
    $this->loader->lang = "xyz";
72
    $this->loader->getTexts();
0 ignored issues
show
Deprecated Code introduced by
The function Nexendrie\Translation\Lo...\FileLoader::getTexts() has been deprecated: Access the property directly ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

72
    /** @scrutinizer ignore-deprecated */ $this->loader->getTexts();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
73
    $resources = $this->loader->resources;
74
    Assert::type("array", $resources);
75
    Assert::count(3, $resources);
76
    Assert::count(1, $resources["messages"]);
77
    Assert::count(1, $resources["book"]);
78
    Assert::count(1, $resources["abc"]);
79
  }
80
  
81
  public function testGetTexts(): void {
82
    $texts = $this->loader->getTexts();
0 ignored issues
show
Deprecated Code introduced by
The function Nexendrie\Translation\Lo...\FileLoader::getTexts() has been deprecated: Access the property directly ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

82
    $texts = /** @scrutinizer ignore-deprecated */ $this->loader->getTexts();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
83
    Assert::type("array", $texts);
84
    Assert::count(3, $texts);
85
    Assert::type("array", $texts["messages"]);
86
    Assert::count(3, $texts["messages"]);
87
    Assert::type("array", $texts["book"]);
88
    Assert::count(5, $texts["book"]);
89
    $this->loader->lang = "cs";
90
    $texts = $this->loader->getTexts();
0 ignored issues
show
Deprecated Code introduced by
The function Nexendrie\Translation\Lo...\FileLoader::getTexts() has been deprecated: Access the property directly ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

90
    $texts = /** @scrutinizer ignore-deprecated */ $this->loader->getTexts();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
91
    Assert::type("array", $texts);
92
    Assert::count(3, $texts);
93
    Assert::type("array", $texts["messages"]);
94
    Assert::count(3, $texts["messages"]);
95
    Assert::type("array", $texts["book"]);
96
    Assert::count(5, $texts["book"]);
97
    if($this->loader instanceof MessagesCatalogue) {
98
      return; // the following tests for some reason fail with MessagesCatalogue
99
    }
100
    $this->loader->lang = "xyz";
101
    $texts = $this->loader->getTexts();
0 ignored issues
show
Deprecated Code introduced by
The function Nexendrie\Translation\Lo...\FileLoader::getTexts() has been deprecated: Access the property directly ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

101
    $texts = /** @scrutinizer ignore-deprecated */ $this->loader->getTexts();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
102
    Assert::type("array", $texts);
103
    Assert::count(3, $texts);
104
    Assert::type("array", $texts["messages"]);
105
    Assert::count(3, $texts["messages"]);
106
    Assert::type("array", $texts["book"]);
107
    Assert::count(5, $texts["book"]);
108
  }
109
  
110
  public function testNoFolder(): void {
111
    Assert::exception(function(): void {
112
      $class = get_class($this->loader);
113
      $this->loader = new $class();
114
      $this->loader->getTexts();
0 ignored issues
show
Deprecated Code introduced by
The function Nexendrie\Translation\Lo...\FileLoader::getTexts() has been deprecated: Access the property directly ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

114
      /** @scrutinizer ignore-deprecated */ $this->loader->getTexts();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
115
    }, FolderNotSetException::class, "Folder for translations was not set.");
116
  }
117
  
118
  public function testGetAvailableLanguages(): void {
119
    $result = $this->loader->getAvailableLanguages();
120
    Assert::type("array", $result);
121
    Assert::count(2, $result);
122
    Assert::contains("en", $result);
123
    Assert::contains("cs", $result);
124
    Assert::exception(function(): void {
125
      $class = get_class($this->loader);
126
      $this->loader = new $class();
127
      $this->loader->getAvailableLanguages();
128
    }, FolderNotSetException::class, "Folder for translations was not set.");
129
  }
130
  
131
  public function testGetResolverName(): void {
132
    $name = $this->loader->getResolverName();
133
    Assert::type("string", $name);
134
    Assert::same("ManualLocaleResolver", $name);
135
  }
136
}
137
?>