Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Memory Repository Bundle Laravel Package

beelab/memory-repository-bundle

View on GitHub
Deep Wiki
Context7

BeelabMemoryRepositoryBundle

The purpose of this bundle is using in-memory repositories with Doctrine. The main usage is during tests, when you can avoid to interact with database, without changing any implementation.

Installation

Add these lines to your composer.json:

    "require": {
        "doctrine/orm": "2.5.*@dev",
        "doctrine/dbal": "2.5.*@dev",
        "doctrine/doctrine-bundle": "~1.3@beta",
        "beelab/memory-repository-bundle": "0.1.*@dev"
    }

then run composer update.

Usage

Suppose you are using an Entity named Article. In your entity, you must add repositoryClass value to @ORM\Entity annotation. For example:

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Acme\DemoBundle\Repository\ArticleRepository")
 */
class Article
{
    // ...
}

So, you need an ArticleRepositoryClass, that extends Doctrine's EntityRepository. Now, create a new class, like this:

<?php

namespace Acme\DemoBundle\MemoryRepository;

use Acme\DemoBundle\Entity\Article;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\Mapping\ClassMetadata;

class ArticleRepository implements ObjectRepository
{
    protected $_entityName, $_em, $_class;

    /**
     * @param EntityManager         $em
     * @param Mapping\ClassMetadata $class
     */
    public function __construct($em, ClassMetadata $class)
    {
        $this->_entityName = $class->name;
        $this->_em         = $em;
        $this->_class      = $class;
    }

    /**
     * {@inheritDoc}
     */
    public function find($id)
    {
        // implement as you prefer...
    }

    /**
     * {@inheritDoc}
     */
    public function findAll()
    {
        // implement as you prefer...
    }

    /**
     * {@inheritDoc}
     */
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
    {
        // implement as you prefer...
    }

    /**
     * {@inheritDoc}
     */
    public function findOneBy(array $criteria)
    {
        // this is just an example...
        if (isset($criteria['slug']) && $criteria['slug'] == 'an-article') {
            $article = new Article();
            $article
                ->setTitle('An article')
                ->setText('Lorem ipsum dolor sit amet.')
            ;

            return $article;
        }
    }

    /**
     * {@inheritDoc}
     */
    public function getClassName()
    {
        return $this->_entityName;
    }
}

For now, you must use directories (and namespaces) named "Repository" and "EntityRepository". This is a bit ugly, we hope to improve.

Finally, add this to your config_test.yml:

doctrine:
    orm:
        repository_factory: beelab.repository.factory

Done! Now, when an Article object is retrieved in a test, it's not retrieved from database, but from your ArticleRepository, depending on your logic.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
andydefer/laravel-notification
adachsoft/cli-process-tester
jhaskell13/laraflow
sebastienheyd/boilerplate-media-manager
ccast/tagixo-primix
pretendtrue/laravel-builder
sawmainek/laravel-scout-tntsearch-driver
tomshaw/laravel-calendar-table
dibakar/laravel-ownership
steadrun/laravel-monitor
labyrinthe/connect-laravel
elmogy/layan
drewdan/sentinel-client
dskripchenko/laravel-llm
fatryst/nuonuo
stevenay/myanfont
gmostafa/php-graphql-client
xeviant/paystack
snowsoft/faker
meditationsteps/blogetc