Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Eloquent in Slim Framework

DZone's Guide to

Using Eloquent in Slim Framework

We have a look at using the Eloquent ORM with PHP's Slim Framework. Follow along with a complete example!

· Web Dev Zone
Free Resource

Should you build your own web experimentation solution? Download this whitepaper by Optimizely to find out.

Eloquent is one of the easier ORMs to get started with, so let's look at how to use and test it within a Slim Framework application.

Setup

You can add Eloquent to your Slim project via composer:

composer require illuminate/database

You now need to bootstrap it. To do this we need some settings, so let's do that in our standard Slim configuration:

$config = [
    'settings' => [
        'db' => [
            // Eloquent configuration
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'bookshelf',
            'username'  => 'rob',
            'password'  => '123456',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ],
    ],
]

We then instantiate the capsule manager and boot Eloquent , probably in index.php:

$container = $app->getContainer()
$capsule = new Illuminate\Database\Capsule\Manager;
$capsule->addConnection($container->get('settings')['db']);
$capsule->bootEloquent();

From this point, you can simply follow the documentation to create your Eloquent models and use them within your route callables.

Testing Your Models

At first glance, it seems quite hard to test your models as the find method is a static.

For example, consider this route callback within an action class:

public function __invoke($request, $response, $args)
{
    return $this->view->render($response, 'author/list.twig', [
        'books' => Author::all(),
    ]);
});

To test it, I followed a tip from Taylor Otwell and wrapped the find method in a class that I could then mock:

namespace Bookshelf;

class AuthorRepository
{
    public function all()
    {
        return Author::all();
    }

    public function find($id, $columns = ['*'])
    {
        return Author::find($id, $columns);
    }
}

The callback then becomes:

public function __invoke($request, $response, $args)
    return $this->view->render($response, 'author/list.twig', [
        'books' => $this->authorRepository->all()
    ]);
});

Like the view, the authorRepository is injected into the constructor and so can be mocked out. The full class looks like this:

namespace Bookshelf;

use Slim\Views\Twig;
use Bookshelf\AuthorRepository;

final class ListAuthorsAction
{
    private $view;
    private $authorRepository;

    public function __construct(
        Twig $view,
        AuthorRepository $authorRepository
    ) {
        $this->view = $view;
        $this->authorRepository = $authorRepository;
    }

    public function __invoke($request, $response, $args)
    {
        return $this->view->render($response, 'author/list.twig', [
            'authors' => $this->authorRepository->all()
        ]);
    }
}

To test using PHPUnit, we start by creating a mock for the AuthorRepository:

$authorRepository = $this->getMockBuilder(AuthorRepository::class)
    ->setMethods(['all'])
    ->disableOriginalConstructor()
    ->getMock();

Then we set the expectation that the all method will return a collection of Authors:

use Illuminate\Database\Eloquent\Collection;
$authors = new Collection([
    new Author(['name' => 'test1']),
    new Author(['name' => 'test2']),
    new Author(['name' => 'test3']),
]);

$authorRepository->expects($this->once())
    ->method('all')
    ->willReturn($authors);

We do the same for the view object:

$view = $this->getMockBuilder(Twig::class)
    ->setMethods(['render'])
    ->disableOriginalConstructor()
    ->getMock();

$response = new Response();
$response->write('Rendered text on page');

$this->view->expects($this->once())
    ->method('render')
    ->with($response, 'author/list.twig', ['authors' => $authors])
    ->willReturn($response);

Now, we can instantiate the action class with our two mock objects. We then create request and response objects and run the action and test the returned response:

$action = new ListAuthorsAction($view, $authorRepository);
$request = Request::createFromEnvironment(Environment::mock());
$response = new Response();

$ReturnedResponse = $action($request, $response);

$this->assertSame((string)$ReturnedResponse->getBody(), 'Rendered text on page');

Summary

Eloquent is quite a nice implementation of the Active Record pattern. If that meets your needs, then it is easy to use within a Slim Framework application, so go for it!

Implementing an Experimentation Solution: Choosing whether to build or buy?

Topics:
php ,web dev ,slim framework ,orm ,example

Published at DZone with permission of Rob Allen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}