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",
        "ExceptionImpots": "/../version-05/Entities/ExceptionImpots.php",
        "Utilitaires": "/../version-05/Utilities/Utilitaires.php",
        "InterfaceDao": "/../version-05/Dao/InterfaceDao.php",
        "TraitDao": "/../version-05/Dao/TraitDao.php",
        "InterfaceDao4TransferAdminData2Database": "/../version-05/Dao/InterfaceDao4TransferAdminData2Database.php",
        "DaoTransferAdminDataFromJsonFile2Database": "/../version-05/Dao/DaoTransferAdminDataFromJsonFile2Database.php",        
        "DaoImpotsWithTaxAdminDataInDatabase": "/../version-05/Dao/DaoImpotsWithTaxAdminDataInDatabase.php",
        "InterfaceMetier": "/../version-05/Métier/InterfaceMetier.php",
        "Metier": "/../version-05/Métier/Metier.php"
    },
    "dependencies4calculate": [
        "BaseEntity",
        "TaxAdminData",
        "TaxPayerData",
        "Database",
        "ExceptionImpots",
        "Utilitaires",
        "InterfaceDao",
        "TraitDao",
        "DaoImpotsWithTaxAdminDataInDatabase",
        "InterfaceMetier",
        "Metier"],
    "dependencies4transfer": [
        "BaseEntity",
        "TaxAdminData",
        "Database",
        "ExceptionImpots",
        "Utilitaires",
        "InterfaceDao",
        "TraitDao",  
        "InterfaceDao4TransferAdminData2Database",
        "DaoTransferAdminDataFromJsonFile2Database"]
}

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 declared types of function parameters
declare (strict_types=1);
 
// namespace
namespace Application;
 
// error handling by PHP
// ini_set("display_errors", "0");
//
// configuration file path
define("CONFIG_FILENAME", "../Data/config.json");
 
// we retrieve the configuration
$config = \json_decode(\file_get_contents(CONFIG_FILENAME), true);
 
// include the necessary script dependencies
$rootDirectory = $config["rootDirectory"];
foreach ($config["dependencies4transfer"] as $dependency) {
  require $rootDirectory . $config["dependencies"][$dependency];
}
 
// definition of constants
define("DATABASE_CONFIG_FILENAME", "$rootDirectory/{$config["databaseFilename"]}");
define("TAXADMINDATA_FILENAME", "$rootDirectory/{$config["taxAdminDataFileName"]}");
 
//
try {
  // creation of the [dao] layer
  $dao = new DaoTransferAdminDataFromJsonFile2Database(DATABASE_CONFIG_FILENAME, TAXADMINDATA_FILENAME);
  // data transfer to the database
  $dao->transferAdminData2Database();
} catch (ExceptionImpots $ex) {
  // error is displayed
  print $ex->getMessage() . "\n";
}
// end
print "Terminé\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 declared types of function parameters
declare (strict_types=1);
 
// namespace
namespace Application;
 
// error handling by PHP
// ini_set("display_errors", "0");
//
// configuration file path
define("CONFIG_FILENAME", "../Data/config.json");
 
// we retrieve the configuration
$config = \json_decode(\file_get_contents(CONFIG_FILENAME), true);
 
// include the necessary script dependencies
$rootDirectory = $config["rootDirectory"];
foreach ($config["dependencies4calculate"] as $dependency) {
  require $rootDirectory . $config["dependencies"][$dependency];
}
 
// definition of 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 {
  // creation of the [dao] layer
  $dao = new DaoImpotsWithTaxAdminDataInDatabase(DATABASE_CONFIG_FILENAME);
  // creation of the [business] layer
  $métier = new Metier($dao);
  // tax calculation in batch mode
  $métier->executeBatchImpots(TAXPAYERSDATA_FILENAME, RESULTS_FILENAME, ERRORS_FILENAME);
} catch (ExceptionImpots $ex) {
  // error is displayed
  print "Une erreur s'est produite : " . utf8_encode($ex->getMessage()) . "\n";
}
// end
print "Terminé\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 declared function parameter types
declare (strict_types=1);
 
// namespace
namespace Application;
 
// defining constants
define("ROOT", "C:/Data/st-2019/dev/php7/poly/scripts-console/impots/version-07");
// configuration file path
define("CONFIG_FILENAME", ROOT."/Data/config.json");
// we retrieve the configuration
$config = \json_decode(\file_get_contents(CONFIG_FILENAME), true);
// include the necessary script dependencies
$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();
    // creation of 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 declared types of function parameters
declare (strict_types=1);
 
// namespace
namespace Application;
 
// defining constants
define("ROOT", "C:/Data/st-2019/dev/php7/poly/scripts-console/impots/version-07");
// configuration file path
define("CONFIG_FILENAME", ROOT . "/Data/config.json");
// we retrieve the configuration
$config = \json_decode(\file_get_contents(CONFIG_FILENAME), true);
// include the necessary script dependencies
$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 MetierTest extends \Codeception\Test\Unit {
  // business layer
  private $métier;
 
  public function __construct() {
    parent::__construct();
    // creation of the [dao] layer
    $dao = new DaoImpotsWithTaxAdminDataInDatabase(DATABASE_CONFIG_FILENAME);
    // creation of the [business] layer
    $this->métier = new Metier($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