PHP coding task for web developer candidates.
Candidates are free to use language features from any stable version of PHP.
Candidates may add code comments or provide any supporting documentation they wish.
This task involves some questions and coding implementations with a mock ActiveRecord-style database ORM. The files for this task are contained are contained with the orm directory.
Assume that the ActiveRecord base class contains a working implementation of the type of features that you would expect of any database ORM.
The DownloadLog model represents a log entry denoting a user (identified by a user_id
) downloading a file (identified by file_id
).
- What does the
final
keyword mean the DownloadLog model? What are the implications in removing thefinal
declaration?
Answer:
- The
DownloadLog
class can not be extended.- After removing the
final
keyword, theDownloadLog
class can be extended by the sub class
- The current of implementation of DownloadLog contains a fatal error. What is it, and how would it be resolved?
Answer:
- The
isModified
method in theActiveRecordInterface
is not implemented.- To fix the issue, just need to implement the
isModified
method in theActiveRecord
class or in theDownloadLog
class.
Extend the current implementation so that the following code block provides the output as described:
Code block
$downloadLog = DownloadLog::create();
echo ($downloadLog->isModified() ? 'DownloadLog is modified' : 'DownloadLog is not modified');
$downloadLog->setFileId(1000)->setUserId(2000);
echo ($downloadLog->isModified() ? 'DownloadLog is modified' : 'DownloadLog is not modified');
echo ("UserId is: " . $downloadLog->getUserId());
Output
DownloadLog is not modified
DownloadLog is modified
UserId is: 2000
Destroying DownloadLog
Answer:
- Clone or download the code to your machine
- Make sure you have the latest version of PHP and composer installed on your machine
- Run
composer install
on the root directory of the project- Run
php test.php
to see the output
Create a trait
which validates that a value is numeric. Add this trait
to DownloadLog and use it to validate that user_id
and file_id
values are numeric. If they are not, throw an exception.
This task outlines some requirements for a one or more base classes that provide controller functionality as commonly found in MVC-style web application frameworks.
There is an existing web application framework that:
- Bootstraps an incoming request via a router.
- The router dispatches the request to a controller via a defined entry point method called
execute
. - The controller
execute
method expectsrequest
andresponse
objects as parameters. - The
request
object implements the Psr\Http\Message\ServerRequestInterface interface. - The
response
object implements the Psr\Http\Message\ResponseInterface interface. - The controller
execute
method returns a modifiedresponse
object.
The web application framework should provide functionality for handling HTTP requests that output both HTML web pages as well as JSON-encoded data responses (as commonly found in REST APIs, for example).
Create one or more base classes that can be integrated into the existing web application framework and provide the following functionality as outlined below.
Functional requirements
- Functionality to return an HTML response body along with any valid HTTP response codes and/or response headers.
- Functionality to return a JSON response body along with any valid HTTP response codes and/or response headers.
- Generic functionality to validate input parameters for a PUT or POST request.
You need not provide a concrete implementation of the base class(es) but can do so if it helps describes the base implementation.
You do not need to create concrete implementations of the request
and reponse
objects. Assume that these have been created and conform to the relevant interfaces as mentioned above.
- My implement of the code is on Controller
- clone or download the code to your machine
- Make sure you have the latest version of PHP and composer installed on your machine
- Run
composer install
on the root directory of the project- Run
composer test
to do the test- You should see something similar to the following output
> ./vendor/bin/phpunit
PHPUnit 7.5.7 by Sebastian Bergmann and contributors.
MjLiang\PhpCodingTask\Tests\orm\DownloadLogTest
✓Destroying DownloadLog
cannot assign string to file id [0.006s]
✓Destroying DownloadLog
cannot assign string to user id [0.000s]
✓Destroying DownloadLog
assign int to user id [0.001s]
✓Destroying DownloadLog
assign int to file id [0.000s]
✓Destroying DownloadLog
is not modified [0.000s]
✓Destroying DownloadLog
is modified [0.000s]
MjLiang\PhpCodingTask\Tests\task2\ControllerTest
✓ json post [0.013s]
✓ form post [0.000s]
✓ get method [0.000s]
✓ delete method [0.001s]
✓ other content type handlers [0.001s]
✓ other method request [0.001s]
✓ bad request [0.001s]
Time: 65 ms, Memory: 4.00 MB
OK (13 tests, 28 assertions)