Files
cake-addresses/tests/TestCase/Controller/AddressesControllerTest.php

336 lines
7.3 KiB
PHP
Raw Normal View History

2025-11-18 00:43:34 -08:00
<?php
declare(strict_types=1);
namespace CakeAddresses\Test\TestCase\Controller;
/**
* CakeAddresses\Controller\AddressesController Test Case
*
* @uses \CakeAddresses\Controller\AddressesController
*/
2026-01-23 18:36:15 -08:00
class AddressesControllerTest extends BaseControllerTest {
/**
2025-11-18 00:43:34 -08:00
* Addresses Table
*
* @var \CakeAddresses\Model\Table\AddressesTable
*/
2026-01-23 18:36:15 -08:00
protected $Addresses;
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
/**
2025-11-18 00:43:34 -08:00
* Fixtures
*
* @var array<string>
*/
2026-01-23 18:36:15 -08:00
protected array $fixtures = [
'plugin.CakeAddresses.Addresses',
2025-11-18 00:43:34 -08:00
// 'plugin.CakeAddresses.Cities',
2026-01-23 18:36:15 -08:00
'plugin.CakeAddresses.Countries',
2025-11-18 00:43:34 -08:00
// 'plugin.CakeAddresses.Regions',
2026-01-23 18:36:15 -08:00
'plugin.CakeAddresses.States',
2025-11-18 00:43:34 -08:00
// 'plugin.CakeAddresses.Subregions',
2026-01-23 18:36:15 -08:00
];
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
/**
2025-11-18 00:43:34 -08:00
* setUp method
*
* @return void
*/
2026-01-23 18:36:15 -08:00
protected function setUp(): void {
parent::setUp();
$this->Addresses = $this->getTableLocator()->get('CakeAddresses.Addresses');
2025-11-18 00:52:28 -08:00
// $this->enableCsrfToken();
2026-01-23 18:36:15 -08:00
}
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
/**
2025-11-18 00:43:34 -08:00
* tearDown method
*
* @return void
*/
2026-01-23 18:36:15 -08:00
protected function tearDown(): void {
unset($this->Addresses);
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
parent::tearDown();
}
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
/**
2025-11-18 00:43:34 -08:00
* Test index method
*
* Tests the index action with an unauthenticated user (not logged in)
*
* @uses \CakeAddresses\Controller\AddressesController::index()
2026-01-23 18:36:15 -08:00
* @throws \PHPUnit\Exception
2025-11-18 00:43:34 -08:00
*
* @return void
*/
2026-01-23 18:36:15 -08:00
public function testIndexGet(): void {
$url = [
'plugin' => 'CakeAddresses',
'controller' => 'Addresses',
'action' => 'index',
];
$this->get($url);
$this->assertResponseCode(200);
}
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
/**
2025-11-18 00:43:34 -08:00
* Test view method
*
* Tests the view action with an unauthenticated user (not logged in)
*
* @uses \CakeAddresses\Controller\AddressesController::view()
2026-01-23 18:36:15 -08:00
* @throws \PHPUnit\Exception
2025-11-18 00:43:34 -08:00
*
* @return void
*/
2026-01-23 18:36:15 -08:00
public function testViewGet(): void {
$id = 1;
$url = [
'plugin' => 'CakeAddresses',
'controller' => 'Addresses',
'action' => 'view',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
/**
2025-11-18 00:43:34 -08:00
* Test add method
*
* Tests the add action with an unauthenticated user (not logged in)
*
* @uses \CakeAddresses\Controller\AddressesController::add()
2026-01-23 18:36:15 -08:00
* @throws \PHPUnit\Exception
2025-11-18 00:43:34 -08:00
*
* @return void
*/
2026-01-23 18:36:15 -08:00
public function testAddGet(): void {
$cntBefore = $this->Addresses->find()->count();
$url = [
'plugin' => 'CakeAddresses',
'controller' => 'Addresses',
'action' => 'add',
];
$this->get($url);
$this->assertResponseCode(200);
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
$cntAfter = $this->Addresses->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
/**
2025-11-18 00:43:34 -08:00
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @uses \CakeAddresses\Controller\AddressesController::add()
2026-01-23 18:36:15 -08:00
* @throws \PHPUnit\Exception
2025-11-18 00:43:34 -08:00
*
* @return void
*/
2026-01-23 18:36:15 -08:00
public function testAddPostSuccess(): void {
$cntBefore = $this->Addresses->find()->count();
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
$this->loginUserByRole();
$url = [
'plugin' => 'CakeAddresses',
'controller' => 'Addresses',
'action' => 'add',
];
$data = [
'address_name' => 'new address',
'contact_name' => 'john doe',
'address_line1' => '123 Test ST',
'address_line2' => '',
'city' => 'Seattle',
'city_id' => '',
'state_id' => 1462,
'postal_code' => '98056',
'country_id' => 233,
'phone_number' => '',
'email' => '',
'notes' => '',
];
$this->post($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('addresses');
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
$cntAfter = $this->Addresses->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
}
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
/**
2025-11-18 00:43:34 -08:00
* Test add method
*
* Tests a POST request to the add action with a logged in user
*
* @uses \CakeAddresses\Controller\AddressesController::add()
2026-01-23 18:36:15 -08:00
* @throws \PHPUnit\Exception
2025-11-18 00:43:34 -08:00
*
* @return void
*/
2026-01-23 18:36:15 -08:00
public function testAddPostFailure(): void {
$cntBefore = $this->Addresses->find()->count();
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
$this->loginUserByRole();
$url = [
'plugin' => 'CakeAddresses',
'controller' => 'Addresses',
'action' => 'add',
];
$data = [
'address_name' => '',
'contact_name' => '',
'address_line1' => '',
'address_line2' => '',
'city' => 'Seattle',
'city_id' => '',
'state_id' => 1462,
'postal_code' => '98056',
'country_id' => 233,
'phone_number' => '',
'email' => '',
'notes' => '',
];
$this->post($url, $data);
$this->assertResponseCode(200);
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
$cntAfter = $this->Addresses->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
}
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
/**
2025-11-18 00:43:34 -08:00
* Test edit method
*
* Tests the edit action with an unauthenticated user (not logged in)
*
* @uses \CakeAddresses\Controller\AddressesController::edit()
2026-01-23 18:36:15 -08:00
* @throws \PHPUnit\Exception
2025-11-18 00:43:34 -08:00
*
* @return void
*/
2026-01-23 18:36:15 -08:00
public function testEditGet(): void {
$id = 1;
$url = [
'plugin' => 'CakeAddresses',
'controller' => 'Addresses',
'action' => 'edit',
$id,
];
$this->get($url);
$this->assertResponseCode(200);
}
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
/**
2025-11-18 00:43:34 -08:00
* Test edit method
*
* Tests a PUT request to the edit action with a logged in user
*
* @uses \CakeAddresses\Controller\AddressesController::edit()
2026-01-23 18:36:15 -08:00
* @throws \PHPUnit\Exception
2025-11-18 00:43:34 -08:00
*
* @return void
*/
2026-01-23 18:36:15 -08:00
public function testEditPutSuccess(): void {
$this->loginUserByRole();
$id = 1;
$before = $this->Addresses->get($id);
$url = [
'plugin' => 'CakeAddresses',
'controller' => 'Addresses',
'action' => 'edit',
$id,
];
$data = [
// test new data here
'address_name' => 'new address',
'contact_name' => 'john doe',
'address_line1' => '123 Test ST',
'address_line2' => '',
'city' => 'Seattle',
'city_id' => '',
'state_id' => 1462,
'postal_code' => '98056',
'country_id' => 233,
'phone_number' => '',
'email' => '',
'notes' => '',
];
$this->put($url, $data);
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
$this->assertResponseCode(302);
$this->assertRedirectContains('addresses');
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
$after = $this->Addresses->get($id);
// assert saved properly below
}
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
/**
2025-11-18 00:43:34 -08:00
* Test edit method
*
* Tests a PUT request to the edit action with a logged in user
*
* @uses \CakeAddresses\Controller\AddressesController::edit()
2026-01-23 18:36:15 -08:00
* @throws \PHPUnit\Exception
2025-11-18 00:43:34 -08:00
*
* @return void
*/
2026-01-23 18:36:15 -08:00
public function testEditPutFailure(): void {
$id = 1;
$before = $this->Addresses->get($id);
$url = [
'plugin' => 'CakeAddresses',
'controller' => 'Addresses',
'action' => 'edit',
$id,
];
$data = [
'address_name' => '',
'contact_name' => '',
'address_line1' => '',
'address_line2' => '',
'city' => 'Seattle',
'city_id' => '',
'state_id' => 1462,
'postal_code' => '98056',
'country_id' => 233,
'phone_number' => '',
'email' => '',
'notes' => '',
];
$this->put($url, $data);
$this->assertResponseCode(200);
$after = $this->Addresses->get($id);
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
// assert save failed below
}
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
/**
2025-11-18 00:43:34 -08:00
* Test delete method
*
* Tests the delete action with an unauthenticated user (not logged in)
*
* @uses \CakeAddresses\Controller\AddressesController::delete()
2026-01-23 18:36:15 -08:00
* @throws \PHPUnit\Exception
2025-11-18 00:43:34 -08:00
*
* @return void
*/
2026-01-23 18:36:15 -08:00
public function testDelete(): void {
$cntBefore = $this->Addresses->find()->count();
$url = [
'plugin' => 'CakeAddresses',
'controller' => 'Addresses',
'action' => 'delete',
1,
];
$this->delete($url);
$this->assertResponseCode(302);
2025-11-18 00:43:34 -08:00
2026-01-23 18:36:15 -08:00
$cntAfter = $this->Addresses->find()->count();
$this->assertEquals($cntBefore - 1, $cntAfter);
}
2025-11-18 00:43:34 -08:00
}