Skip to content

15. Application Exercise – Version 7

Image

15.1. Implementation

Here, we will revisit version 6 by moving the constants used in the main scripts into a configuration file. The configuration file will be a JSON file with the following content:


{
    "rootDirectory": "C:/Data/st-2019/dev/php7/poly/scripts-console/impots/version-07",
    "databaseFilename": "Data/database.json",
    "taxAdminDataFileName": "Data/taxadmindata.json",
    "taxPayersDataFileName": "Data/taxpayersdata.json",
    "resultsFileName": "Data/results.json",
    "errorsFileName": "Data/errors.json",
    "dependencies": {
        "BaseEntity": "/../version-05/Entities/BaseEntity.php",
        "TaxAdminData": "/../version-05/Entities/TaxAdminData.php",
        "TaxPayerData": "/../version-05/Entities/TaxPayerData.php",
        "Database": "/../version-05/Entities/Database.php",
        "TaxExceptions": "/../version-05/Entities/TaxExceptions.php",
        "Utilities": "/../version-05/Utilities/Utilities.php",
        "DaoInterface": "/../version-05/Dao/DaoInterface.php",
        "DaoProcess": "/../version-05/Dao/DaoProcess.php",
        "DaoInterface4TransferAdminDataToDatabase": "/../version-05/Dao/DaoInterface4TransferAdminDataToDatabase.php",
        "DaoTransferAdminDataFromJsonFile2Database": "/../version-05/Dao/DaoTransferAdminDataFromJsonFile2Database.php",        
        "DaoImpotsWithTaxAdminDataInDatabase": "/../version-05/Dao/DaoImpotsWithTaxAdminDataInDatabase.php",
        "BusinessLogicInterface": "/../version-05/BusinessLogic/BusinessLogicInterface.php",
        "BusinessLogic": "/../version-05/BusinessLogic/BusinessLogic.php"
    },
    "dependencies4calculate": [
        "BaseEntity",
        "TaxAdminData",
        "TaxPayerData",
        "Database",
        "TaxExceptions",
        "Utilities",
        "InterfaceDao",
        "TraitDao",
        "DaoTaxesWithTaxAdminDataInDatabase",
        "BusinessInterface",
        "Metier"],
    "dependencies4transfer": [
        "BaseEntity",
        "TaxAdminData",
        "Database",
        "TaxExceptions",
        "Utilities",
        "DaoInterface",
        "DaoProcessing",  
        "DaoInterface4TransferAdminData2Database",
        "DaoTransferAdminDataFromJsonFileToDatabase"]
}

In this configuration:

  • line 2: the directory from which all paths in this configuration file are measured;
  • lines 3–7: the paths to all JSON files in the application;
  • lines 8–22: the paths to all application files, in the form [key=>path];
  • lines 23–34: the dependencies for tax calculation in the form of a list of keys from the [dependencies] dictionary (lines 8–22);
  • lines 35–44: dependencies for transferring the JSON file [taxadmindata.json] to the database in the form of a list of keys from the [dependencies] dictionary (lines 8–22);

The script for transferring tax administration data to the database [MainTransferAdminDataFromFile2PostgresSQLDatabase.php] becomes the following:


<?php

// Strict adherence to the declared types of function parameters
declare (strict_types=1);

// namespace
namespace Application;

// PHP error handling
// ini_set("display_errors", "0");
//
// path to the configuration file
define("CONFIG_FILENAME", "../Data/config.json");

// retrieve the configuration
$config = \json_decode(\file_get_contents(CONFIG_FILENAME), true);

// include dependencies required by the script
$rootDirectory = $config["rootDirectory"];
foreach ($config["dependencies4transfer"] as $dependency) {
  require $rootDirectory . $config["dependencies"][$dependency];
}

// Define constants
define("DATABASE_CONFIG_FILENAME", "$rootDirectory/{$config["databaseFilename"]}");
define("TAXADMINDATA_FILENAME", "$rootDirectory/{$config["taxAdminDataFileName"]}");

//
try {
  // Create the [dao] layer
  $dao = new DaoTransferAdminDataFromJsonFile2Database(DATABASE_CONFIG_FILENAME, TAXADMINDATA_FILENAME);
  // transfer data to the database
  $dao->transferAdminData2Database();
} catch (TaxException $ex) {
  // display the error
  print $ex->getMessage() . "\n";
}
// end
print "Done\n";
exit;

Comments

The code remains the same as in the linked section. The only difference is the use of the [config.json] file instead of constants on lines 18–26;

  • line 16: the [file_get_contents] function transfers the [config.json] file into a string. The [json_decode] function then uses this string to construct the [$config] dictionary. The second parameter [true] of the [json_decode] function indicates that we want to construct a dictionary;
  • lines 19–22: we include the dependencies required for the script to transfer data from the [taxadmindata.json] file to the database;
    • [$config["dependencies4transfer"]] is the array of dependencies required by the transfer script. It is a list of keys. The paths of the files to be included in the project are found in the [$config["dependencies"]] dictionary;
    • $config["rootDirectory"] represents the path with which the files to be included must be prefixed;

Similarly, the tax calculation script becomes the following [MainCalculateImpotsWithTaxAdminDataInPostgresDatabase]:


<?php

// Strict adherence to the declared types of function parameters
declare (strict_types=1);

// namespace
namespace Application;

// PHP error handling
// ini_set("display_errors", "0");
//
// path to the configuration file
define("CONFIG_FILENAME", "../Data/config.json");

// retrieve the configuration
$config = \json_decode(\file_get_contents(CONFIG_FILENAME), true);

// include dependencies required by the script
$rootDirectory = $config["rootDirectory"];
foreach ($config["dependencies4calculate"] as $dependency) {
  require $rootDirectory . $config["dependencies"][$dependency];
}

// Define constants
define("DATABASE_CONFIG_FILENAME", "$rootDirectory/{$config["databaseFilename"]}");
define("TAXPAYERSDATA_FILENAME", "$rootDirectory/{$config["taxPayersDataFileName"]}");
define("RESULTS_FILENAME", "$rootDirectory/{$config["resultsFileName"]}");
define("ERRORS_FILENAME", "$rootDirectory/{$config["errorsFileName"]}");

//
try {
  // Create the [dao] layer
  $dao = new DaoImpotsWithTaxAdminDataInDatabase(DATABASE_CONFIG_FILENAME);
  // Create the [business logic] layer
  $business = new Business($dao);
  // Calculate taxes in batch mode
  $businessLogic = new BusinessLogic($dao);
} catch (TaxException $ex) {
  // display the error
  print "An error occurred: " . utf8_encode($ex->getMessage()) . "\n";
}
// end
print "Done\n";
exit;

15.2. Tests [Codeception]

This version, like the previous ones, is validated by [Codeception] tests.

Image

15.2.1. [DAO] layer testing

The [DaoTest.php] test is as follows:


<?php

// Strict adherence to the declared types of function parameters
declare (strict_types=1);

// namespace
namespace Application;

// definition of constants
define("ROOT", "C:/Data/st-2019/dev/php7/poly/scripts-console/impots/version-07");
// path to the configuration file
define("CONFIG_FILENAME", ROOT."/Data/config.json");
// retrieve the configuration
$config = \json_decode(\file_get_contents(CONFIG_FILENAME), true);
// include dependencies required by the script
$rootDirectory = $config["rootDirectory"];
foreach ($config["dependencies4calculate"] as $dependency) {
  require $rootDirectory . $config["dependencies"][$dependency];
}
// other constants
define("DATABASE_CONFIG_FILENAME", "$rootDirectory/{$config["databaseFilename"]}");

// test -----------------------------------------------------

class DaoTest extends \Codeception\Test\Unit {
  // TaxAdminData
  private $taxAdminData;

  public function __construct() {
    parent::__construct();
    // Create the [dao] layer
    $dao = new DaoImpotsWithTaxAdminDataInDatabase(DATABASE_CONFIG_FILENAME);
    $this->taxAdminData = $dao->getTaxAdminData();
  }

  // tests
  public function testTaxAdminData() {

  }

}

Comments

  • lines 9–21: definition of the test environment. We use the same environment, without the [business] layer, as the one used by the main script [MainCalculateImpotsWithTaxAdminDataInPostgresDatabase] described in the linked section;
  • lines 29–34: construction of the [dao] layer;
  • line 33: the [$this→taxAdminData] attribute contains the data to be tested;
  • Lines 37–39: The [testTaxAdminData] method is the one described in the linked section;

The test results are as follows:

Image

15.2.2. [Business] layer test

The [MetierTest.php] test is as follows:


<?php

// Strict adherence to the declared types of function parameters
declare (strict_types=1);

// namespace
namespace Application;

// definition of constants
define("ROOT", "C:/Data/st-2019/dev/php7/poly/scripts-console/impots/version-07");
// path to the configuration file
define("CONFIG_FILENAME", ROOT . "/Data/config.json");
// retrieve the configuration
$config = \json_decode(\file_get_contents(CONFIG_FILENAME), true);
// include dependencies required by the script
$rootDirectory = $config["rootDirectory"];
foreach ($config["dependencies4calculate"] as $dependency) {
  require $rootDirectory . $config["dependencies"][$dependency];
}
// other constants
define("DATABASE_CONFIG_FILENAME", "$rootDirectory/{$config["databaseFilename"]}");

// test class
class BusinessTest extends \Codeception\Test\Unit {
  // business layer
  private $business;

  public function __construct() {
    parent::__construct();
    // creation of the [DAO] layer
    $dao = new DaoImpotsWithTaxAdminDataInDatabase(DATABASE_CONFIG_FILENAME);
    // Create the [business] layer
    $this->business = new Business($dao);
  }

  // tests
  public function test1() {

  }

  -----------------------------------

  public function test11() {

  }

}

Comments

  • lines 9–21: definition of the test environment. We use the same one as the main script [MainCalculateImpotsWithTaxAdminDataInPostgresDatabase] described in the linked section;
  • lines 28–34: construction of the [dao] layer;
  • line 33: the attribute [$this→business] is a reference to the [business] layer to be tested;
  • lines 37–45: the methods [test1, test2…, test11] are those described in the linked section;

The test results are as follows:

Image