Last active
September 17, 2018 04:17
-
-
Save ravisharmaa/4065a6ca46b2cf05bb93923aa3fbee94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: ravibastola | |
* Date: 9/16/18 | |
* Time: 11:09 PM. | |
*/ | |
namespace App; | |
class Employee | |
{ | |
/** | |
* @var string | |
*/ | |
protected $name; | |
/** | |
* Employee constructor. | |
* | |
* @param $address | |
*/ | |
public function __construct($name) | |
{ | |
$this->name = $name; | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: ravibastola | |
* Date: 9/16/18 | |
* Time: 11:09 PM. | |
*/ | |
namespace App; | |
class EmployeeRepository | |
{ | |
private $dbConnection; | |
public function __construct($dbConnection) | |
{ | |
$this->dbConnection = new \mysqli('localhost', 'root', '', 'office'); | |
} | |
/** | |
* @param $address | |
* @return array | |
* @throws \Exception | |
*/ | |
public function findByAddress($address) | |
{ | |
$sql = 'SELECT * FROM employees WHERE address = ?'; | |
$stmt = $this->dbConnection->prepare($sql); | |
if (!$stmt) { | |
throw new \Exception($this->dbConnection->getError()); | |
} | |
$stmt->bind_param('s', $address); | |
if (!$stmt->execute()) { | |
return []; | |
} | |
$result = $stmt->get_result(); | |
$employeeName = []; | |
while ($data = $result->fetch_assoc()) { | |
$employeeName[] = new Employee($data['name']); | |
} | |
$result->free(); | |
return $employeeName; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\EmployeeRepository; | |
use PHPUnit\Framework\TestCase; | |
/** | |
* Created by PhpStorm. | |
* User: ravibastola | |
* Date: 9/16/18 | |
* Time: 11:05 PM. | |
*/ | |
class EmployeeRepositoryTest extends TestCase | |
{ | |
private $employeeRepository; | |
private $databaseMock; | |
public function setUp() | |
{ | |
parent::setUp(); | |
$this->databaseMock = Mockery::mock(mysqli::class); | |
$this->employeeRepository = new EmployeeRepository($this->databaseMock); | |
} | |
/** | |
* @test | |
*/ | |
public function it_finds_the_employees_by_given_address() | |
{ | |
$result = Mockery::spy('mysqli_result_mock')->shouldIgnoreMissing(); | |
$result->shouldReceive('fetch_assoc') | |
->andReturn(['name' => 'John Doe'], null); | |
$statement = Mockery::spy('mysqli_stmt_mock'); | |
$statement->shouldReceive('bind_param') | |
->with('s', 'Kathmandu'); | |
$statement->shouldReceive('execute') | |
->andReturn(true); | |
$statement->shouldReceive('get_result') | |
->andReturn($result); | |
$this->databaseMock->shouldReceive('prepare') | |
->with('SELECT * FROM employees WHERE address = ?') | |
->andReturn($statement); | |
$employeeName = $this->employeeRepository->findByAddress('Kathmandu'); | |
$this->assertInstanceOf(\App\Employee::class, $employeeName[0]); | |
$this->assertSame('John Doe', $employeeName[0]->getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment