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..efc81f7fa 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,13 @@ - + + + + src/OpenCATS/Tests/UnitTests + + + src/OpenCATS/Tests/IntegrationTests + + 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/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 @@ 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,16 +92,14 @@ 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); } - /** - * @expectedException OpenCATS\Entity\CompanyRepositoryException - */ function test_persist_FailToCreateNewCompany_ThrowsException() { $databaseConnectionMock = $this->getDatabaseConnectionMock(); @@ -103,6 +107,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); } @@ -114,7 +119,8 @@ private function getHistoryMock() private function getDatabaseConnectionMock() { return $this->getMockBuilder('\DatabaseConnection') - ->setMethods(['makeQueryString', 'makeQueryInteger', 'query', 'getLastInsertID']) + ->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..ba2787f57 100644 --- a/src/OpenCATS/Tests/UnitTests/FileUtilityTest.php +++ b/src/OpenCATS/Tests/UnitTests/FileUtilityTest.php @@ -1,11 +1,6 @@ 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) @@ -81,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) @@ -131,4 +126,4 @@ function testGetUniqueDirectory() ); } } -?> \ No newline at end of file +?> diff --git a/src/OpenCATS/Tests/UnitTests/ResultSetUtilityTest.php b/src/OpenCATS/Tests/UnitTests/ResultSetUtilityTest.php index 8477b284a..f661c540d 100644 --- a/src/OpenCATS/Tests/UnitTests/ResultSetUtilityTest.php +++ b/src/OpenCATS/Tests/UnitTests/ResultSetUtilityTest.php @@ -1,11 +1,6 @@ _session->getCSRFToken(); $t2 = $this->_session->getCSRFToken(); - $this->assertEquals($t1, $t2); + $this->assertSame($t1, $t2); } function testRotateCSRFTokenChangesToken() @@ -39,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)); } diff --git a/src/OpenCATS/Tests/UnitTests/StringUtilityTest.php b/src/OpenCATS/Tests/UnitTests/StringUtilityTest.php index 392a722d8..f511924bf 100644 --- a/src/OpenCATS/Tests/UnitTests/StringUtilityTest.php +++ b/src/OpenCATS/Tests/UnitTests/StringUtilityTest.php @@ -1,11 +1,6 @@ assertMatchesRegularExpression($pattern, $value); + } + else + { + $this->assertSame(1, preg_match($pattern, $value)); + } + } + function testVersion() { $this->assertSame(VCard::VCARD_VERSION, '2.1'); @@ -31,10 +38,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 +95,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 +112,4 @@ function testVCard2() $this->assertSame($vCard->getFilename(), 'John Smith.vcf'); } } -?> \ No newline at end of file +?> diff --git a/src/OpenCATS/Tests/bootstrap.php b/src/OpenCATS/Tests/bootstrap.php new file mode 100644 index 000000000..5daac1f15 --- /dev/null +++ b/src/OpenCATS/Tests/bootstrap.php @@ -0,0 +1,7 @@ +