Skip to content

Commit ee0e0fc

Browse files
committed
Using some classes with the dic, for writing workable phpunit tests
1 parent 57e9d60 commit ee0e0fc

File tree

5 files changed

+304
-13
lines changed

5 files changed

+304
-13
lines changed

src/Attribute/AttributeTypeFactory.php

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@
2121

2222
namespace MetaModels\AttributeTranslatedFileBundle\Attribute;
2323

24+
use Contao\Config;
25+
use Contao\CoreBundle\Framework\Adapter;
26+
use Contao\FilesModel;
27+
use Contao\StringUtil;
28+
use Contao\Validator;
2429
use Doctrine\DBAL\Connection;
2530
use MetaModels\Attribute\IAttributeTypeFactory;
31+
use MetaModels\Helper\ToolboxFile;
2632

2733
/**
2834
* Attribute type factory for translated combined values attributes.
@@ -35,15 +41,65 @@ class AttributeTypeFactory implements IAttributeTypeFactory
3541
* @var Connection
3642
*/
3743
private $connection;
44+
/**
45+
* The toolbox for file.
46+
*
47+
* @var ToolboxFile
48+
*/
49+
private $toolboxFile;
50+
51+
/**
52+
* The string util.
53+
*
54+
* @var Adapter|StringUtil
55+
*/
56+
private $stringUtil;
57+
58+
/**
59+
* The validator.
60+
*
61+
* @var Adapter|Validator
62+
*/
63+
private $validator;
64+
65+
/**
66+
* The repository for files.
67+
*
68+
* @var Adapter|FilesModel
69+
*/
70+
private $fileRepository;
71+
72+
/**
73+
* The contao configurations.
74+
*
75+
* @var Adapter|Config
76+
*/
77+
private $config;
3878

3979
/**
4080
* Create a new instance.
4181
*
42-
* @param Connection $connection Database connection.
82+
* @param Connection $connection Database connection.
83+
* @param ToolboxFile $toolboxFile The toolbox for file.
84+
* @param Adapter|StringUtil $stringUtil The string util.
85+
* @param Adapter|Validator $validator The validator.
86+
* @param Adapter|FilesModel $fileRepository The repository for files.
87+
* @param Adapter|Config $config The contao configurations.
4388
*/
44-
public function __construct(Connection $connection)
45-
{
46-
$this->connection = $connection;
89+
public function __construct(
90+
Connection $connection,
91+
ToolboxFile $toolboxFile,
92+
Adapter $stringUtil,
93+
Adapter $validator,
94+
Adapter $fileRepository,
95+
Adapter $config
96+
) {
97+
$this->connection = $connection;
98+
$this->toolboxFile = $toolboxFile;
99+
$this->stringUtil = $stringUtil;
100+
$this->validator = $validator;
101+
$this->fileRepository = $fileRepository;
102+
$this->config = $config;
47103
}
48104

49105
/**
@@ -67,7 +123,16 @@ public function getTypeIcon()
67123
*/
68124
public function createInstance($information, $metaModel)
69125
{
70-
return new TranslatedFile($metaModel, $information, $this->connection);
126+
return new TranslatedFile(
127+
$metaModel,
128+
$information,
129+
$this->connection,
130+
$this->toolboxFile,
131+
$this->stringUtil,
132+
$this->validator,
133+
$this->fileRepository,
134+
$this->config
135+
);
71136
}
72137

73138
/**

src/Attribute/TranslatedFile.php

Lines changed: 127 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,139 @@
2929
namespace MetaModels\AttributeTranslatedFileBundle\Attribute;
3030

3131
use Contao\Config;
32+
use Contao\CoreBundle\Framework\Adapter;
3233
use Contao\FilesModel;
3334
use Contao\StringUtil;
35+
use Contao\System;
3436
use Contao\Validator;
37+
use Doctrine\DBAL\Connection;
3538
use MetaModels\Attribute\TranslatedReference;
3639
use MetaModels\Helper\ToolboxFile;
40+
use MetaModels\IMetaModel;
3741
use MetaModels\Render\Template;
3842

3943
/**
4044
* This is the MetaModelAttribute class for handling translated file fields.
4145
*/
4246
class TranslatedFile extends TranslatedReference
4347
{
48+
/**
49+
* The toolbox for file.
50+
*
51+
* @var ToolboxFile|null
52+
*/
53+
private $toolboxFile;
54+
55+
/**
56+
* The string util.
57+
*
58+
* @var Adapter|StringUtil|null
59+
*/
60+
private $stringUtil;
61+
62+
/**
63+
* The validator.
64+
*
65+
* @var Adapter|Validator|null
66+
*/
67+
private $validator;
68+
69+
/**
70+
* The repository for files.
71+
*
72+
* @var Adapter|FilesModel|null
73+
*/
74+
private $fileRepository;
75+
76+
/**
77+
* The contao configurations.
78+
*
79+
* @var Adapter|Config|null
80+
*/
81+
private $config;
82+
83+
/**
84+
* {@inheritDoc}
85+
*
86+
* @param ToolboxFile|null $toolboxFile The toolbox for file.
87+
* @param Adapter|StringUtil|null $stringUtil The string util.
88+
* @param Adapter|Validator|null $validator The validator.
89+
* @param Adapter|FilesModel|null $fileRepository The repository for files.
90+
* @param Adapter|Config|null $config The contao configurations.
91+
*/
92+
public function __construct(
93+
IMetaModel $objMetaModel,
94+
$arrData = [],
95+
Connection $connection = null,
96+
ToolboxFile $toolboxFile = null,
97+
Adapter $stringUtil = null,
98+
Adapter $validator = null,
99+
Adapter $fileRepository = null,
100+
Adapter $config = null
101+
) {
102+
parent::__construct($objMetaModel, $arrData, $connection);
103+
104+
if (null === $toolboxFile) {
105+
// @codingStandardsIgnoreStart
106+
@\trigger_error(
107+
'Toolbox file is missing. It has to be passed in the constructor. Fallback will be dropped.',
108+
E_USER_DEPRECATED
109+
);
110+
// @codingStandardsIgnoreEnd
111+
$toolboxFile = System::getContainer()->get('metamodels.attribute_file.toolbox.file');
112+
}
113+
114+
if (null === $stringUtil) {
115+
// @codingStandardsIgnoreStart
116+
@\trigger_error(
117+
'String util file is missing. It has to be passed in the constructor. Fallback will be dropped.',
118+
E_USER_DEPRECATED
119+
);
120+
// @codingStandardsIgnoreEnd
121+
122+
$stringUtil = System::getContainer()->get('contao.framework')->getAdapter(StringUtil::class);
123+
}
124+
125+
if (null === $validator) {
126+
// @codingStandardsIgnoreStart
127+
@\trigger_error(
128+
'Validator is missing. It has to be passed in the constructor. Fallback will be dropped.',
129+
E_USER_DEPRECATED
130+
);
131+
// @codingStandardsIgnoreEnd
132+
133+
$validator = System::getContainer()->get('contao.framework')->getAdapter(Validator::class);
134+
}
135+
136+
if (null === $fileRepository) {
137+
// @codingStandardsIgnoreStart
138+
@\trigger_error(
139+
'File repository is missing. It has to be passed in the constructor. Fallback will be dropped.',
140+
E_USER_DEPRECATED
141+
);
142+
// @codingStandardsIgnoreEnd
143+
144+
$fileRepository = System::getContainer()->get('contao.framework')->getAdapter(FilesModel::class);
145+
}
146+
147+
if (null === $config) {
148+
// @codingStandardsIgnoreStart
149+
@\trigger_error(
150+
'Config is missing. It has to be passed in the constructor. Fallback will be dropped.',
151+
E_USER_DEPRECATED
152+
);
153+
// @codingStandardsIgnoreEnd
154+
155+
$config = System::getContainer()->get('contao.framework')->getAdapter(Config::class);
156+
}
157+
158+
$this->toolboxFile = $toolboxFile;
159+
$this->stringUtil = $stringUtil;
160+
$this->validator = $validator;
161+
$this->fileRepository = $fileRepository;
162+
$this->config = $config;
163+
}
164+
44165
/**
45166
* {@inheritdoc}
46167
*/
@@ -98,7 +219,7 @@ protected function prepareTemplate(Template $objTemplate, $arrRowData, $objSetti
98219
{
99220
parent::prepareTemplate($objTemplate, $arrRowData, $objSettings);
100221

101-
$toolbox = new ToolboxFile();
222+
$toolbox = clone $this->toolboxFile;
102223
$toolbox
103224
->setBaseLanguage($this->getMetaModel()->getActiveLanguage())
104225
->setFallbackLanguage($this->getMetaModel()->getFallbackLanguage())
@@ -177,10 +298,10 @@ private function handleCustomFileTree(&$fieldDefinition)
177298
// Set root path of file chooser depending on contao version.
178299
$file = null;
179300

180-
if (Validator::isStringUuid($this->get('file_uploadFolder'))
181-
|| Validator::isBinaryUuid($this->get('file_uploadFolder'))
301+
if ($this->validator->isStringUuid($this->get('file_uploadFolder'))
302+
|| $this->validator->isBinaryUuid($this->get('file_uploadFolder'))
182303
) {
183-
$file = FilesModel::findByUuid($this->get('file_uploadFolder'));
304+
$file = $this->fileRepository->findByUuid($this->get('file_uploadFolder'));
184305
}
185306

186307
// Check if we have a file.
@@ -210,7 +331,7 @@ public function getFieldDefinition($arrOverrides = array())
210331

211332
$fieldDefinition['inputType'] = 'fileTree';
212333
$fieldDefinition['eval']['files'] = true;
213-
$fieldDefinition['eval']['extensions'] = Config::get('allowedDownload');
334+
$fieldDefinition['eval']['extensions'] = $this->config->get('allowedDownload');
214335
$fieldDefinition['eval']['multiple'] = (bool) $this->get('file_multiple');
215336

216337
$widgetMode = $this->getOverrideValue('file_widgetMode', $arrOverrides);
@@ -345,7 +466,7 @@ public function getTranslatedDataFor($arrIds, $strLangCode)
345466
$values = parent::getTranslatedDataFor($arrIds, $strLangCode);
346467
foreach ($values as $valueId => $value) {
347468
$values[$valueId]['value'] = ToolboxFile::convertUuidsOrPathsToMetaModels(
348-
StringUtil::deserialize($value['value'], true)
469+
$this->stringUtil->deserialize($value['value'], true)
349470
);
350471
}
351472

src/Resources/config/factory.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ services:
33
class: MetaModels\AttributeTranslatedFileBundle\Attribute\AttributeTypeFactory
44
arguments:
55
- "@database_connection"
6+
- "@metamodels.attribute_file.toolbox.file"
7+
- "@=service('contao.framework').getAdapter('Contao\\\\StringUtil')"
8+
- "@=service('contao.framework').getAdapter('Contao\\\\Validator')"
9+
- "@=service('contao.framework').getAdapter('Contao\\\\FilesModel')"
10+
- "@=service('contao.framework').getAdapter('Contao\\\\Config')"
611
tags:
712
- { name: metamodels.attribute_factory }
813

tests/Attribute/TranslatedFileAttributeTypeFactoryTest.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace MetaModels\AttributeTranslatedFileBundle\Test\Attribute;
2323

24+
use Contao\CoreBundle\Framework\Adapter;
2425
use Doctrine\DBAL\Connection;
2526
use MetaModels\Attribute\Events\CollectMetaModelAttributeInformationEvent;
2627
use MetaModels\Attribute\IAttribute;
@@ -30,6 +31,7 @@
3031
use MetaModels\AttributeTranslatedFileBundle\Attribute\TranslatedFile;
3132
use MetaModels\AttributeTranslatedFileBundle\Attribute\TranslatedFileOrder;
3233
use MetaModels\AttributeTranslatedFileBundle\EventListener\Factory\AddAttributeInformation;
34+
use MetaModels\Helper\ToolboxFile;
3335
use MetaModels\IMetaModel;
3436
use PHPUnit\Framework\TestCase;
3537
use Symfony\Component\EventDispatcher\EventDispatcher;
@@ -126,12 +128,59 @@ private function mockConnection()
126128
->getMock();
127129
}
128130

131+
private function mockToolboxFile()
132+
{
133+
return $this
134+
->getMockBuilder(ToolboxFile::class)
135+
->disableOriginalConstructor()
136+
->getMock();
137+
}
138+
139+
private function mockStringUtil()
140+
{
141+
return $this
142+
->getMockBuilder(Adapter::class)
143+
->disableOriginalConstructor()
144+
->getMock();
145+
}
146+
147+
private function mockValidator()
148+
{
149+
return $this
150+
->getMockBuilder(Adapter::class)
151+
->disableOriginalConstructor()
152+
->getMock();
153+
}
154+
155+
private function mockFileRepository()
156+
{
157+
return $this
158+
->getMockBuilder(Adapter::class)
159+
->disableOriginalConstructor()
160+
->getMock();
161+
}
162+
163+
private function mockConfig()
164+
{
165+
return $this
166+
->getMockBuilder(Adapter::class)
167+
->disableOriginalConstructor()
168+
->getMock();
169+
}
170+
129171
private function mockAttributeFactory($connection)
130172
{
131173
$factory = $this->getMockForAbstractClass('\MetaModels\Attribute\IAttributeFactory');
132174

133175
$factories = [
134-
'translatedfile' => new AttributeTypeFactory($connection),
176+
'translatedfile' => new AttributeTypeFactory(
177+
$connection,
178+
$this->mockToolboxFile(),
179+
$this->mockStringUtil(),
180+
$this->mockValidator(),
181+
$this->mockFileRepository(),
182+
$this->mockConfig()
183+
),
135184
'translatedfilesort' => new AttributeOrderTypeFactory($connection)
136185
];
137186

0 commit comments

Comments
 (0)