From 43d8b456a1b6ce1582e5572bc438f39821dcd1d9 Mon Sep 17 00:00:00 2001 From: anonymoususer72041 <247563575+anonymoususer72041@users.noreply.github.com> Date: Wed, 22 Apr 2026 19:22:28 +0200 Subject: [PATCH 1/7] Replace deprecated regex assertions in tests --- src/OpenCATS/Tests/UnitTests/VCardTest.php | 24 ++++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/OpenCATS/Tests/UnitTests/VCardTest.php b/src/OpenCATS/Tests/UnitTests/VCardTest.php index f36f64ad4..59734d992 100644 --- a/src/OpenCATS/Tests/UnitTests/VCardTest.php +++ b/src/OpenCATS/Tests/UnitTests/VCardTest.php @@ -10,6 +10,18 @@ class VCardTest extends TestCase { + private function assertRegexCompat($pattern, $value) + { + if( method_exists($this, 'assertMatchesRegularExpression') ) + { + $this->assertMatchesRegularExpression($pattern, $value); + } + else + { + $this->assertSame(1, preg_match($pattern, $value)); + } + } + function testVersion() { $this->assertSame(VCard::VCARD_VERSION, '2.1'); @@ -31,10 +43,7 @@ function testVCard1() $this->assertSame($outputLines[3], 'FN:John Smith'); /* Test revision timestamp. */ - $this->assertRegExp( - '/^REV:\d{8}T\d{6}$/', - $outputLines[4] - ); + $this->assertRegexCompat('/^REV:\d{8}T\d{6}$/', $outputLines[4]); $currentREVNumeric = date('YmdHis'); $vCardREVNumeric = preg_replace('/REV:|T/', '', $outputLines[4]); @@ -91,10 +100,7 @@ function testVCard2() $this->assertSame($outputLines[10], 'URL:http://www.slashdot.org'); /* Test revision timestamp. */ - $this->assertRegExp( - '/^REV:\d{8}T\d{6}$/', - $outputLines[11] - ); + $this->assertRegexCompat('/^REV:\d{8}T\d{6}$/', $outputLines[11]); $currentREVNumeric = date('YmdHis'); $vCardREVNumeric = preg_replace('/REV:|T/', '', $outputLines[11]); @@ -111,4 +117,4 @@ function testVCard2() $this->assertSame($vCard->getFilename(), 'John Smith.vcf'); } } -?> \ No newline at end of file +?> From 028aa4a4c925edcaf3fc44fc32fbaf66de1bb6a4 Mon Sep 17 00:00:00 2001 From: anonymoususer72041 <247563575+anonymoususer72041@users.noreply.github.com> Date: Wed, 22 Apr 2026 19:27:17 +0200 Subject: [PATCH 2/7] Use expectException in CompanyRepositoryTest --- src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php b/src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php index c7a74fe04..f407b2528 100644 --- a/src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php +++ b/src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php @@ -93,9 +93,6 @@ function test_persist_CreateNewCompany_StoresHistoryWithCompanyId() $CompanyRepository->persist($this->createCompany(), $historyMock); } - /** - * @expectedException OpenCATS\Entity\CompanyRepositoryException - */ function test_persist_FailToCreateNewCompany_ThrowsException() { $databaseConnectionMock = $this->getDatabaseConnectionMock(); @@ -103,6 +100,7 @@ function test_persist_FailToCreateNewCompany_ThrowsException() ->willReturn(false); $historyMock = $this->getHistoryMock(); $CompanyRepository = new CompanyRepository($databaseConnectionMock); + $this->expectException(\OpenCATS\Entity\CompanyRepositoryException::class); $CompanyRepository->persist($this->createCompany(), $historyMock); } From 392f8633e8479b72e8e81d72e1fba0d4f91fff8d Mon Sep 17 00:00:00 2001 From: anonymoususer72041 <247563575+anonymoususer72041@users.noreply.github.com> Date: Wed, 22 Apr 2026 19:30:42 +0200 Subject: [PATCH 3/7] Replace deprecated mock builder methods --- src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php b/src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php index f407b2528..ba212a299 100644 --- a/src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php +++ b/src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php @@ -112,7 +112,7 @@ private function getHistoryMock() private function getDatabaseConnectionMock() { return $this->getMockBuilder('\DatabaseConnection') - ->setMethods(['makeQueryString', 'makeQueryInteger', 'query', 'getLastInsertID']) + ->onlyMethods(['makeQueryString', 'makeQueryInteger', 'query', 'getLastInsertID']) ->getMock(); } From f128958c0eebab02fead821c92b69437bdc66b66 Mon Sep 17 00:00:00 2001 From: anonymoususer72041 <247563575+anonymoususer72041@users.noreply.github.com> Date: Wed, 22 Apr 2026 19:36:22 +0200 Subject: [PATCH 4/7] Refactor consecutive mock expectations --- .../Tests/UnitTests/CompanyRepositoryTest.php | 51 +++++++++++-------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php b/src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php index ba212a299..313b440ea 100644 --- a/src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php +++ b/src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php @@ -34,28 +34,38 @@ class CompanyRepositoryTests extends TestCase function test_persist_CreatesNewCompany_InputValuesAreEscaped() { $databaseConnectionMock = $this->getDatabaseConnectionMock(); + $expectedStringValues = [ + self::COMPANY_NAME, + self::ADDRESS, + self::ADDRESS2, + self::CITY, + self::STATE, + self::ZIP_CODE, + self::PHONE_NUMBER_ONE, + self::PHONE_NUMBER_TWO, + self::FAX_NUMBER, + self::URL, + self::KEY_TECHNOLOGIES, + self::NOTES + ]; + $stringCallIndex = 0; $databaseConnectionMock->expects($this->exactly(12)) ->method('makeQueryString') - ->withConsecutive( - [$this->equalTo(self::COMPANY_NAME)], - [$this->equalTo(self::ADDRESS)], - [$this->equalTo(self::ADDRESS2)], - [$this->equalTo(self::CITY)], - [$this->equalTo(self::STATE)], - [$this->equalTo(self::ZIP_CODE)], - [$this->equalTo(self::PHONE_NUMBER_ONE)], - [$this->equalTo(self::PHONE_NUMBER_TWO)], - [$this->equalTo(self::FAX_NUMBER)], - [$this->equalTo(self::URL)], - [$this->equalTo(self::KEY_TECHNOLOGIES)], - [$this->equalTo(self::NOTES)] - ); + ->willReturnCallback(function($value) use ($expectedStringValues, &$stringCallIndex) { + $this->assertSame($expectedStringValues[$stringCallIndex], $value); + $stringCallIndex++; + }); + $expectedIntegerValues = [ + self::ENTERED_BY, + self::OWNER + ]; + $integerCallIndex = 0; $databaseConnectionMock->expects($this->exactly(2)) ->method('makeQueryInteger') - ->withConsecutive( - [$this->equalTo(self::ENTERED_BY)], - [$this->equalTo(self::OWNER)] - ); + ->willReturnCallback(function($value) use ($expectedIntegerValues, &$integerCallIndex) { + $this->assertSame($expectedIntegerValues[$integerCallIndex], $value); + $integerCallIndex++; + }); $databaseConnectionMock->method('query') ->willReturn(true); $databaseConnectionMock->method('getLastInsertID') @@ -86,8 +96,9 @@ function test_persist_CreateNewCompany_StoresHistoryWithCompanyId() $historyMock = $this->getHistoryMock(); $historyMock->expects($this->exactly(1)) ->method('storeHistoryNew') - ->withConsecutive( - [DATA_ITEM_COMPANY, self::COMPANY_ID] + ->with( + $this->equalTo(DATA_ITEM_COMPANY), + $this->equalTo(self::COMPANY_ID) ); $CompanyRepository = new CompanyRepository($databaseConnectionMock); $CompanyRepository->persist($this->createCompany(), $historyMock); From 2a1ef52c7e72f682fcbf644918ed72300b9eda41 Mon Sep 17 00:00:00 2001 From: anonymoususer72041 <247563575+anonymoususer72041@users.noreply.github.com> Date: Wed, 22 Apr 2026 19:44:48 +0200 Subject: [PATCH 5/7] Clean up PHPUnit configuration --- .github/workflows/ci.yml | 4 ++-- README-testing.md | 4 ++-- phpunit.xml.dist | 10 +++++++++- test/runAllTests.sh | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 069145dbb..e2ffcefa3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,7 +51,7 @@ jobs: run: mkdir -p reports/behat-default reports/behat-security test/screenshots - name: Run PHPUnit (Unit Tests) - run: ./vendor/bin/phpunit --log-junit reports/unit-report.xml src/OpenCATS/Tests/UnitTests + run: ./vendor/bin/phpunit --log-junit reports/unit-report.xml --testsuite UnitTests - name: Run Integration Tests (Docker) run: | @@ -78,7 +78,7 @@ jobs: docker compose -f docker-compose-test.yml exec -T php php -r "echo 'IP for integrationtestdb: ' . gethostbyname('integrationtestdb') . PHP_EOL;" echo "Running PHPUnit Integration Tests..." - docker compose -f docker-compose-test.yml exec -T --workdir /var/www/public php ./vendor/bin/phpunit --log-junit /var/www/public/reports/integration-report.xml src/OpenCATS/Tests/IntegrationTests + docker compose -f docker-compose-test.yml exec -T --workdir /var/www/public php ./vendor/bin/phpunit --log-junit /var/www/public/reports/integration-report.xml --testsuite IntegrationTests echo "Running Behat Suites..." docker compose -f docker-compose-test.yml exec -T --workdir /var/www/public php ./vendor/bin/behat -v -c ./test/behat.yml --suite="default" --format=progress diff --git a/README-testing.md b/README-testing.md index 5150f7b45..334d41c6c 100644 --- a/README-testing.md +++ b/README-testing.md @@ -35,8 +35,8 @@ To mirror the CI environment on your machine: ``` 4. **Run the suites:** - * **PHPUnit Unit Tests:** `docker exec -it opencats_test_php ./vendor/bin/phpunit src/OpenCATS/Tests/UnitTests` - * **PHPUnit Integration Tests:** `docker exec -it opencats_test_php ./vendor/bin/phpunit src/OpenCATS/Tests/IntegrationTests` + * **PHPUnit Unit Tests:** `docker exec -it opencats_test_php ./vendor/bin/phpunit --testsuite UnitTests` + * **PHPUnit Integration Tests:** `docker exec -it opencats_test_php ./vendor/bin/phpunit --testsuite IntegrationTests` * **Behat:** `docker exec -it opencats_test_php ./vendor/bin/behat -c ./test/behat.yml` --- diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 24a485c4a..cc2695cef 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,13 @@ - + + + + src/OpenCATS/Tests/UnitTests + + + src/OpenCATS/Tests/IntegrationTests + + diff --git a/test/runAllTests.sh b/test/runAllTests.sh index 99736135a..81660524c 100755 --- a/test/runAllTests.sh +++ b/test/runAllTests.sh @@ -3,6 +3,6 @@ cd /var/www/public/ dockerize -wait tcp://opencats_test_mariadb:3306 -wait http://opencats_test_web:80 -timeout 30s php test/scripts/waitForDb.php cat config.php -./vendor/bin/phpunit src/OpenCATS/Tests/IntegrationTests +./vendor/bin/phpunit --testsuite IntegrationTests ./vendor/bin/behat -v -c ./test/behat.yml --suite="default" ./vendor/bin/behat -v -c ./test/behat.yml --suite="security" From f9c07a8eb09d39dcacbf3c07a6abb2079cc1cf65 Mon Sep 17 00:00:00 2001 From: anonymoususer72041 <247563575+anonymoususer72041@users.noreply.github.com> Date: Wed, 22 Apr 2026 19:50:31 +0200 Subject: [PATCH 6/7] Centralize PHPUnit test bootstrap --- phpunit.xml.dist | 2 +- src/OpenCATS/Tests/IntegrationTests/DatabaseSearchTest.php | 5 ----- src/OpenCATS/Tests/IntegrationTests/DatabaseTestCase.php | 6 ------ src/OpenCATS/Tests/UnitTests/AJAXInterfaceTest.php | 5 ----- src/OpenCATS/Tests/UnitTests/AddressParserTest.php | 5 ----- src/OpenCATS/Tests/UnitTests/ArrayUtilityTest.php | 5 ----- src/OpenCATS/Tests/UnitTests/BrowserDetectionTest.php | 5 ----- src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php | 7 ++----- src/OpenCATS/Tests/UnitTests/DateUtilityTest.php | 5 ----- src/OpenCATS/Tests/UnitTests/FileUtilityTest.php | 5 ----- src/OpenCATS/Tests/UnitTests/ResultSetUtilityTest.php | 5 ----- src/OpenCATS/Tests/UnitTests/SessionCSRFTest.php | 5 ----- src/OpenCATS/Tests/UnitTests/StringUtilityTest.php | 5 ----- src/OpenCATS/Tests/UnitTests/TemplateEscapeTest.php | 5 ----- src/OpenCATS/Tests/UnitTests/VCardTest.php | 5 ----- src/OpenCATS/Tests/bootstrap.php | 7 +++++++ 16 files changed, 10 insertions(+), 72 deletions(-) create mode 100644 src/OpenCATS/Tests/bootstrap.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cc2695cef..efc81f7fa 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ - + src/OpenCATS/Tests/UnitTests diff --git a/src/OpenCATS/Tests/IntegrationTests/DatabaseSearchTest.php b/src/OpenCATS/Tests/IntegrationTests/DatabaseSearchTest.php index 8bc7da75d..51adb12c3 100644 --- a/src/OpenCATS/Tests/IntegrationTests/DatabaseSearchTest.php +++ b/src/OpenCATS/Tests/IntegrationTests/DatabaseSearchTest.php @@ -5,11 +5,6 @@ use DatabaseConnection; use DatabaseSearch; -if( !defined('LEGACY_ROOT') ) -{ - define('LEGACY_ROOT', '.'); -} - include_once(LEGACY_ROOT . '/lib/DatabaseSearch.php'); class DatabaseSearchTest extends DatabaseTestCase diff --git a/src/OpenCATS/Tests/IntegrationTests/DatabaseTestCase.php b/src/OpenCATS/Tests/IntegrationTests/DatabaseTestCase.php index 2d92352c9..a51400320 100644 --- a/src/OpenCATS/Tests/IntegrationTests/DatabaseTestCase.php +++ b/src/OpenCATS/Tests/IntegrationTests/DatabaseTestCase.php @@ -11,12 +11,6 @@ protected function setUp(): void { global $mySQLConnection; parent::setUp(); - - // Ensure roots are defined for legacy includes - if (!defined('LEGACY_ROOT')) { - define('LEGACY_ROOT', '.'); - } - include_once('./constants.php'); // We define these for the rest of the app logic, diff --git a/src/OpenCATS/Tests/UnitTests/AJAXInterfaceTest.php b/src/OpenCATS/Tests/UnitTests/AJAXInterfaceTest.php index 2aec763ad..d3325c0fc 100644 --- a/src/OpenCATS/Tests/UnitTests/AJAXInterfaceTest.php +++ b/src/OpenCATS/Tests/UnitTests/AJAXInterfaceTest.php @@ -1,11 +1,6 @@ getMockBuilder('\DatabaseConnection') + ->disableOriginalConstructor() ->onlyMethods(['makeQueryString', 'makeQueryInteger', 'query', 'getLastInsertID']) ->getMock(); } diff --git a/src/OpenCATS/Tests/UnitTests/DateUtilityTest.php b/src/OpenCATS/Tests/UnitTests/DateUtilityTest.php index 8ce5cb86e..f71a8c7dc 100644 --- a/src/OpenCATS/Tests/UnitTests/DateUtilityTest.php +++ b/src/OpenCATS/Tests/UnitTests/DateUtilityTest.php @@ -38,11 +38,6 @@ use PHPUnit\Framework\TestCase; -if( !defined('LEGACY_ROOT') ) -{ - define('LEGACY_ROOT', '.'); -} - include_once(LEGACY_ROOT . '/constants.php'); include_once(LEGACY_ROOT . '/lib/StringUtility.php'); include_once(LEGACY_ROOT . '/lib/DateUtility.php'); /* Depends on StringUtility. */ diff --git a/src/OpenCATS/Tests/UnitTests/FileUtilityTest.php b/src/OpenCATS/Tests/UnitTests/FileUtilityTest.php index 174d59a8f..2db46ca39 100644 --- a/src/OpenCATS/Tests/UnitTests/FileUtilityTest.php +++ b/src/OpenCATS/Tests/UnitTests/FileUtilityTest.php @@ -1,11 +1,6 @@ Date: Wed, 22 Apr 2026 19:59:58 +0200 Subject: [PATCH 7/7] Use stricter assertions in tests --- .../DatabaseConnectionTest.php | 2 +- .../Tests/UnitTests/FileUtilityTest.php | 22 +++++++++---------- .../Tests/UnitTests/SessionCSRFTest.php | 6 ++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/OpenCATS/Tests/IntegrationTests/DatabaseConnectionTest.php b/src/OpenCATS/Tests/IntegrationTests/DatabaseConnectionTest.php index 7227efe9d..5a81ef2ee 100644 --- a/src/OpenCATS/Tests/IntegrationTests/DatabaseConnectionTest.php +++ b/src/OpenCATS/Tests/IntegrationTests/DatabaseConnectionTest.php @@ -172,7 +172,7 @@ function testQuery() false, 'SELECT query should succeed' ); - $this->assertEquals( + $this->assertSame( mysqli_num_rows($queryResult), 1, '1 row should be returned' diff --git a/src/OpenCATS/Tests/UnitTests/FileUtilityTest.php b/src/OpenCATS/Tests/UnitTests/FileUtilityTest.php index 2db46ca39..ba2787f57 100644 --- a/src/OpenCATS/Tests/UnitTests/FileUtilityTest.php +++ b/src/OpenCATS/Tests/UnitTests/FileUtilityTest.php @@ -52,22 +52,22 @@ function testGetUniqueDirectory() $directoryD = FileUtility::getUniqueDirectory('attachments'); /* Make sure all directory names look like md5 strings. */ - $this->assertEquals( + $this->assertSame( strlen($directoryA), 32, sprintf("'%s' should be 32 characters long", $directoryA) ); - $this->assertEquals( + $this->assertSame( strlen($directoryB), 32, sprintf("'%s' should be 32 characters long", $directoryB) ); - $this->assertEquals( + $this->assertSame( strlen($directoryC), 32, sprintf("'%s' should be 32 characters long", $directoryB) ); - $this->assertEquals( + $this->assertSame( strlen($directoryD), 32, sprintf("'%s' should be 32 characters long", $directoryB) @@ -76,32 +76,32 @@ function testGetUniqueDirectory() /* Make sure extra data is actually being added (directory names * should not be identical). */ - $this->assertNotEquals( + $this->assertNotSame( $directoryA, $directoryB, sprintf("'%s' should not equal '%s'", $directoryA, $directoryB) ); - $this->assertNotEquals( + $this->assertNotSame( $directoryA, $directoryC, sprintf("'%s' should not equal '%s'", $directoryA, $directoryC) ); - $this->assertNotEquals( + $this->assertNotSame( $directoryA, $directoryD, sprintf("'%s' should not equal '%s'", $directoryA, $directoryD) ); - $this->assertNotEquals( + $this->assertNotSame( $directoryB, $directoryC, sprintf("'%s' should not equal '%s'", $directoryB, $directoryC) ); - $this->assertNotEquals( + $this->assertNotSame( $directoryB, $directoryD, sprintf("'%s' should not equal '%s'", $directoryB, $directoryD) ); - $this->assertNotEquals( + $this->assertNotSame( $directoryC, $directoryD, sprintf("'%s' should not equal '%s'", $directoryC, $directoryD) @@ -126,4 +126,4 @@ function testGetUniqueDirectory() ); } } -?> \ No newline at end of file +?> diff --git a/src/OpenCATS/Tests/UnitTests/SessionCSRFTest.php b/src/OpenCATS/Tests/UnitTests/SessionCSRFTest.php index 22b9469b3..24eebcda0 100644 --- a/src/OpenCATS/Tests/UnitTests/SessionCSRFTest.php +++ b/src/OpenCATS/Tests/UnitTests/SessionCSRFTest.php @@ -26,7 +26,7 @@ function testGetCSRFTokenStableWithoutRotation() $t1 = $this->_session->getCSRFToken(); $t2 = $this->_session->getCSRFToken(); - $this->assertEquals($t1, $t2); + $this->assertSame($t1, $t2); } function testRotateCSRFTokenChangesToken() @@ -34,8 +34,8 @@ function testRotateCSRFTokenChangesToken() $old = $this->_session->getCSRFToken(); $new = $this->_session->rotateCSRFToken(); - $this->assertNotEquals($old, $new); - $this->assertEquals($new, $this->_session->getCSRFToken()); + $this->assertNotSame($old, $new); + $this->assertSame($new, $this->_session->getCSRFToken()); $this->assertSame(64, strlen($new)); $this->assertTrue(ctype_xdigit($new)); }