Skip to content

Commit

Permalink
Merge pull request #157 from loic425/docs/design-your-entity-with-php…
Browse files Browse the repository at this point in the history
…spec

[Docs] Specify your entities with phpspec
  • Loading branch information
loic425 committed Mar 18, 2019
2 parents 92bfa83 + 0246201 commit 1bb9328
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/cookbook/bdd/how-to-configure-phpspec-with-code-coverage.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
How to configure phpspec with code coverage
===========================================

By default, phpspec on Monofony is configured with code coverage which needs xdebug or phpdbg installed.
Thus you have two options:
* install xdebug
* install phpdbg (easier and faster)


.. note::

But if you don't need that feature, :doc:`disable code coverage </cookbook/bdd/how-to-disable-phpspec-code-coverage>`.


Install phpdbg
--------------

.. code-block:: bash
$ # on linux
$ sudo apt-get install php7.2-phpdbg
$
$ # on max
$ brew install php72-phpdbg
202 changes: 202 additions & 0 deletions docs/cookbook/bdd/how-to-design-entities-with-phpspec.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
How to design entities with phpspec
===================================

Lets configure an Article entity with a title and an author.
Title is a simple string and author implements CustomerInterface.

.. warning::

By default, phpspec on Monofony is configured with code coverage.
:doc:`Learn how to configure phpspec with code coverage </cookbook/bdd/how-to-configure-phpspec-with-code-coverage>` or :doc:`disable code coverage </cookbook/bdd/how-to-disable-phpspec-code-coverage>`.

Generate phpspec for your entity
--------------------------------

.. code-block:: bash
$ vendor/bin/phpspec describe App/Entity/Article
$ # with phpdbg installed
$ phpdbg -qrr vendor/bin/phpspec describe App/Entity/Article
.. code-block:: php
# spec/src/App/Entity/Article.php
namespace spec\App\Entity;
use App\Entity\Article;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class ArticleSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(Article::class);
}
}
Run phpspec and do not fear Red
-------------------------------

To run phpspec for our Article entity, run this command:

.. code-block:: bash
$ vendor/bin/phpspec run spec/App/Entity/ArticleSpec.php -n
$
$ # with phpdbg installed
$ phpdbg -qrr vendor/bin/phpspec run spec/App/Entity/ArticleSpec.php -n
And be happy with your first error message with red color.

.. note::

You can simply run all the phpspec tests by running `vendor/bin/phpspec run -n`


Create a minimal Article class
------------------------------

.. code-block:: php
# src/App/Entity/Article.php
namespace App\Entity;
class Article
{
}
Rerun phpspec and see a beautiful green color.

Specify it implements sylius resource interface
-----------------------------------------------

.. code-block:: php
function it_implements_sylius_resource_interface(): void
{
$this->shouldImplement(ResourceInterface::class);
}
.. warning::

And Rerun phpspec, DO NOT FEAR RED COLOR!
It's important to check that you write code which solves your specifications.

Solve this on your entity
-------------------------

.. code-block:: php
# src/App/Entity/Article.php
namespace App\Entity;
use Sylius\Component\Resource\Model\ResourceInterface;
class Article implements ResourceInterface
{
use IdentifiableTrait;
}
.. warning::

Rerun phpspec again and check this specification is solved.

Specify it has a title
----------------------

.. code-block:: php
function its_title_is_mutable(): void
{
$this->setTitle('This documentation is so great');
$this->getTitle()->shouldReturn('This documentation is so great');
}
.. warning::

Don't forget to rerun phpspec on each step.

Add title on Article entity
---------------------------

.. code-block:: php
# src/App/Entity/Article.php
/**
* @var string|null
*/
private $title;
/**
* @return string|null
*/
public function getTitle(): ?string
{
return $this->title;
}
/**
* @param string|null $title
*/
public function setTitle(?string $title): void
{
$this->title = $title;
}
Specify author of the article
-----------------------------

.. code-block:: php
# spec/src/App/Entity/Article.php
use Sylius\Component\Customer\Model\CustomerInterface;
// [...]
function its_author_is_mutable(CustomerInterface $author): void
{
$this->setAuthor($author);
$this->getAuthor()->shouldReturn($author);
}
Add author on your entity
-------------------------

.. code-block:: php
# src/App/Entity/Article.php
// [...]
/**
* @var CustomerInterface|null
*/
private $author;
// [...]
/**
* @return CustomerInterface|null
*/
public function getAuthor(): ?CustomerInterface
{
return $this->author;
}
/**
* @param CustomerInterface|null $author
*/
public function setAuthor(?CustomerInterface $author): void
{
$this->author = $author;
}
That's all to design your first entity!
13 changes: 13 additions & 0 deletions docs/cookbook/bdd/how-to-disable-phpspec-code-coverage.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
How to disable phpspec code coverage
====================================

.. code-block:: bash
$ cp phpspec.yml.dist phpspec
And just comment the content

.. code-block:: yaml
# extensions:
# LeanPHP\PhpSpec\CodeCoverage\CodeCoverageExtension: ~
3 changes: 3 additions & 0 deletions docs/cookbook/bdd/map.rst.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* :doc:`/cookbook/bdd/how-to-configure-phpspec-with-code-coverage`
* :doc:`/cookbook/bdd/how-to-disable-phpspec-code-coverage`
* :doc:`/cookbook/bdd/how-to-design-entities-with-phpspec`
12 changes: 12 additions & 0 deletions docs/cookbook/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ Fixtures

.. include:: /cookbook/fixtures/map.rst.inc

BDD
---

.. toctree::
:hidden:

bdd/how-to-configure-phpspec-with-code-coverage
bdd/how-to-disable-phpspec-code-coverage
bdd/how-to-design-entities-with-phpspec

.. include:: /cookbook/bdd/map.rst.inc


Deployment
----------
Expand Down

0 comments on commit 1bb9328

Please sign in to comment.