I have a problem with writing test for pdf generation. I have controller, service and repository, so service is injected in my controller and it's calling method which is part of repository class. My test always returns error 500, probably because missing dependencies. Does anybody knows how to make test passing?
This is the code:
<?php
namespace Smart\Http\Controllers\Warehouse\Article;
use Barryvdh\DomPDF\Facade as PDF;
use Smart\Http\Controllers\Controller;
use Smart\Models\Warehouse\Article;
use Smart\Services\Warehouse\ArticleService;
use Smart\Services\Warehouse\WarehouseService;
class PdfProfileController extends Controller
{
/**
* @var ArticleService
*/
private ArticleService $articleService;
/**
* PdfProfileController constructor.
*
* @param ArticleService $articleService
*/
public function __construct(ArticleService $articleService)
{
$this->articleService = $articleService;
}
/**
* @param Article $article
* @param string|null $report
*
* @return mixed
*/
public function __invoke(Article $article, string $report = null)
{
$articlesByCenterAndWarehouse = $this->articleService->warehousesOfArticle($article->id);
$pdf = PDF::loadView('warehouses.articles.templates.pdf-single-report', [
'article' => $article,
'warehouses' => $articlesByCenterAndWarehouse,
'stateOnDate' => request()->date,
'warehouseService' => app(WarehouseService::class),
])
->setPaper('A4', 'portrait');
return $pdf->stream($article->name.'.pdf');
}
}
<?php
namespace Smart\Services\Warehouse;
use Illuminate\Http\Request;
use Smart\Exceptions\General;
use Smart\Models\Warehouse\Article;
use Smart\Repositories\Warehouse\ArticleRepository;
class ArticleService
{
/**
* @var ArticleRepository
*/
private ArticleRepository $articleRepository;
/**
* ArticleService constructor.
*
* @param ArticleRepository $articleRepository
*/
public function __construct(ArticleRepository $articleRepository)
{
$this->articleRepository = $articleRepository;
}
/**
* @param int $id
*
* @return mixed
*/
public function warehousesOfArticle(int $id)
{
return $this->articleRepository->getWarehousesOfArticle($id);
}
}
<?php
namespace Smart\Repositories\Warehouse;
use Illuminate\Support\Facades\DB;
use Smart\Models\Center\Building;
use Smart\Models\Center\Room;
use Smart\Models\Codebooks\PurposeOfBuilding;
use Smart\Models\Codebooks\PurposeOfRoom;
use Smart\Models\Warehouse\Article;
use Smart\Repositories\BaseRepository;
class ArticleRepository extends BaseRepository
{
/**
* ArticleRepository constructor.
*
* @param Article $model
*/
public function __construct(Article $model)
{
$this->model = $model;
}
/**
* @param int $id
*
* @return mixed
*/
public function getWarehousesOfArticle(int $id)
{
$articleIncoming = DB::table('article_incoming_document')
->where('article_id', $id)
->select('article_id', 'warehouse as warehouse_id', 'warehouse_type', DB::raw('sum(quantity) as quantity'))
->groupBy(['article_id', 'warehouse', 'warehouse_type']);
$articleOutgoing = DB::table('article_outgoing_document')
->where('article_id', $id)
->select('article_id', 'warehouse as warehouse_id', 'warehouse_type', DB::raw('sum(quantity) as quantity'))
->groupBy(['article_id', 'warehouse', 'warehouse_type']);
$rooms = DB::table('rooms')
->select([
'rooms.id',
'rooms.name as warehouse',
'centers.name as center',
DB::raw('coalesce(incoming.quantity,0) - coalesce(outgoing.quantity,0) as quantity')
])
->selectRaw('"Smart\\\Models\\\Center\\\Room" as type')
->join('buildings', 'buildings.id', '=', 'rooms.building_id')
->join('centers', 'centers.id', '=', 'buildings.center_id')
->leftJoin('purpose_of_rooms', 'purpose_of_rooms.id', '=', 'rooms.purpose_of_room_id')
->where('purpose_of_rooms.name', PurposeOfRoom::WAREHOUSE_ROOM)
->leftJoinSub($articleIncoming, 'incoming', function ($join) {
$join->on('rooms.id', '=', 'incoming.warehouse_id')
->where('incoming.warehouse_type', '=', Room::class);
})
->leftJoinSub($articleOutgoing, 'outgoing', function ($join) {
$join->on('rooms.id', '=', 'outgoing.warehouse_id')
->where('outgoing.warehouse_type', '=', Room::class);
})
->having('quantity', '>', 0);
$warehouses = DB::table('buildings')
->select([
'buildings.id',
'buildings.name as warehouse',
'centers.name as center',
DB::raw('coalesce(incoming.quantity,0) - coalesce(outgoing.quantity,0) as quantity')
])
->selectRaw('"Smart\\\Models\\\Center\\\Building" as type')
->join('centers', 'centers.id', '=', 'buildings.center_id')
->join('purpose_of_buildings', 'purpose_of_buildings.id', '=', 'buildings.purpose_of_building_id')
->where('purpose_of_buildings.name', PurposeOfBuilding::WAREHOUSE_BUILDING)
->leftJoinSub($articleIncoming, 'incoming', function ($join) {
$join->on('buildings.id', '=', 'incoming.warehouse_id')
->where('incoming.warehouse_type', '=', Building::class);
})
->leftJoinSub($articleOutgoing, 'outgoing', function ($join) {
$join->on('buildings.id', '=', 'outgoing.warehouse_id')
->where('outgoing.warehouse_type', '=', Building::class);
})
->having('quantity', '>', 0)
->union($rooms);
return DB::query()->fromSub($warehouses, 'warehouses')->get();
}
}