PHP code example of cydrickn / laravel-query-factory
1. Go to this page and download the library: Download cydrickn/laravel-query-factory library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cydrickn / laravel-query-factory example snippets
// Model
use Illuminate\Database\Eloquent\Model;
class Person extends Model {
}
// Repository
use Illuminate\Support\Collection;
class PersonRepository
{
public function findPerson(int $id): ?Person
{
return Person::find(1);
}
}
// Test
class PersonRepositoryTest extends TestCase
{
public function testFindPerson()
{
factory(Person::class)->create(['id' => 1]);
$person = $this->app->make(PersonRepository::class)->findPerson(1);
$this->assertInstanceOf(Person::class, $person);
$this->assertSame(1, $person->id);
}
}
'providers' => [
// Other Service Providers
LaravelQueryFactory\QueryFactoryProvider::class,
];
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use LaravelQueryFactory\Models\Traits\QueryFactoryTrait;
class Person extends Model
{
use QueryFactoryTrait;
protected $fillable = ['name', 'gender'];
}
namespace App\Repository;
use App\Models\Person;
use Illuminate\Support\Collection;
class PersonRepository
{
public function findById(): ?Person
{
return Person::find(1);
}
public function findByGender(string $gender): Collection
{
return Person::where('gender', '=', $gender)->get();
}
}
namespace App\Tests\Unit;
use LaravelQueryFactory\Traits\MockQueryFactory;
use LaravelQueryFactory\Facades\QueryFactoryFacade;
class PersonRepositoryTest extends TestCase
{
use MockQueryFactory;
/**
* Test by just using mock connection and QueryFacades
* and you can assert the generated sql.
*/
public function testFindWithGeneratedSql()
{
$connection = $this->mockConnection('mysql');
$queryBuilder = Person::newQueryBuilder();
$queryBuilder->connection = $connection;
QueryFactoryFacade::shouldReceive('createQueryBuilder')->andReturn($queryBuilder);
$repository = new PersonRepository();
$repository->findById(1);
$this->assertSame('select * from `people` where `people`.`id` = ? limit 1', $queryBuilder->toSql());
$this->assertSame([1], $queryBuilder->getBindings());
}
/**
* Test by mocking connection with result and QueryFacades
*/
public function testFindWithResult()
{
$connection = $this->mockConnection('mysql');
// Mock result from connection
$connection->shouldReceive('select')->once()->andReturn([['id' => 1]]);
$queryBuilder = Person::newQueryBuilder();
$queryBuilder->connection = $connection;
QueryFactoryFacade::shouldReceive('createQueryBuilder')->andReturn($queryBuilder);
$repository = new PersonRepository();
$person = $repository->findById(1);
$this->assertInstanceOf(Person::class, $person);
}
/**
* Test by mocking query builder
*/
public function testFindByGenderWithMockingQueryBuilder()
{
$connection = $this->mockConnection('mysql');
// Mocking Query Builder
$queryBuilder = Mockery::mock(QueryBuilder::class);
$queryBuilder->shouldReceive('getConnection')->andReturn($connection);
$queryBuilder->shouldReceive('from')->with('people')->andReturnSelf();
$queryBuilder->shouldReceive('where')->once()->with('gender', '=', 'male')->andReturnSelf();
$queryBuilder->shouldReceive('get')->once()->with(['*'])->andReturnSelf();
$queryBuilder->shouldReceive('all')
->once()
->withNoArgs()
->andReturn([['id' => 1, 'gender' => 'male']]);
QueryFactoryFacade::shouldReceive('createQueryBuilder')->andReturn($queryBuilder);
$repository = new PersonRepository();
$persons = $repository->findByGender('male');
$this->assertSame(1, $persons->first()->id);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.