Используйте Mock PHPUnit в нетестовом классеPhp

Кемеровские программисты php общаются здесь
Anonymous
Используйте Mock PHPUnit в нетестовом классе

Сообщение Anonymous »

Для одного проекта мне нужно создать макет класса, и я хочу использовать этот макет в своем коде для имитации поведения.
В своем тестовом классе я поместил этот код:

Код: Выделить всё

public function testExportCsv()
{
$mockObject = $this->getMockBuilder('\Client')
->setConstructorArgs(array("0"))
->getMock();
$res = $this->searchDocApiDocumentsStub();
$mockObject->method('searchDocuments')
->willReturn($res);
}

public function searchDocApiDocumentsStub()
{
$res = array();
$yml = Yaml::parse(file_get_contents("../src/ExportCSVBundle/Resources/config/generic.yml"));
$typeDoc = "FAC";
$metas[$typeDoc] = $yml["ETT"][strtoupper($typeDoc)];

foreach ($this->documents as $document) {
if ($document["type"] == "DocumentsAPI\\Model\\" . str_replace('$eq ', '', $typeDoc)) {
foreach ($metas[$typeDoc] as $field) {
$docres[] = $document["metas"][$field];
}
$res = array_merge($res, $docres);
}
}
return $res;
}
В другом классе «Экспорт» мне нужно использовать класс, который я издеваюсь, в атрибуте этого класса есть объект «Клиент», класс, который я издеваюсь.
Тогда мне придется использовать этот объект.

Код: Выделить всё

class Export {
public function __construct(Client $docApiClient)
{
$this->docApiClient = $docApiClient;
}

$docs = $this->docApiClient->searchDocuments($client, $query, null, false, false, $metasToExport);
Я хочу, чтобы этот «searchDocuments» был созданной мной заглушкой.

Код: Выделить всё

$export = new \ExportLibraryBundle\ExportLibrary\Export(//What Do I put ??);
Не знаю, понятно ли я, но спасибо за помощь.

Подробнее здесь: https://stackoverflow.com/questions/439 ... test-class

Вернуться в «Php»