Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix code and other implementaions of code optimisation #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
Technical Skills Assessment (PHP)
=================================
phpunit tests/testBasics.php

Requirements
Time: 42 ms, Memory: 2.75Mb

* Local development environment with PHP (5.4+) interpreter and MySQL (5.5+) server
OK (3 tests, 3 assertions)

Instructions
======================================

1. Create a Github account if you don’t have one already
2. Send an email to [email protected] with the subject of “PHP Skills Assessment” and include your Github account ID in the body. You will have 2 hours to complete the assessment from the time you send this email.
3. Fork the repository at: https://github.com/edgeprod/PHP-assesment into your account
4. Pull your forked repository into your IDE
5. Fix any PHP syntax errors you find. Errors have been intentionally placed in the code.
6. Modify code to meet PSR-1 and PSR-2 code style guidelines (hint: use PHP CodeSniffer)
7. Initialize and run Composer to get needed dependencies
8. Create a MySQL database and import the ‘schema.sql’ file
9. Update ‘config/database.php’ with credentials to access newly created database
10. Run unit tests to see failures
11. Make failing unit tests pass by fixing the code. Do not change the unit tests.
12. Push all changes back to your forked repository
13. Replace that ‘README.md’ file with the output of all the unit tests passing
14. Create a Pull Request to integrate the fixed code back into the master repository. The Pull Request will ultimately not be merged but will signal your completion of the skills assessment.
phpunit tests/testQuestion.php

Time: 93 ms, Memory: 2.75Mb

OK (7 tests, 12 assertions)
4 changes: 2 additions & 2 deletions boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
session_start();

require_once __DIR__ . '/vendor/autoload.php';
//require_once __DIR__ . '/core/logging.php';
//require_once __DIR__ . '/models/question.php';
require_once __DIR__ . '/core/logging.php';
require_once __DIR__ . '/models/question.php';
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/core/database.php';
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require-dev" : {
"phpunit/phpunit": "4.2.*"
}
"require-dev": {
"phpunit/phpunit": "4.2.*"
}
}
18 changes: 12 additions & 6 deletions config/database.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,54 @@
<?php

namespace interview;
namespace Interview\Config;

class Config_Database
{

private $credentials = array(
'host' => 'localhost',
'port' => NULL,
'host' => 'localhost',
'port' => NULL,
'database' => 'interview',
'user' => 'questions',
'pass' => 'answers'
'user' => 'questions',
'pass' => 'answers'
);

public function getHost()
{
return $this->credentials['host'];
}

//--------------------------------------------------------------------------


public function getPort{
public function getPort()
{
return $this->credentials['port'];
}

//--------------------------------------------------------------------------


public function getDatabase()
{
return $this->credentials['database'];
}

//--------------------------------------------------------------------------


public function getUser()
{
return $this->credentials['user'];
}

//--------------------------------------------------------------------------


public function getPass()
{
return $this->credentials['pass'];
}

//--------------------------------------------------------------------------
}
46 changes: 27 additions & 19 deletions core/database.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
<?php

namespace interview;
namespace Interview\Core;

class Database
{

class Database {
protected $link;
protected $connected;

public function __construct() {
$credentials = new Config_Database();
public function __construct()
{
$credentials = new \Interview\Config\Config_Database();

try {
$this->link = new \PDO(
'mysql:host=' . $credentials['host'] . 'dbname=' . $credentials['database'],
$credentials->getUser(),
$credentials->getPass(),
array(
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION)
'mysql:host=' . $credentials->getHost() . ';dbname=' . $credentials->getDatabase(), $credentials->getUser(), $credentials->getPass(), array(
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION)
);
} catch (\PDOException $e) {
Logging::logDBErrorAndExit($e->getMessage());
\Interview\Core\Logging::logDBErrorAndExit($e->getMessage());
}
}

//--------------------------------------------------------------------------


public function insert($tableName, $columns, $data, $ignore = false)
{
$statement = "INSERT";
$statement = "INSERT";

if ($ignore) {
$statement .= " IGNORE";
Expand All @@ -37,15 +38,19 @@ public function insert($tableName, $columns, $data, $ignore = false)
$statement .= " (";

for ($x = 0; $x < sizeof($columns); $x++) {
if ($x > 0) { $statement .= ', '; }
if ($x > 0) {
$statement .= ', ';
}
$statement .= $columns[$x];
}

$statement .= ")";
$statement .= " values (";

for ($x = 0; $x < sizeof($data); $x++) {
if ($x > 0) { $statement .= ', '; }
if ($x > 0) {
$statement .= ', ';
}
$statement .= "?";
}

Expand All @@ -55,15 +60,16 @@ public function insert($tableName, $columns, $data, $ignore = false)
$insert = $this->link->prepare($statement);
$insert->execute($data);
} catch (\PDOException $e) {
Logging::logDBErrorAndExit($e->getMessage());
\Interview\Core\Logging::logDBErrorAndExit($e->getMessage());
}
}

//--------------------------------------------------------------------------


public function updateOne($tableName, $column, $data, $where, $condition)
{
$statement = "UPDATE";
$statement = "UPDATE";

$statement .= " `" . $tableName . "`";
$statement .= " SET `";
Expand All @@ -78,9 +84,10 @@ public function updateOne($tableName, $column, $data, $where, $condition)
$update = $this->link->prepare($statement);
$update->execute(array($data, $condition));
} catch (\PDOException $e) {
Logging::logDBErrorAndExit($e->getMessage());
\Interview\Core\Logging::logDBErrorAndExit($e->getMessage());
}
}

//--------------------------------------------------------------------------


Expand All @@ -90,14 +97,15 @@ public function getArray($statement)
$sql = $this->link->query($statement);
$results = $sql->fetchAll(\PDO::FETCH_ASSOC);
} catch (\PDOException $e) {
Logging::logDBErrorAndExit($e->getMessage());
\Interview\Core\Logging::logDBErrorAndExit($e->getMessage());
}

if (!empty($results)) {
if (empty($results)) {
return false;
}

return $results;
}

//--------------------------------------------------------------------------
}
13 changes: 10 additions & 3 deletions core/logging.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<?php

namespace interview;
namespace Interview\Core;

class Logging
{

public static function logDBErrorAndExit($error)
{
die('An Error Occurred: ' . $error);
}

class Logging {
public static function logDBErrorAndExit($error) { die('An Error Occurred: ' . $error); }
//--------------------------------------------------------------------------
}
47 changes: 25 additions & 22 deletions models/question.php
Original file line number Diff line number Diff line change
@@ -1,79 +1,81 @@
<?php

namespace interview;
namespace Interview\Models;

class Question
{

public $id;
protected $name;
public $name;
public $text;
public $answer;
public $created;

protected $tableName = 'questions';
const TABLENAME = 'questions';
const TABLENAME = 'questions';

public function __construct($questionId, Database $db)
public function __construct($questionId, \Interview\Core\Database $db)
{
$sql = "SELECT * FROM `$this->tableName WHERE `id` = '" . $questionId . "' LIMIT 1;";

$sql = "SELECT * FROM `" . self::TABLENAME . "` WHERE `id` = '" . $questionId . "' LIMIT 1;";
$result = $db->getArray($sql);

$this->id = $questionId;
$this->name = $result[0]['name'];
$this->text = $result[0]['text'];
$this->answer = $result[0]['answer'];
$this->created = $result['created'];
$this->id = $questionId;
$this->name = $result[0]['name'];
$this->text = $result[0]['text'];
$this->answer = $result[0]['answer'];
$this->created = $result[0]['created'];
}

//--------------------------------------------------------------------------


public static function getNameById($questionId, Database $db)
public static function getNameById($questionId, \Interview\Core\Database $db)
{
$sql = "SELECT `name` FROM `" . self::TABLENAME . "` WHERE `id` = '" . $questionId . "' LIMIT 1;";
$result = $db->getArray($sql);

return $result[0]['name'];
}

//--------------------------------------------------------------------------


public static function getTextById($questionId, Database $db)
public static function getTextById($questionId, \Interview\Core\Database $db)
{
$sql = "SELECT `text` FROM `" . self::TABLENAME . "` WHERE `id` = '" . $questionId . "' LIMIT 1;";
$result = $db->getArray($sql);

return $this->text;
return $result[0]['text'];
}

//--------------------------------------------------------------------------


public static function getAnswerById($questionId, Database $db)
public static function getAnswerById($questionId, \Interview\Core\Database $db)
{
$sql = "SELECT `answer` FROM " . self::TABLENAME . "` WHERE `id` = '" . $questionId . "' LIMIT 1;";
$sql = "SELECT `answer` FROM `" . self::TABLENAME . "` WHERE `id` = '" . $questionId . "' LIMIT 1;";
$result = $db->getArray($sql);

return $result[0]['answer'];
}

//--------------------------------------------------------------------------


public static function getCreatedById($questionId, Database $db)
public static function getCreatedById($questionId, \Interview\Core\Database $db)
{
$sql = "SELECT `created` FROM `" . self::TABLENAME . "` WHERE `id` = '" . $questionId . "' LIMIT 1;";
$result = $db->getArray($sql);

return $result[0]['created'];
}

//--------------------------------------------------------------------------


public static function addQuestion($questionName, $questionText, $questionAnswer, Database $db)
public static function addQuestion($questionName, $questionText, $questionAnswer, \Interview\Core\Database $db)
{
$columns = array(
'name',
'text'
'text',
'answer'
);

Expand All @@ -87,5 +89,6 @@ public static function addQuestion($questionName, $questionText, $questionAnswer

return true;
}

//--------------------------------------------------------------------------
}
}
5 changes: 4 additions & 1 deletion schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ CREATE TABLE `questions` (

LOCK TABLES `questions` WRITE;
/*!40000 ALTER TABLE `questions` DISABLE KEYS */;
INSERT INTO `questions` VALUES (1,'MVC Principles','Explain MVC principles, and why they are important?','','2014-09-25 22:30:16'),(2,'Unit Testing','What constitutes a good unit test?','','2014-09-25 22:30:37'),(3,'Roadblocks','You are required to work with a huge chunk of code and you have no idea how it works and there’s no documentation and no tests. What steps do you take?','','2014-09-25 22:32:41');
INSERT INTO `questions` VALUES
(1,'MVC Principles','Explain MVC principles, and why they are important?','MVC - model-controller-view is a pattern for building applications. The main goal is to separate the functions which are working with data from thier views. In scheme it look like: User uses Controller, Controller manipulated Model, Model updates View, User sees View Well designed MVC application allow frontend and backend developers working on each part(frontend/backend) separetely. Model is working with data, change the states of these data, however does not know about views of these data. Views only showing the data Controller provides a link between user and the system and uses the model to implement the necessary reaction. Well designed MVC application allow frontend and backend developers working on each part(frontend/backend) separetely.','2014-09-25 22:30:16'),
(2,'Unit Testing','What constitutes a good unit test?','Good unit test should be reliable, easy to maintain, not depend on the environment in which they are performed, easy to read and easy to understand, observe a single naming convention, regularly run in automatic mode.','2014-09-25 22:30:37'),
(3,'Roadblocks','You are required to work with a huge chunk of code and you have no idea how it works and there’s no documentation and no tests. What steps do you take?','Steps are really depends on the developer\'s tasks. For example, the code is working not correctly and I need to figure out the issue and fix without any refactoting, then it can be done via die(var_dump()) or debugger.','2014-09-25 22:32:41');
/*!40000 ALTER TABLE `questions` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
Expand Down
Loading