using phpunit to test controllers which are running in codeigniter3 requires further request/response mocking; setup of swagger-ui and swagger openapi doc generation for bookmark.php; WIP integration tests with ci3 & phpunit, maybe annotation generation solution for swagger annotations

This commit is contained in:
Szabo Mónika
2025-01-30 09:35:06 +01:00
committed by Johann Hoffmann
parent 37fa899540
commit 664796c69f
13 changed files with 558 additions and 83 deletions
@@ -0,0 +1,80 @@
<?php
// tests/Integration/ApiControllerTest.php
use PHPUnit\Framework\TestCase;
class BookmarkIntegrationTest extends TestCase
{
/** @var CI_Controller */
protected $CI;
public function setUp()
{
// Grab the global CI instance
$this->CI =& get_instance();
// Make sure we're in a clean state:
// - no lingering URI segments
// - fresh router rules
$this->CI->uri->uri_string = '';
$this->CI->router->_set_routing([]);
}
/**
* Simulate a GET to /api/users and assert JSON list
*/
public function testGetUsersReturnsJsonArray()
{
// 1) Fake server variables
$_SERVER['REQUEST_METHOD'] = 'GET';
// you can use PATH_INFO or REQUEST_URI depending on your config
$_SERVER['PATH_INFO'] = '/api/users';
$_GET = []; $_POST = [];
// 2) Re-run routing so CI picks up our fake path
$this->CI->router->_set_routing();
$this->CI->uri->_set_uri_string('/api/users');
$this->CI->router->set_class('api');
$this->CI->router->set_method('users');
// 3) Capture the output
ob_start();
// This is the entry point:
// core/CodeIgniter.php will look at $RTR->class/method
// instantiate your Api controller, call ->users()
require BASEPATH . 'core/CodeIgniter.php';
$output = ob_get_clean();
// 4) Assert JSON shape, status header, etc.
$this->assertNotEmpty($output, 'No output at all');
$decoded = json_decode($output, true);
$this->assertIsArray($decoded, 'Expected JSON array');
// more finegrained assertions…
}
/**
* Simulate a POST to /api/users
*/
public function testCreateUser()
{
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['PATH_INFO'] = '/api/users';
$_POST = [
'name' => 'Alice',
'email' => 'alice@example.com',
];
$_GET = [];
$this->CI->router->_set_routing();
$this->CI->uri->_set_uri_string('/api/users');
$this->CI->router->set_class('api');
$this->CI->router->set_method('users');
ob_start();
require BASEPATH . 'core/CodeIgniter.php';
$out = ob_get_clean();
$this->assertStringContainsString('"id":', $out);
$this->assertMatchesRegularExpression('/201 Created/', xdebug_get_headers());
}
}
@@ -1,48 +1,28 @@
<?php
namespace BookmarkTest;
echo "BookmarkTest loaded\n";
echo "Resolved path: " . APPPATH.'controllers/api/frontend/v1/Bookmark.php' . "\n";
//require_once(APPPATH.'controllers/api/frontend/v1/Bookmark.php');
//require_once(APPPATH.'libraries/AuthLib.php');
use Bookmark;
use PHPUnit\Framework\TestCase;
class BookmarkTest extends TestCase
{
private $_ci;
// public function setUp(): void
//
//echo "BookmarkTest loaded\n";
//require_once __DIR__ . '/../../CI_TestCase.php';
//
//class BookmarkTest extends CI_TestCase
//{
//
// public function setUp()
// {
// error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED);
// parent::setUp();
//
// echo 'in the setUp';
// // Load CodeIgniter instance
// $this->_ci = &get_instance();
//
// if (!is_object($this->_ci)) {
// throw new \Exception('CI instance is not available');
// }
//
// $this->_ci->load->library('AuthLib', null, 'AuthLib');
//
// $this->_ci->AuthLib->loginLDAP('horauer', 'FHCompleteDemo42!');
// $this->_ci->load->controller('api/v1/Bookmark');
//
// $this->obj = new Bookmark();
//// require_once APPPATH . 'core/FHC_Controller.php';
//// require_once APPPATH . 'core/Auth_Controller.php';
//// require_once APPPATH . 'core/FHCAPI_Controller.php';
// require_once APPPATH . 'controllers/api/frontend/v1/Bookmark.php';
// $this->bookmark = new Bookmark(); // or $this->CI->bookmark
// }
/** @test */
public function test_true()
{
echo 'in the test_true case';
$this->assertTrue(true);
}
}
//
// /** @test */
// public function test_true()
// {
// echo "expected response";
//
// }
//
//
//}