Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

---
Expand Down
10 changes: 9 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<phpunit bootstrap="src/OpenCATS/Tests/bootstrap.php">
<testsuites>
<testsuite name="UnitTests">
<directory>src/OpenCATS/Tests/UnitTests</directory>
</testsuite>
<testsuite name="IntegrationTests">
<directory>src/OpenCATS/Tests/IntegrationTests</directory>
</testsuite>
</testsuites>
<php>
<!-- Define HTML_ENCODING for PHPUnit because config.php is not loaded in unit tests. -->
<const name="HTML_ENCODING" value="UTF-8"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 0 additions & 5 deletions src/OpenCATS/Tests/IntegrationTests/DatabaseSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions src/OpenCATS/Tests/IntegrationTests/DatabaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 0 additions & 5 deletions src/OpenCATS/Tests/UnitTests/AJAXInterfaceTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<?php
use PHPUnit\Framework\TestCase;

if( !defined('LEGACY_ROOT') )
{
define('LEGACY_ROOT', '.');
}

include_once(LEGACY_ROOT . '/lib/AJAXInterface.php');

class AJAXInterfaceTest extends TestCase
Expand Down
5 changes: 0 additions & 5 deletions src/OpenCATS/Tests/UnitTests/AddressParserTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<?php
use PHPUnit\Framework\TestCase;

if( !defined('LEGACY_ROOT') )
{
define('LEGACY_ROOT', '.');
}

include_once(LEGACY_ROOT . '/lib/StringUtility.php');
include_once(LEGACY_ROOT . '/lib/AddressParser.php'); /* Depends on StringUtility. */

Expand Down
5 changes: 0 additions & 5 deletions src/OpenCATS/Tests/UnitTests/ArrayUtilityTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<?php
use PHPUnit\Framework\TestCase;

if( !defined('LEGACY_ROOT') )
{
define('LEGACY_ROOT', '.');
}

include_once(LEGACY_ROOT . '/lib/ArrayUtility.php');

class ArrayUtilityTest extends TestCase
Expand Down
5 changes: 0 additions & 5 deletions src/OpenCATS/Tests/UnitTests/BrowserDetectionTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<?php
use PHPUnit\Framework\TestCase;

if( !defined('LEGACY_ROOT') )
{
define('LEGACY_ROOT', '.');
}

include_once(LEGACY_ROOT . '/lib/BrowserDetection.php');

class BrowserDetectionTest extends TestCase
Expand Down
64 changes: 35 additions & 29 deletions src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
use OpenCATS\Entity\CompanyRepository;
use OpenCATS\Entity\Company;

if( !defined('LEGACY_ROOT') )
{
define('LEGACY_ROOT', '.');
}

include_once(LEGACY_ROOT . '/lib/History.php');
include_once(LEGACY_ROOT . '/lib/DatabaseConnection.php');

class CompanyRepositoryTests extends TestCase
{
Expand All @@ -34,28 +30,38 @@
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 = [

Check notice on line 54 in src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/OpenCATS/Tests/UnitTests/CompanyRepositoryTest.php#L54

Avoid excessively long variable names like $expectedIntegerValues. Keep variable name length under 20.
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')
Expand Down Expand Up @@ -86,23 +92,22 @@
$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();
$databaseConnectionMock->method('query')
->willReturn(false);
$historyMock = $this->getHistoryMock();
$CompanyRepository = new CompanyRepository($databaseConnectionMock);
$this->expectException(\OpenCATS\Entity\CompanyRepositoryException::class);
$CompanyRepository->persist($this->createCompany(), $historyMock);
}

Expand All @@ -114,7 +119,8 @@
private function getDatabaseConnectionMock()
{
return $this->getMockBuilder('\DatabaseConnection')
->setMethods(['makeQueryString', 'makeQueryInteger', 'query', 'getLastInsertID'])
->disableOriginalConstructor()
->onlyMethods(['makeQueryString', 'makeQueryInteger', 'query', 'getLastInsertID'])
->getMock();
}

Expand Down
5 changes: 0 additions & 5 deletions src/OpenCATS/Tests/UnitTests/DateUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
27 changes: 11 additions & 16 deletions src/OpenCATS/Tests/UnitTests/FileUtilityTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<?php
use PHPUnit\Framework\TestCase;

if( !defined('LEGACY_ROOT') )
{
define('LEGACY_ROOT', '.');
}

include_once(LEGACY_ROOT . '/lib/FileUtility.php');

class FileUtilityTest extends TestCase
Expand Down Expand Up @@ -57,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)
Expand All @@ -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)
Expand All @@ -131,4 +126,4 @@ function testGetUniqueDirectory()
);
}
}
?>
?>
5 changes: 0 additions & 5 deletions src/OpenCATS/Tests/UnitTests/ResultSetUtilityTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<?php
use PHPUnit\Framework\TestCase;

if( !defined('LEGACY_ROOT') )
{
define('LEGACY_ROOT', '.');
}

include_once(LEGACY_ROOT . '/lib/ResultSetUtility.php');

class ResultSetUtilityTest extends TestCase
Expand Down
11 changes: 3 additions & 8 deletions src/OpenCATS/Tests/UnitTests/SessionCSRFTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<?php
use PHPUnit\Framework\TestCase;

if( !defined('LEGACY_ROOT') )
{
define('LEGACY_ROOT', '.');
}

include_once(LEGACY_ROOT . '/lib/Session.php');

class SessionCSRFTest extends TestCase
Expand All @@ -31,16 +26,16 @@ function testGetCSRFTokenStableWithoutRotation()
$t1 = $this->_session->getCSRFToken();
$t2 = $this->_session->getCSRFToken();

$this->assertEquals($t1, $t2);
$this->assertSame($t1, $t2);
}

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));
}
Expand Down
5 changes: 0 additions & 5 deletions src/OpenCATS/Tests/UnitTests/StringUtilityTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<?php
use PHPUnit\Framework\TestCase;

if( !defined('LEGACY_ROOT') )
{
define('LEGACY_ROOT', '.');
}

include_once(LEGACY_ROOT . '/lib/StringUtility.php');

class StringUtilityTest extends TestCase
Expand Down
5 changes: 0 additions & 5 deletions src/OpenCATS/Tests/UnitTests/TemplateEscapeTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
<?php
use PHPUnit\Framework\TestCase;

if( !defined('LEGACY_ROOT') )
{
define('LEGACY_ROOT', '.');
}

include_once(LEGACY_ROOT . '/lib/Template.php');

class TemplateEscapeStringableFixture
Expand Down
Loading
Loading