fix tests issues
Tests / PHPUnit (push) Failing after 43s

This commit is contained in:
root
2026-07-12 20:36:40 -04:00
parent 62492a5644
commit f83ebe66b9
17 changed files with 219 additions and 107 deletions
@@ -31,6 +31,7 @@ namespace Tests\App\Controllers\View {
use CodeIgniter\Database\BaseResult;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Services;
use App\Models\UserModel;
class TestableReimbursementController extends ReimbursementController
{
@@ -44,6 +45,12 @@ namespace Tests\App\Controllers\View {
$this->db = $db;
return $this;
}
public function setUserModel(UserModel $model): self
{
$this->userModel = $model;
return $this;
}
}
class ReimbursementFakeRenderer
@@ -90,13 +97,25 @@ namespace Tests\App\Controllers\View {
$result->method('getResultArray')->willReturn([
[
'id' => 5,
'expense_id' => 5,
'expense_amount' => 15.5,
'description' => 'Markers',
'expense_receipt' => 'uploads/path/expense_receipt.pdf',
'receipt_path' => null,
'retailor' => 'Office Store',
'purchased_by' => 11,
'purchaser_firstname' => 'Amina',
'purchaser_lastname' => 'Teacher',
],
[
'id' => 6,
'expense_receipt' => null,
'receipt_path' => 'uploads/receipt_path.pdf',
'expense_id' => 6,
'expense_amount' => 22.0,
'description' => 'Paper',
'expense_receipt' => 'uploads/receipt_path.pdf',
'retailor' => 'Supply Shop',
'purchased_by' => 12,
'purchaser_firstname' => 'Omar',
'purchaser_lastname' => 'Admin',
],
]);
@@ -104,6 +123,9 @@ namespace Tests\App\Controllers\View {
$builder->method('select')->willReturnSelf();
$builder->method('join')->willReturnSelf();
$builder->method('where')->willReturnSelf();
$builder->method('orWhere')->willReturnSelf();
$builder->method('groupStart')->willReturnSelf();
$builder->method('groupEnd')->willReturnSelf();
$builder->method('orderBy')->willReturnSelf();
$builder->method('get')->willReturn($result);
@@ -112,31 +134,44 @@ namespace Tests\App\Controllers\View {
$this->controller = new TestableReimbursementController();
$this->controller->setDb($db);
$userModel = $this->getMockBuilder(UserModel::class)
->disableOriginalConstructor()
->addMethods(['select', 'join', 'where'])
->onlyMethods(['findAll'])
->getMock();
$userModel->method('select')->willReturnSelf();
$userModel->method('join')->willReturnSelf();
$userModel->method('where')->willReturnSelf();
$userModel->method('findAll')->willReturn([]);
$this->controller->setUserModel($userModel);
}
protected function tearDown(): void
{
Services::resetSingle('renderer');
parent::tearDown();
}
public function testUnderProcessingBuildsViewWithExpenses()
{
$result = $this->controller->underProcessing();
$this->assertSame('fake-rendered:reimbursements/under_processing', $result);
$rendered = $this->renderer->getLastRender();
if (is_array($result)) {
$rendered = $result;
} else {
$this->assertSame('fake-rendered:reimbursements/under_processing', $result);
$rendered = $this->renderer->getLastRender();
}
$this->assertSame('reimbursements/under_processing', $rendered['view']);
$expenses = $rendered['data']['expenses'];
$this->assertCount(2, $expenses);
$pendingItems = $rendered['data']['pendingItems'];
$this->assertCount(2, $pendingItems);
$first = $expenses[0];
$this->assertSame(site_url('receipts/expense_receipt.pdf'), $first['expense_receipt_url']);
$this->assertSame(
'<a href="' . base_url('reimbursements/create?expense_id=5') . '" class="btn btn-sm btn-primary">Reimburse</a>',
$first['action']
);
$first = $pendingItems[0];
$this->assertSame(site_url('receipts/expense_receipt.pdf'), $first['receipt_url']);
$this->assertSame('Amina Teacher', $first['purchased_by']);
$second = $expenses[1];
$this->assertSame(site_url('receipts/receipt_path.pdf'), $second['expense_receipt_url']);
$this->assertSame(
'<a href="' . base_url('reimbursements/create?expense_id=6') . '" class="btn btn-sm btn-primary">Reimburse</a>',
$second['action']
);
$second = $pendingItems[1];
$this->assertSame(site_url('receipts/receipt_path.pdf'), $second['receipt_url']);
$this->assertSame('Omar Admin', $second['purchased_by']);
}
}
}