Skip to content

15. تمرين تطبيقي – الإصدار 7

Image

15.1. التنفيذ

هنا، سنعيد النظر في الإصدار 6 عن طريق نقل الثوابت المستخدمة في البرامج النصية الرئيسية إلى ملف تكوين. سيكون ملف التكوين ملف JSON يحتوي على المحتوى التالي:


{
    "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"]
}

في هذا التكوين:

  • السطر 2: الدليل الذي تُقاس منه جميع المسارات في ملف التكوين هذا؛
  • الأسطر 3–7: المسارات إلى جميع ملفات JSON في التطبيق؛
  • الأسطر 8-22: المسارات إلى جميع ملفات التطبيق، في شكل [مفتاح=>مسار]؛
  • الأسطر 23-34: التبعيات لحساب الضرائب في شكل قائمة مفاتيح من قاموس [dependencies] (الأسطر 8-22)؛
  • الأسطر 35–44: التبعيات لنقل ملف JSON [taxadmindata.json] إلى قاعدة البيانات في شكل قائمة مفاتيح من قاموس [dependencies] (الأسطر 8–22)؛

يصبح البرنامج النصي لنقل بيانات إدارة الضرائب إلى قاعدة البيانات [MainTransferAdminDataFromFile2PostgresSQLDatabase.php] كما يلي:


<?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;

تعليقات

يظل الكود كما هو في القسم المرتبط. والفرق الوحيد هو استخدام ملف [config.json] بدلاً من الثوابت في الأسطر 18–26؛

  • السطر 16: تقوم الدالة [file_get_contents] بنقل ملف [config.json] إلى سلسلة. ثم تستخدم الدالة [json_decode] هذه السلسلة لإنشاء قاموس [$config]. تشير المعلمة الثانية [true] للدالة [json_decode] إلى أننا نريد إنشاء قاموس؛
  • الأسطر 19–22: نقوم بتضمين التبعيات المطلوبة للنص البرمجي لنقل البيانات من ملف [taxadmindata.json] إلى قاعدة البيانات؛
    • [$config["dependencies4transfer"]] هو مصفوفة التبعيات المطلوبة من قبل البرنامج النصي للنقل. وهي عبارة عن قائمة من المفاتيح. توجد مسارات الملفات المراد تضمينها في المشروع في قاموس [$config["dependencies"]
    • $config["rootDirectory"] يمثل المسار الذي يجب أن تسبقه الملفات المراد تضمينها؛

وبالمثل، يصبح البرنامج النصي لحساب الضرائب كما يلي [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. اختبارات [Codeception]

تم التحقق من صحة هذه النسخة، مثل النسخ السابقة، بواسطة اختبارات [Codeception].

Image

15.2.1. اختبار طبقة [DAO]

اختبار [DaoTest.php] هو كما يلي:


<?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() {

  }
 
}

تعليقات

  • الأسطر 9–21: تعريف بيئة الاختبار. نستخدم نفس البيئة، بدون طبقة [الأعمال]، التي يستخدمها البرنامج النصي الرئيسي [MainCalculateImpotsWithTaxAdminDataInPostgresDatabase] الموصوف في القسم المرتبط؛
  • الأسطر 29–34: إنشاء طبقة [dao]؛
  • السطر 33: تحتوي السمة [$this→taxAdminData] على البيانات المراد اختبارها؛
  • الأسطر 37–39: طريقة [testTaxAdminData] هي الطريقة الموصوفة في القسم المرتبط؛

نتائج الاختبار هي كما يلي:

Image

15.2.2. اختبار طبقة [Business]

اختبار [MetierTest.php] هو كما يلي:


<?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() {

  }
 
}

تعليقات

  • الأسطر 9–21: تعريف بيئة الاختبار. نستخدم نفس البيئة المستخدمة في البرنامج النصي الرئيسي [MainCalculateImpotsWithTaxAdminDataInPostgresDatabase] الموصوف في القسم المرتبط؛
  • الأسطر 28–34: إنشاء طبقة [dao]؛
  • السطر 33: السمة [$this→business] هي إشارة إلى طبقة [business] المراد اختبارها؛
  • الأسطر 37–45: الطرق [test1، test2…، test11] هي تلك الموصوفة في القسم المرتبط؛

نتائج الاختبار هي كما يلي:

Image