Skip to content

Instantly share code, notes, and snippets.

@hex333ham
Last active May 5, 2019 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hex333ham/9fae56fd8f0cdf26f7377a150dc9d72a to your computer and use it in GitHub Desktop.
Save hex333ham/9fae56fd8f0cdf26f7377a150dc9d72a to your computer and use it in GitHub Desktop.
Setup FOSMessageBundle testing env with FOSUserBundle

Simple list of commands to get setup quickly (perhaps not ideally) with FOSMessageBundle and FOSUserBundle, also includes config notes and doctrine entites.

The aim is to test the bundle for stability, not build a scalable web app.

FOSMessageBundle - https://github.com/FriendsOfSymfony/FOSMessageBundle

Composer

To be ran from a Symfony project - https://symfony.com/doc/current/setup.html

composer install
composer require --dev symfony/browser-kit symfony/css-selector symfony/phpunit-bridge symfony/maker-bundle symfony/web-server-bundle
composer require doctrine/doctrine-bundle doctrine/orm symfony/translation swiftmailer-bundle
composer require friendsofsymfony/user-bundle "~2.0"
composer require friendsofsymfony/message-bundle dev-master

Config

services.yaml

parameters:
    mailer_user: '%env(MAILERUSER)%'
    locale: 'en'

fos_user:
    db_driver: orm # other valid values are 'mongodb' and 'couchdb'
    firewall_name: main
    user_class: App\Entity\User
    from_email:
        address: "%mailer_user%"
        sender_name: "%mailer_user%"

fos_message:
    db_driver: orm
    thread_class: App\Entity\Thread
    message_class: App\Entity\Message

framework.yaml

Add at bottom

    templating:
        engines:
            twig

routes.yaml

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

fos_message:
    resource: "@FOSMessageBundle/Resources/config/routing.xml"
    prefix: /messaging

security.yaml

security:
    # https://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers

    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_MOD:         ROLE_USER
        ROLE_ADMIN:       ROLE_MOD
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        in_memory: { memory: ~ }
        fos_userbundle:
            id: fos_user.user_provider.username_email

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register$, role: IS_AUTHENTICATED_ANONYMOUSLY }

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            provider: in_memory

            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager
                remember_me: true

            logout:       true
            anonymous:    true

            remember_me:
                secret:   '%kernel.secret%'
                lifetime: 604800 # 1 week in seconds
                path:     /
                domain:   ~

            #anonymous: ~

            # activate different ways to authenticate

            # http_basic: ~
            # https://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate

            # form_login: ~
            # https://symfony.com/doc/current/cookbook/security/form_login_setup.html

.env

Just make sure that DATABASE_URL, MAILER_URL are setup and MAILERUSER is too.

DATABASE_URL=mysql://users:password@127.0.0.1:3306/db_name

###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=null://localhost
MAILERUSER='test@test.com'
###< symfony/swiftmailer-bundle ###

Entites

Copy the php files into your src/Entity folder, then run the following doctrine commands:

php bin/console doctrine:database:create
php bin/console make:migration
php bin/console doctrine:migrations:migrate

Run and test

Manual testing

Create two users:

php bin/console fos:user:create test test@example.com p@ssword
php bin/console fos:user:create test2 test@example.com p@ssword

Run your project (and clear the cache):

php bin/console cache:clear
php bin/console server:run

The project won't have any CSS at all so use these urls to navigate:

Automated tests (TDD)

To run the tests:

php bin/phpunit vendor/friendsofsymfony/message-bundle/Tests
<?php
// src/App/Entity/Message.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use FOS\MessageBundle\Entity\Message as BaseMessage;
/**
* @ORM\Entity
*/
class Message extends BaseMessage
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(
* targetEntity="App\Entity\Thread",
* inversedBy="messages"
* )
* @var \FOS\MessageBundle\Model\ThreadInterface
*/
protected $thread;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @var \FOS\MessageBundle\Model\ParticipantInterface
*/
protected $sender;
/**
* @ORM\OneToMany(
* targetEntity="App\Entity\MessageMetadata",
* mappedBy="message",
* cascade={"all"}
* )
* @var MessageMetadata[]|Collection
*/
protected $metadata;
}
<?php
// src/App/Entity/MessageMetadata.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\MessageBundle\Entity\MessageMetadata as BaseMessageMetadata;
/**
* @ORM\Entity
*/
class MessageMetadata extends BaseMessageMetadata
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(
* targetEntity="App\Entity\Message",
* inversedBy="metadata"
* )
* @var \FOS\MessageBundle\Model\MessageInterface
*/
protected $message;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @var \FOS\MessageBundle\Model\ParticipantInterface
*/
protected $participant;
}
<?php
// src/App/Entity/Thread.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use FOS\MessageBundle\Entity\Thread as BaseThread;
/**
* @ORM\Entity
*/
class Thread extends BaseThread
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @var \FOS\MessageBundle\Model\ParticipantInterface
*/
protected $createdBy;
/**
* @ORM\OneToMany(
* targetEntity="App\Entity\Message",
* mappedBy="thread"
* )
* @var Message[]|Collection
*/
protected $messages;
/**
* @ORM\OneToMany(
* targetEntity="App\Entity\ThreadMetadata",
* mappedBy="thread",
* cascade={"all"}
* )
* @var ThreadMetadata[]|Collection
*/
protected $metadata;
}
<?php
// src/App/Entity/ThreadMetadata.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\MessageBundle\Entity\ThreadMetadata as BaseThreadMetadata;
/**
* @ORM\Entity
*/
class ThreadMetadata extends BaseThreadMetadata
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(
* targetEntity="App\Entity\Thread",
* inversedBy="metadata"
* )
* @var \FOS\MessageBundle\Model\ThreadInterface
*/
protected $thread;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @var \FOS\MessageBundle\Model\ParticipantInterface
*/
protected $participant;
}
<?php
// src/App/Entity/User.php
namespace App\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use FOS\MessageBundle\Model\ParticipantInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser implements ParticipantInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment