This script works with PHP versions 8.2+ and requires composer
Unit tests are automatically run and passing on PHP 8.2 - 8.5.
Although there are no external library dependencies for the running of the script, it uses composer's autoloader to manage dependency autoloading.
To install, clone this repository and run composer install to install PHPunit and initialise the autoloader.
or
Run composer dump if you do not want to run tests and simply want to use the cli script.
The script should be run in the following way:
php console.php --action {action} --file {file}where the required parameters are:
{file} - a csv source file where each row contains two numbers between -100 and 100, delimited with a semicolon (;)
{action} - the action performed on the two numbers taken from each row of the above file. The actions can be:
- plus - the sum of both numbers
- minus - the result of subtracting the second number from the first number
- multiply - the result of multiplying the two numbers
- division - the result of dividing the first number by the second number
The results of this operation are written to a semicolon-delimited csv file file called output.csv.
The output format is:
first number;second number;result
If any of the rows in the {file} are invalid, they will be logged in the generated error.log file along with an message explaining why.
Rows are invalid if:
- they do not contain two values
- The result of the operation is not greater than zero
- They are in an incorrect format (e.g. strings, whitespace)
- The mathematical operation is not possible (e.g. division by zero)
Note that the output files error.log and output.csv are overwritten on each run of the script.
If you wish to keep these files, copy them out of the script directory before running the script again.
From the original code, the command line application has been refactored in the following ways:
- the large do everything classes called ClassOne...etc have been replaced with testable classes that have single responsibilities. See the
srcdirectory for details. These classes also have a consistent naming convention and interface, unlike the original classes. - Autoloading is implemented to manage file inclusion, ensuring that files are included only when used.
- Repeated functionality such as opening files, reading/writing CSV files, logging output etc has been extracted to independent classes that can be used in class composition to carry out these tasks. This ensures that there is one source of each responsibility.
- The action type is handled with an enum, to ensure consistency, rather than relying on passing strings.
- The strategy pattern is implemented for each mathematical operation to allow specific logic to be executed depending on the
{action}chosen at runtime. This makes for a more easily testable, expandable set of capabilities. See thesrc/Actionsdirectory for details. - The
CsvReaderclass returns a generator for memory-efficient reading of large files while making it easy for the consumer to loop over records as though it were an array. It also has built-in prevention of directory-traversal to prevent relative paths being used to read system files. - Unit tests have been generated for the classes that were created. These are run on each push to the
mainbranch.
To run the PHPunit tests, ensure that PHPunit is installed
composer installand run the tests (see tests directory for details)
composer testWe have prepared for you simple test task what as we believe, allow us to estimate your experience. It is a small php-script, which should be started in console like:
php console.php --action {action} --file {file}
Script will take two required parameters:
{file} - csv-source file with numbers, where each row contains two numbers between -100 and 100, and
{action} - what action should we do with numbers from {file}, and can take next values:
- plus - to count summ of the numbers on each row in the {file}
- minus - to count difference between first number in the row and second
- multiply - to multiply the numbers on each row in the {file}
- division - to divide first number in the row and second
As result of the command execution should be csv file with three columns: first number, second number, and result. In CSV-file should be written ONLY numbers greater than null. If result less than null - it should be written in logs.
Example 1
php console.php --action plus --file {file}, where in file you can find next numbers:
10 20
-30 20
-3 5
As result in CSV file you should write:
10 20 30
-3 5 2
And in log file, something like "numbers are - 30 and 20 are wrong"
Example 2
php console.php --action division --file {file}, where in file you can find next numbers:
20 10
-30 20
3 0
As result in CSV file you should write:
20 10 2
And in log file, something like:
numbers are -30 and 20 are wrong
numbers are 3 and 0 are wrong, is not allowed
You need to refactor code and write it on proper way. Just do your best: update/delete/add code as you wish.
After finishing - please push your code in your github/bitbucket account, and send me link back.
- After refactoring code shoud work
- Code should work on PHP8.2+
- As file source example please use test.csv
Please put result of your work in your Github or Bitbucket account, and send link back.