Test factory method

Write tests to cover 100% code - this is what we should try to achieve. But I came across a situation where I do not know how to test a method (factory method):

public function getDocument(){
    $document = new Document();
    $document->settings(new Settings());
    $document->filesystem(new Filesystem('e:'));
    return $document;
}

The purpose of this method is a shortcut to create a document, 3 lines are written each time.

How to check this method?

Or maybe this is the situation, why do we have a @codeCoverageIgnoreStart block? For this reason, PHPUnit provides this kind of annotation.


EDIT: The main idea of ​​this method is to make life easier for the client. Nothing more, no configuration, etc. (But the method will be a good place for this).

//I don't want bother client with Settings() and Filesystem('e:')
$document = new Document(new Settings(), new Filesystem()); //NO
$document = Files.getDocument() //much easier and shorter.

//Changing API to getDocument($var, $var) make no sense, the same thing I could have normally.
$document = new Document(new Settings(),new Filesystem('e:'));

Perhaps I should think about whether I really need to provide this method, the user who wants to use the document should know about the dependencies, he should not hide.

+4
4

? Document. , , , , Document Settings Filesystem. , , .

, , . , , , , .

unit testing, unit, . , . , , , - , .

+3

(Document, Settings, Filesystem) , , .

100% - , , .

+3

factory, . mocks .

1

, :

public function getDocument(SettingsFactory $sf, FilesystemFactory $ff){
    $document = new Document();
    $document->settings($sf->getSettings());
    $document->filesystem($ff->getFilesystem());
    return $document;
}

:

  • Settings mock SettingsFactory, getSettings Settings
  • Filesystem mock FilesytemFactory mock, getFilesystem Filesystem
  • DocumentFactory, . , Document
  • , , Document, , mocks

A getSettings getFilesystem Document factory. Factory . , getDocument, getSettings getFilesystem, .

2

:

public function getDocument(Settings $settings, Filesystem $filesystem) {
    $document = new Document();
    $document->settings($settings);
    $document->filesystem($filesystem);
    return $document;
}

:

  • Settings mock
  • Filesystem
  • DocumentFactory, Settings Filesystem. , Document
  • , , Document, - , factory
+1
-2

Source: https://habr.com/ru/post/1607099/


All Articles