833 lines
33 KiB
PHP
833 lines
33 KiB
PHP
<?php
|
|
|
|
use CodeIgniter\Test\CIUnitTestCase;
|
|
use App\Controllers\CustomerController;
|
|
use App\Models\Customer;
|
|
use App\Models\CustomerAddress;
|
|
use CodeIgniter\HTTP\IncomingRequest;
|
|
// use App\Controllers\Backend\CustomerController;
|
|
|
|
class CustomerAddressTest extends CIUnitTestCase
|
|
{
|
|
protected $controller;
|
|
protected $request;
|
|
protected $customerModelMock;
|
|
protected $customerAddressModelMock;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->controller = new \App\Controllers\Backend\CustomerController();
|
|
$this->controller->initController(
|
|
service('request'),
|
|
service('response'),
|
|
service('logger')
|
|
);
|
|
|
|
$this->request = $this->createMock(IncomingRequest::class);
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'request', $this->request);
|
|
|
|
// Create and inject model mocks
|
|
$this->customerModelMock = $this->createMock(\App\Models\Customer::class);
|
|
$this->customerAddressModelMock = $this->createMock(\App\Models\CustomerAddress::class);
|
|
\Config\Services::injectMock('customer', $this->customerModelMock);
|
|
\Config\Services::injectMock('customeraddress', $this->customerAddressModelMock);
|
|
|
|
}
|
|
|
|
protected function setProtectedOrPrivateProperty($object, $property, $value)
|
|
{
|
|
$reflection = new \ReflectionObject($object);
|
|
while (!$reflection->hasProperty($property)) {
|
|
$reflection = $reflection->getParentClass();
|
|
}
|
|
$prop = $reflection->getProperty($property);
|
|
$prop->setAccessible(true);
|
|
$prop->setValue($object, $value);
|
|
}
|
|
|
|
public function testCreateCustomerAddressReturnsIfCustomerIdMissing()
|
|
{
|
|
$response = $this->controller->createCustomerAddress();
|
|
$this->assertEquals(422, $response->getStatusCode());
|
|
$json = json_decode($response->getBody(), true);
|
|
$this->assertEquals('error', $json['status']);
|
|
$this->assertEquals('Customer ID is required.', $json['message']);
|
|
|
|
}
|
|
|
|
public function testCreateCustomerAddressReturnsIfCustomerNotFound()
|
|
{
|
|
$mockCustomerModel = $this->getMockBuilder(Customer::class)
|
|
->addMethods(['where'])
|
|
->onlyMethods(['first'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$mockCustomerModel->method('where')->willReturnSelf();
|
|
$mockCustomerModel->method('first')->willReturn(null);
|
|
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerModel', $mockCustomerModel);
|
|
|
|
$response = $this->controller->createCustomerAddress(1);
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
|
|
$json = json_decode($response->getBody(), true);
|
|
$this->assertEquals('error', $json['status']);
|
|
$this->assertEquals('Customer with ID 1 not found or not active.', $json['message']);
|
|
|
|
}
|
|
|
|
public function testCreateCustomerAddressReturnsOnSuccess()
|
|
{
|
|
$mockCustomerModel = $this->getMockBuilder(Customer::class)
|
|
->addMethods(['where'])
|
|
->onlyMethods(['first'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$mockCustomerModel->method('where')->willReturnSelf();
|
|
$mockCustomerModel->method('first')->willReturn(['id' => 1, 'status' => 'active']);
|
|
|
|
$mockCustomerAddressModel = $this->getMockBuilder(CustomerAddress::class)
|
|
->onlyMethods(['set', 'update', 'insert'])
|
|
->addMethods(['where'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$mockCustomerAddressModel->method('where')->willReturnSelf();
|
|
$mockCustomerAddressModel->method('set')->willReturnSelf();
|
|
$mockCustomerAddressModel->method('update')->willReturn(true);
|
|
$mockCustomerAddressModel->method('insert')->willReturn(true);
|
|
|
|
$mockRequest = $this->createMock(IncomingRequest::class);
|
|
$mockRequest->method('getVar')->willReturnMap([
|
|
['name', null, 'John'],
|
|
['phone', null, '123456789'],
|
|
['address', null, '123 Main St'],
|
|
['unit', null, 'Unit 5'],
|
|
['note', null, 'Test note'],
|
|
]);
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'request', $mockRequest);
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerModel', $mockCustomerModel);
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mockCustomerAddressModel);
|
|
|
|
$response = $this->controller->createCustomerAddress(1);
|
|
$this->assertEquals(201, $response->getStatusCode());
|
|
|
|
$json = json_decode($response->getBody(), true);
|
|
$this->assertEquals('success', $json['status']);
|
|
$this->assertEquals('Customer address created successfully.', $json['message']);
|
|
}
|
|
|
|
public function testCreateCustomerAddressReturnsOnInsertFailure()
|
|
{
|
|
$mockCustomerModel = $this->getMockBuilder(Customer::class)
|
|
->addMethods(['where'])
|
|
->onlyMethods(['first'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$mockCustomerModel->method('where')->willReturnSelf();
|
|
$mockCustomerModel->method('first')->willReturn(['id' => 1, 'status' => 'active']);
|
|
|
|
$mockCustomerAddressModel = $this->getMockBuilder(CustomerAddress::class)
|
|
->onlyMethods(['set', 'update', 'insert'])
|
|
->addMethods(['where'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$mockCustomerAddressModel->method('where')->willReturnSelf();
|
|
$mockCustomerAddressModel->method('set')->willReturnSelf();
|
|
$mockCustomerAddressModel->method('update')->willReturn(true);
|
|
$mockCustomerAddressModel->method('insert')->willReturn(false);
|
|
|
|
$mockRequest = $this->createMock(IncomingRequest::class);
|
|
$mockRequest->method('getVar')->willReturn('Some value');
|
|
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'request', $mockRequest);
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerModel', $mockCustomerModel);
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mockCustomerAddressModel);
|
|
|
|
|
|
$response = $this->controller->createCustomerAddress(1);
|
|
$this->assertEquals(500, $response->getStatusCode());
|
|
|
|
$json = json_decode($response->getBody(), true);
|
|
$this->assertEquals('error', $json['status']);
|
|
$this->assertEquals('Failed to create customer address.', $json['message']);
|
|
}
|
|
|
|
public function testCustomerAddressIndexReturnsMultipleAddresses()
|
|
{
|
|
// Arrange: Prepare a list of 2 fake addresses
|
|
$fakeAddresses = [
|
|
[
|
|
'id' => 1,
|
|
'customer_id' => 1,
|
|
'name' => 'Home',
|
|
'phone' => '0123456789',
|
|
'address' => '123 Test Street',
|
|
'unit' => 'A-1-1',
|
|
'note' => 'Ring bell twice',
|
|
'is_default' => 1,
|
|
'created_at' => '2025-07-30 10:00:00',
|
|
'updated_at' => '2025-07-30 10:00:00',
|
|
'deleted_at' => null,
|
|
],
|
|
[
|
|
'id' => 2,
|
|
'customer_id' => 1,
|
|
'name' => 'Office',
|
|
'phone' => '0987654321',
|
|
'address' => '456 Work Avenue',
|
|
'unit' => 'B-2-2',
|
|
'note' => 'Reception 3rd floor',
|
|
'is_default' => 0,
|
|
'created_at' => '2025-07-30 11:00:00',
|
|
'updated_at' => '2025-07-30 11:00:00',
|
|
'deleted_at' => null,
|
|
],
|
|
];
|
|
|
|
// Create a mock for CustomerAddress model
|
|
$mockCustomerAddressModel = $this->createMock(\App\Models\CustomerAddress::class);
|
|
$mockCustomerAddressModel
|
|
->expects($this->once())
|
|
->method('findAll')
|
|
->willReturn($fakeAddresses);
|
|
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mockCustomerAddressModel);
|
|
|
|
$response = $this->controller->customerAddressIndex();
|
|
|
|
|
|
$this->assertIsObject($response);
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
$decoded = json_decode($response->getBody(), true);
|
|
|
|
$this->assertEquals('success', $decoded['status']);
|
|
$this->assertEquals('Customer addresses retrieved successfully.', $decoded['message']);
|
|
$this->assertCount(2, $decoded['data']); // Ensure two addresses
|
|
$this->assertEquals($fakeAddresses, $decoded['data']);
|
|
}
|
|
|
|
public function testShowCustomerAddressSuccess()
|
|
{
|
|
$addressData = [
|
|
'id' => 1,
|
|
'customer_id' => 101,
|
|
'name' => 'Home',
|
|
'phone' => '0123456789',
|
|
'address' => '123 Main St',
|
|
'unit' => 'A-1-1',
|
|
'note' => 'Main house',
|
|
'is_default' => 1,
|
|
];
|
|
|
|
$mockCustomerAddressModel = $this->createMock(CustomerAddress::class);
|
|
$mockCustomerAddressModel->method('find')->with(1)->willReturn($addressData);
|
|
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mockCustomerAddressModel);
|
|
|
|
$response = $this->controller->showCustomerAddress(1);
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
$this->assertStringContainsString('Customer address retrieved successfully', $response->getBody());
|
|
}
|
|
|
|
public function testShowCustomerAddressNotFound()
|
|
{
|
|
$mockCustomerAddressModel = $this->createMock(CustomerAddress::class);
|
|
$mockCustomerAddressModel->method('find')->with(999)->willReturn(null);
|
|
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mockCustomerAddressModel);
|
|
|
|
$response = $this->controller->showCustomerAddress(999);
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
$this->assertStringContainsString('not found', $response->getBody());
|
|
}
|
|
|
|
public function testShowCustomerAddressMissingId()
|
|
{
|
|
$this->controller = new \App\Controllers\Backend\CustomerController();
|
|
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'response', \Config\Services::response());
|
|
|
|
$response = $this->controller->showCustomerAddress(null);
|
|
|
|
$this->assertEquals(422, $response->getStatusCode());
|
|
$this->assertStringContainsString('Address ID is required', $response->getBody());
|
|
}
|
|
|
|
public function testShowAddressesByCustomerSuccess()
|
|
{
|
|
$mockModel = $this->getMockBuilder(CustomerAddress::class)
|
|
->onlyMethods(['findAll'])
|
|
->addMethods(['where'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
|
|
$mockModel->method('where')->with('customer_id', 101)->willReturnSelf();
|
|
$mockModel->method('findAll')->willReturn([
|
|
['id' => 1, 'customer_id' => 101, 'address' => '123 Main St'],
|
|
['id' => 2, 'customer_id' => 101, 'address' => '456 Side St']
|
|
]);
|
|
|
|
// $this->controller = new CustomerController();
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mockModel);
|
|
|
|
$response = $this->controller->showAddressesByCustomer(101);
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
$this->assertStringContainsString('Customer addresses retrieved successfully', $response->getBody());
|
|
}
|
|
|
|
public function testShowAddressesByCustomerEmpty()
|
|
{
|
|
$mockModel = $this->getMockBuilder(CustomerAddress::class)
|
|
->onlyMethods(['findAll'])
|
|
->addMethods(['where'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
|
|
$mockModel->method('where')->with('customer_id', 999)->willReturnSelf();
|
|
$mockModel->method('findAll')->willReturn([]);
|
|
|
|
// $this->controller = new CustomerController();
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mockModel);
|
|
|
|
$response = $this->controller->showAddressesByCustomer(999);
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
$this->assertStringContainsString('No addresses found', $response->getBody());
|
|
}
|
|
|
|
public function testShowAddressesByCustomerMissingId()
|
|
{
|
|
$this->controller = new \App\Controllers\Backend\CustomerController();
|
|
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'response', \Config\Services::response());
|
|
|
|
$response = $this->controller->showAddressesByCustomer(null);
|
|
$this->assertEquals(422, $response->getStatusCode());
|
|
$this->assertStringContainsString('Customer ID is required', $response->getBody());
|
|
}
|
|
|
|
public function testUpdateCustomerAddressMissingId()
|
|
{
|
|
$result = $this->controller->updateCustomerAddress(null);
|
|
$this->assertEquals(422, $result->getStatusCode());
|
|
$this->assertStringContainsString('Address ID is required', $result->getJSON());
|
|
}
|
|
|
|
public function testUpdateCustomerAddressInvalidId()
|
|
{
|
|
// Mock CustomerAddress model where 'find' returns null
|
|
$mockAddressModel = $this->createMock(CustomerAddress::class);
|
|
$mockAddressModel->method('find')->with(99)->willReturn(null);
|
|
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mockAddressModel);
|
|
|
|
// Customer model might still be called depending on controller flow — mock it
|
|
$mockCustomerModel = $this->createMock(Customer::class);
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerModel', $mockCustomerModel);
|
|
|
|
$result = $this->controller->updateCustomerAddress(99);
|
|
|
|
$this->assertEquals(404, $result->getStatusCode());
|
|
$this->assertStringContainsString('not found', $result->getBody());
|
|
}
|
|
|
|
|
|
public function testUpdateCustomerAddressCustomerNotFound()
|
|
{
|
|
$mockAddressModel = $this->getMockBuilder(CustomerAddress::class)
|
|
->onlyMethods(['find'])
|
|
->addMethods(['where'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$mockAddressModel->method('find')->willReturn(['customer_id' => 5]);
|
|
|
|
$mockCustomerModel = $this->getMockBuilder(Customer::class)
|
|
->addMethods(['where'])
|
|
->onlyMethods(['first'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$mockCustomerModel->method('where')->willReturnSelf();
|
|
$mockCustomerModel->method('first')->willReturn(null);
|
|
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerModel', $mockCustomerModel);
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mockAddressModel);
|
|
|
|
$result = $this->controller->updateCustomerAddress(1);
|
|
$this->assertEquals(404, $result->getStatusCode());
|
|
$this->assertStringContainsString('Customer not found', $result->getJSON());
|
|
}
|
|
|
|
|
|
public function testUpdateCustomerAddressValidationFail()
|
|
{
|
|
$mockAddressModel = $this->createMock(CustomerAddress::class);
|
|
$mockAddressModel->method('find')->willReturn(['customer_id' => 1]);
|
|
|
|
$mockCustomerModel = $this->getMockBuilder(Customer::class)
|
|
->addMethods(['where'])
|
|
->onlyMethods(['first'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$mockCustomerModel->method('where')->willReturnSelf();
|
|
$mockCustomerModel->method('first')->willReturn(['id' => 1, 'status' => 'active']);
|
|
|
|
// Inject into controller directly
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerModel', $mockCustomerModel);
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mockAddressModel);
|
|
|
|
// Mock request object
|
|
$request = \Config\Services::request();
|
|
$request->setGlobal('post', [
|
|
'name' => '', // validation fail
|
|
'phone' => '',
|
|
'address' => '',
|
|
'is_default' => ''
|
|
]);
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'request', $request);
|
|
|
|
$result = $this->controller->updateCustomerAddress(1);
|
|
$this->assertEquals(422, $result->getStatusCode());
|
|
$this->assertStringContainsString('Validation failed', $result->getJSON());
|
|
}
|
|
|
|
public function testUpdateCustomerAddressSuccess()
|
|
{
|
|
$addressId = 1;
|
|
|
|
$existingAddress = [
|
|
'id' => $addressId,
|
|
'customer_id' => 1,
|
|
'name' => 'Old Address Name',
|
|
'phone' => '0123456789',
|
|
'address' => 'Old Street',
|
|
'unit' => 'B-2-3',
|
|
'note' => 'Old note',
|
|
'is_default' => 0,
|
|
];
|
|
|
|
$mockCustomerAddressModel = $this->getMockBuilder(\App\Models\CustomerAddress::class)
|
|
->onlyMethods(['find', 'update', 'set'])
|
|
->addMethods(['where'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$mockCustomerAddressModel->method('find')->with($addressId)->willReturn($existingAddress);
|
|
$mockCustomerAddressModel->method('update')->willReturn(true);
|
|
$mockCustomerAddressModel->method('where')->willReturnSelf(); // For setting others to is_default = 0
|
|
$mockCustomerAddressModel->method('set')->willReturnSelf();
|
|
|
|
// Mock Customer model
|
|
$mockCustomerModel = $this->getMockBuilder(\App\Models\Customer::class)
|
|
->onlyMethods(['first'])
|
|
->addMethods(['where'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$mockCustomerModel->method('where')->willReturnSelf();
|
|
$mockCustomerModel->method('first')->willReturn([
|
|
'id' => 1,
|
|
'name' => 'Test Customer',
|
|
'status' => 'active',
|
|
'deleted_at' => null,
|
|
]);
|
|
|
|
// Simulate POST data
|
|
$mockRequest = $this->getMockBuilder(IncomingRequest::class)
|
|
->disableOriginalConstructor()
|
|
->onlyMethods(['getVar', 'getMethod'])
|
|
->getMock();
|
|
|
|
$mockRequest->method('getVar')->willReturnCallback(function ($key = null) {
|
|
$data = [
|
|
'name' => 'Updated Address Name',
|
|
'phone' => '0199999999',
|
|
'address' => 'New Street',
|
|
'unit' => 'A-1-1',
|
|
'note' => 'New note',
|
|
'is_default' => 1,
|
|
'customer_id' => 1,
|
|
];
|
|
return $key ? ($data[$key] ?? null) : $data;
|
|
});
|
|
|
|
$mockRequest->method('getMethod')->willReturn('post');
|
|
|
|
// Validation mock
|
|
$mockValidation = $this->getMockBuilder(\CodeIgniter\Validation\Validation::class)
|
|
->disableOriginalConstructor()
|
|
->onlyMethods(['run', 'getErrors'])
|
|
->getMock();
|
|
$mockValidation->method('run')->willReturn(true);
|
|
$mockValidation->method('getErrors')->willReturn([]);
|
|
\Config\Services::injectMock('validation', $mockValidation);
|
|
|
|
// Create controller and inject dependencies
|
|
$controller = new \App\Controllers\Backend\CustomerController();
|
|
$this->setProtectedOrPrivateProperty($controller, 'request', $mockRequest);
|
|
$this->setProtectedOrPrivateProperty($controller, 'customerAddressModel', $mockCustomerAddressModel);
|
|
$this->setProtectedOrPrivateProperty($controller, 'customerModel', $mockCustomerModel);
|
|
$this->setProtectedOrPrivateProperty($controller, 'response', \Config\Services::response());
|
|
|
|
// Run the controller method
|
|
$response = $controller->updateCustomerAddress($addressId);
|
|
|
|
$statusCode = $response->getStatusCode();
|
|
$body = $response->getBody();
|
|
|
|
// var_dump($statusCode);
|
|
// var_dump($body);
|
|
|
|
// Assert the response
|
|
$this->assertSame(200, $response->getStatusCode());
|
|
$decodedBody = json_decode($response->getBody(), true);
|
|
$this->assertEquals('success', $decodedBody['status']);
|
|
$this->assertEquals('Customer address updated successfully.', $decodedBody['message']);
|
|
}
|
|
|
|
|
|
public function testUpdateCustomerAddressFailsToSave()
|
|
{
|
|
$addressId = 1;
|
|
|
|
$existingAddress = [
|
|
'id' => $addressId,
|
|
'customer_id' => 1,
|
|
'name' => 'Old Address Name',
|
|
'phone' => '0123456789',
|
|
'address' => 'Old Street',
|
|
'unit' => 'B-2-3',
|
|
'note' => 'Old note',
|
|
'is_default' => 0,
|
|
];
|
|
|
|
$mockCustomerAddressModel = $this->getMockBuilder(\App\Models\CustomerAddress::class)
|
|
->onlyMethods(['find', 'update', 'set'])
|
|
->addMethods(['where'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$mockCustomerAddressModel->method('find')->with($addressId)->willReturn($existingAddress);
|
|
$mockCustomerAddressModel->method('update')->willReturn(false); // Simulate update failure
|
|
$mockCustomerAddressModel->method('where')->willReturnSelf();
|
|
$mockCustomerAddressModel->method('set')->willReturnSelf();
|
|
|
|
$mockCustomerModel = $this->getMockBuilder(\App\Models\Customer::class)
|
|
->onlyMethods(['first'])
|
|
->addMethods(['where'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$mockCustomerModel->method('where')->willReturnSelf();
|
|
$mockCustomerModel->method('first')->willReturn([
|
|
'id' => 1,
|
|
'name' => 'Test Customer',
|
|
'status' => 'active',
|
|
'deleted_at' => null,
|
|
]);
|
|
|
|
$mockRequest = $this->getMockBuilder(IncomingRequest::class)
|
|
->disableOriginalConstructor()
|
|
->onlyMethods(['getVar', 'getMethod'])
|
|
->getMock();
|
|
|
|
$mockRequest->method('getVar')->willReturnCallback(function ($key = null) {
|
|
$data = [
|
|
'name' => 'Updated Address Name',
|
|
'phone' => '0199999999',
|
|
'address' => 'New Street',
|
|
'unit' => 'A-1-1',
|
|
'note' => 'New note',
|
|
'is_default' => 1,
|
|
'customer_id' => 1,
|
|
];
|
|
return $key ? ($data[$key] ?? null) : $data;
|
|
});
|
|
|
|
$mockRequest->method('getMethod')->willReturn('post');
|
|
|
|
$mockValidation = $this->getMockBuilder(\CodeIgniter\Validation\Validation::class)
|
|
->disableOriginalConstructor()
|
|
->onlyMethods(['run', 'getErrors'])
|
|
->getMock();
|
|
$mockValidation->method('run')->willReturn(true);
|
|
$mockValidation->method('getErrors')->willReturn([]);
|
|
\Config\Services::injectMock('validation', $mockValidation);
|
|
|
|
$controller = new \App\Controllers\Backend\CustomerController();
|
|
$this->setProtectedOrPrivateProperty($controller, 'request', $mockRequest);
|
|
$this->setProtectedOrPrivateProperty($controller, 'customerAddressModel', $mockCustomerAddressModel);
|
|
$this->setProtectedOrPrivateProperty($controller, 'customerModel', $mockCustomerModel);
|
|
$this->setProtectedOrPrivateProperty($controller, 'response', \Config\Services::response());
|
|
|
|
$response = $controller->updateCustomerAddress($addressId);
|
|
|
|
$this->assertSame(500, $response->getStatusCode());
|
|
|
|
$decoded = json_decode($response->getBody(), true);
|
|
$this->assertEquals('error', $decoded['status']);
|
|
$this->assertEquals('Failed to update customer address.', $decoded['message']);
|
|
|
|
}
|
|
|
|
public function testDeleteCustomerAddressMissingId()
|
|
{
|
|
$response = $this->controller->deleteCustomerAddress(null);
|
|
$this->assertEquals(422, $response->getStatusCode());
|
|
$this->assertStringContainsString('ID is required', $response->getJSON());
|
|
}
|
|
|
|
public function testDeleteCustomerAddressNotFound()
|
|
{
|
|
$mockCustomerAddressModel = $this->getMockBuilder(\App\Models\CustomerAddress::class)
|
|
->onlyMethods(['find'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
|
|
$mockCustomerAddressModel->method('find')->willReturn(null);
|
|
|
|
// Inject mock into private property
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mockCustomerAddressModel);
|
|
|
|
// Call the method and assert response
|
|
$response = $this->controller->deleteCustomerAddress(999);
|
|
|
|
|
|
// Assert
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
$decodedBody = json_decode($response->getBody(), true);
|
|
$this->assertEquals('error', $decodedBody['status']);
|
|
$this->assertEquals('Customer address with ID 999 not found.', $decodedBody['message']);
|
|
}
|
|
|
|
public function testDeleteCustomerAddressSuccessNonDefault()
|
|
{
|
|
$addressId = 1;
|
|
$address = [
|
|
'id' => $addressId,
|
|
'customer_id' => 1,
|
|
'is_default' => 0,
|
|
];
|
|
|
|
$mock = $this->createMock(CustomerAddress::class);
|
|
$mock->method('find')->with($addressId)->willReturn($address);
|
|
$mock->method('delete')->with($addressId)->willReturn(true);
|
|
|
|
// Inject mock using reflection
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mock);
|
|
|
|
$response = $this->controller->deleteCustomerAddress($addressId);
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
$this->assertStringContainsString('deleted successfully', $response->getJSON());
|
|
}
|
|
|
|
public function testDeleteCustomerAddressSuccessDefault()
|
|
{
|
|
$addressId = 1;
|
|
$customerId = 101;
|
|
$defaultAddress = [
|
|
'id' => $addressId,
|
|
'customer_id' => $customerId,
|
|
'is_default' => 1,
|
|
];
|
|
|
|
$anotherAddress = [
|
|
'id' => 2,
|
|
'customer_id' => $customerId,
|
|
'is_default' => 0,
|
|
];
|
|
|
|
$mock = $this->getMockBuilder(CustomerAddress::class)
|
|
->onlyMethods(['find', 'delete', 'first', 'update'])
|
|
->addMethods(['where', 'orderBy'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
|
|
$mock->method('find')->with($addressId)->willReturn($defaultAddress);
|
|
$mock->method('delete')->with($addressId)->willReturn(true);
|
|
$mock->method('where')->willReturnSelf();
|
|
$mock->method('orderBy')->willReturnSelf();
|
|
$mock->method('first')->willReturn($anotherAddress);
|
|
$mock->method('update')->with($anotherAddress['id'], ['is_default' => 1])->willReturn(true);
|
|
|
|
// Inject mock using reflection
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mock);
|
|
|
|
|
|
$response = $this->controller->deleteCustomerAddress($addressId);
|
|
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
$this->assertStringContainsString('deleted successfully', $response->getJSON());
|
|
}
|
|
|
|
|
|
public function testDeleteCustomerAddressFailsToDelete()
|
|
{
|
|
$addressId = 1;
|
|
$address = [
|
|
'id' => $addressId,
|
|
'customer_id' => 1,
|
|
'is_default' => 0,
|
|
];
|
|
|
|
$mock = $this->createMock(CustomerAddress::class);
|
|
$mock->method('find')->with($addressId)->willReturn($address);
|
|
$mock->method('delete')->with($addressId)->willReturn(false);
|
|
|
|
// Inject mock using reflection
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $mock);
|
|
|
|
$response = $this->controller->deleteCustomerAddress($addressId);
|
|
$this->assertEquals(500, $response->getStatusCode());
|
|
$this->assertStringContainsString('Failed to delete', $response->getJSON());
|
|
}
|
|
|
|
|
|
public function testSetDefaultCustomerAddressSuccess()
|
|
{
|
|
$customerId = 123;
|
|
$addressId = 456;
|
|
|
|
// Mock request
|
|
$this->request->method('getVar')->with('customer_id')->willReturn($customerId);
|
|
|
|
$this->customerModelMock = $this->getMockBuilder(\App\Models\Customer::class)
|
|
->addMethods(['where'])
|
|
->onlyMethods(['first'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
|
|
$this->customerModelMock->method('where')->willReturnSelf();
|
|
$this->customerModelMock->method('first')->willReturn([
|
|
'id' => $customerId,
|
|
'status' => 'active',
|
|
]);
|
|
|
|
// Mock address model
|
|
$this->customerAddressModelMock = $this->getMockBuilder(\App\Models\CustomerAddress::class)
|
|
->addMethods(['where'])
|
|
->onlyMethods(['first', 'set', 'update'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
|
|
$this->customerAddressModelMock->method('where')->willReturnSelf();
|
|
$this->customerAddressModelMock->method('first')->willReturn([
|
|
'id' => $addressId,
|
|
'customer_id' => $customerId,
|
|
]);
|
|
|
|
$this->customerAddressModelMock->method('set')->willReturnSelf();
|
|
$this->customerAddressModelMock->method('update')->willReturn(true);
|
|
|
|
// Inject mocks into service container
|
|
\Config\Services::injectMock('customer', $this->customerModelMock);
|
|
\Config\Services::injectMock('customeraddress', $this->customerAddressModelMock);
|
|
// Set mocked model into the controller before calling the method
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerModel', $this->customerModelMock);
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $this->customerAddressModelMock);
|
|
|
|
// Capture response
|
|
$response = $this->controller->setDefaultCustomerAddress($addressId);
|
|
|
|
$responseArray = json_decode($response->getBody(), true);
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
$this->assertEquals('success', $responseArray['status']);
|
|
$this->assertEquals("Address ID {$addressId} is now the default for customer ID {$customerId}.", $responseArray['message']);
|
|
$this->assertEquals($customerId, $responseArray['data']['customer_id']);
|
|
$this->assertEquals($addressId, $responseArray['data']['address_id']);
|
|
}
|
|
|
|
public function testSetDefaultCustomerAddressMissingCustomerId()
|
|
{
|
|
// Arrange
|
|
$addressId = 456;
|
|
|
|
// Simulate request missing 'customer_id'
|
|
$this->request->method('getVar')->with('customer_id')->willReturn(null);
|
|
|
|
// Act
|
|
$response = $this->controller->setDefaultCustomerAddress($addressId);
|
|
$responseArray = json_decode($response->getBody(), true);
|
|
|
|
// Assert
|
|
$this->assertEquals(422, $response->getStatusCode());
|
|
$this->assertEquals('error', $responseArray['status']);
|
|
$this->assertEquals('Customer ID is required.', $responseArray['message']);
|
|
$this->assertNull($responseArray['data']);
|
|
}
|
|
|
|
public function testSetDefaultCustomerAddressFailsWhenCustomerNotFound()
|
|
{
|
|
$customerId = 123;
|
|
$addressId = 456;
|
|
|
|
// Mock request
|
|
$this->request->method('getVar')->with('customer_id')->willReturn($customerId);
|
|
|
|
// Mock customer model to return null
|
|
$this->customerModelMock = $this->getMockBuilder(\App\Models\Customer::class)
|
|
->addMethods(['where'])
|
|
->onlyMethods(['first'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
|
|
$this->customerModelMock->method('where')->willReturnSelf();
|
|
$this->customerModelMock->method('first')->willReturn(null); // customer not found
|
|
|
|
\Config\Services::injectMock('customer', $this->customerModelMock);
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerModel', $this->customerModelMock);
|
|
|
|
$response = $this->controller->setDefaultCustomerAddress($addressId);
|
|
$responseArray = json_decode($response->getBody(), true);
|
|
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
$this->assertEquals('error', $responseArray['status']);
|
|
$this->assertEquals('Customer not found or inactive.', $responseArray['message']);
|
|
$this->assertNull($responseArray['data']);
|
|
}
|
|
|
|
public function testSetDefaultCustomerAddressFailsWhenAddressNotFound()
|
|
{
|
|
$customerId = 123;
|
|
$addressId = 456;
|
|
|
|
$this->request->method('getVar')->with('customer_id')->willReturn($customerId);
|
|
|
|
// Mock valid customer
|
|
$this->customerModelMock = $this->getMockBuilder(\App\Models\Customer::class)
|
|
->addMethods(['where'])
|
|
->onlyMethods(['first'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$this->customerModelMock->method('where')->willReturnSelf();
|
|
$this->customerModelMock->method('first')->willReturn([
|
|
'id' => $customerId,
|
|
'status' => 'active',
|
|
]);
|
|
|
|
// Mock address not found
|
|
$this->customerAddressModelMock = $this->getMockBuilder(\App\Models\CustomerAddress::class)
|
|
->addMethods(['where'])
|
|
->onlyMethods(['first'])
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$this->customerAddressModelMock->method('where')->willReturnSelf();
|
|
$this->customerAddressModelMock->method('first')->willReturn(null);
|
|
|
|
\Config\Services::injectMock('customer', $this->customerModelMock);
|
|
\Config\Services::injectMock('customeraddress', $this->customerAddressModelMock);
|
|
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerModel', $this->customerModelMock);
|
|
$this->setProtectedOrPrivateProperty($this->controller, 'customerAddressModel', $this->customerAddressModelMock);
|
|
|
|
$response = $this->controller->setDefaultCustomerAddress($addressId);
|
|
$responseArray = json_decode($response->getBody(), true);
|
|
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
$this->assertEquals('error', $responseArray['status']);
|
|
$this->assertEquals('Address not found or does not belong to the customer.', $responseArray['message']);
|
|
$this->assertNull($responseArray['data']);
|
|
}
|
|
|
|
}
|
|
|
|
|