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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"UtilsTest::testRandomNumberWithinRange":8,"UtilsTest::testRandomness":8,"UtilsTest::testEdgeCaseSingleValue":8},"times":{"UtilsTest::testRandomNumberWithinRange":0.371,"UtilsTest::testRandomness":0.001,"UtilsTest::testEdgeCaseSingleValue":0.001,"utilsTest::testRandomNumberWithinRange":0.009,"utilsTest::testRandomness":0.002,"utilsTest::testEdgeCaseSingleValue":0}}
40 changes: 28 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
# PHP Evaluation
Submission for PHP Evaluation Project

## You must create a branch on this repository to push your work.
Dear Evaluators,

### Task 1
I am pleased to submit my work for the PHP evaluation project. Here’s an overview of what I have implemented:

* Write testing functions to check the function `getSecureRandom` in `utils.php` really returns strong and accpetable random numbers
Overview
In this submission, I created a PHP API that generates secure random numbers using the getSecureRandom function located in utils.php. Additionally, I implemented comprehensive testing functions to ensure the reliability and strength of the random numbers produced.

* Try at least to write 3 testing cases
* Structure/write the testing function in any way you think is best and fast
Task 1: Testing the getSecureRandom Function
I developed three testing cases to validate the functionality of getSecureRandom:

### Task 2
Random Number Range: Ensures that the generated number falls within the specified minimum and maximum range.
Randomness: Executes multiple calls to verify that not all numbers are the same, demonstrating randomness.
Edge Cases: Tests scenarios where the minimum and maximum values are equal, confirming that the expected value is returned.
These test cases help guarantee the integrity of the random number generation process.

* Create `getSecureRandom` API that is served on some server
* The API must use `getSecureRandom` in `utils.php` to get the response
* Structure/write the API in any way you think is best and fast
Task 2: API Implementation
I also created a simple API endpoint named api.php that:

### Note
* You have to compromise implementation quality for task speed. Simple working approaches that are implemented fast is more preferred than best implementation that is written slow.
Accepts GET requests with min and max parameters.
Validates input and responds with the generated random number in JSON format.
Handles errors gracefully, providing informative error messages for common issues, such as missing parameters or invalid ranges.
Testing
The API can be easily tested with standard HTTP request tools. Sample requests and their expected responses are included in the README file for reference.

Conclusion
I focused on delivering a simple yet functional implementation while ensuring that the code is easy to understand and maintain. I look forward to your feedback and hope you find my project meets the evaluation criteria.

Thank you for your time!

Best regards,
kirubel gulilat
kirub21
11/23/2024
50 changes: 50 additions & 0 deletions api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

require_once 'utils.php'; // Include Utils class

header("Content-Type: application/json");

// Check if running in CLI
if (php_sapi_name() === 'cli') {
echo json_encode(['error' => 'This script must be run from a web server.']);
exit;
}

if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
http_response_code(405); // Method Not Allowed
echo json_encode(['error' => 'Only GET method is allowed.']);
exit;
}

// Add debug line to check GET parameters
var_dump($_GET); // Check what parameters are available

// Sanitize and validate inputs
$min = isset($_GET['min']) ? (int)$_GET['min'] : null;
$max = isset($_GET['max']) ? (int)$_GET['max'] : null;

if ($min === null || $max === null) {
http_response_code(400); // Bad Request
echo json_encode(['error' => 'Missing required parameters "min" and "max".']);
exit;
}

if ($min > $max) {
http_response_code(400); // Bad Request
echo json_encode(['error' => '"min" cannot be greater than "max".']);
exit;
}

try {
// Ensure min and max are sanitized
$randomNumber = utils::getSecureRandom($min, $max);
echo json_encode(['random_number' => $randomNumber]);
} catch (Exception $e) {
http_response_code(500); // Internal Server Error
echo json_encode([
'error' => 'Internal server error.',
'details' => $e->getMessage()
]);
}

?>
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"require-dev": {
"phpunit/phpunit": "^11.4"
},
"autoload": {
"classmap": [
"utils.php"
]
}
}
Loading