<commit_before>Object Construction
===================

In **phpspec** specs the object you are describing is not a separate variable
but is `$this`. So instead of writing something like:

.. code-block:: php

    <?php

    namespace spec;

    use PhpSpec\ObjectBehavior;

    class MarkdownSpec extends ObjectBehavior
    {
        function it_converts_plain_text_to_html_paragraphs()
        {
            $markdown = new Markdown();
            $markdown->toHtml("Hi, there")->shouldReturn("<p>Hi, there</p>");
        }
    }

as you might with other tools, you write:

.. code-block:: php

    <?php

    namespace spec;

    use PhpSpec\ObjectBehavior;

    class MarkdownSpec extends ObjectBehavior
    {
        function it_converts_plain_text_to_html_paragraphs()
        {
            $this->toHtml("Hi, there")->shouldReturn("<p>Hi, there</p>");
        }
    }

One thing this means is that you do not construct the object you are
describing in the examples. **phpspec** instead handles creating the
object you are describing when you run the specs.

The default way **phpspec** does this is the same as ``new Markdown()``.
If it does not need any values or dependencies passing to it then this is
fine but for many objects this will not be good enough. You can tell **phpspec**
how you want it to create the object though.

Using the Constructor
---------------------

You can tell phpspec to pass values to the constructor when it constructs the object:

<?php

    namespace spec;

    use PhpSpec\ObjectBehavior;
    use Markdown\Writer;

    class MarkdownSpec extends ObjectBehavior
    {
        function it_outputs_converted_text(Writer $writer)
        {
            $this->beConstructedWith($writer);
            $writer->writeText("<p>Hi, there</p>")->shouldBeCalled();

            $this->outputHtml("Hi, there");
        }
    }

Using a Factory Method
----------------------

You may not want to use the constructor but use static factory methods to create the class.
This allows you to create it in different ways for different use cases since you can
only have a single constructor in PHP.

<?php

    use Markdown\Writer;

    class Markdown
    {
        static public function createForWriting(Writer $writer)
        {
            $markdown = new Self();
            $markdown->writer = $writer;

            return $markdown;
        }
    }

You can tell *phpspec* this is how you want to construct the object as follows:

<?php

    namespace spec;

    use PhpSpec\ObjectBehavior;
    use Markdown\Writer;

    class MarkdownSpec extends ObjectBehavior
    {
        function it_outputs_converted_text(Writer $writer)
        {
            $this->beConstructedThrough('createForWriting', [$writer]);
            $writer->writeText("<p>Hi, there</p>")->shouldBeCalled();

            $this->outputHtml("Hi, there");
        }
    }

Where the first argument is the method name and the second an array of the values
to pass to that method.

Overriding
----------

To avoid repetition you can tell **phpspec** how to construct the object in `let`.
However, you may have a single example that needs constructing in a different way.
You can do this by calling ``beConstructedWith`` again in the example. The last time you
call ``beConstructedWith`` will determine how the **phpspec** constructs the object:


<?php

    namespace spec;

    use PhpSpec\ObjectBehavior;
    use Markdown\Writer;

    class MarkdownSpec extends ObjectBehavior
    {<commit_msg>Clean up a couple of inconsistencies in docs<commit_after>Object Construction
===================

In **phpspec** specs the object you are describing is not a separate variable
but is `$this`. So instead of writing something like:

.. code-block:: php

    <?php

    namespace spec;

    use PhpSpec\ObjectBehavior;

    class MarkdownSpec extends ObjectBehavior
    {
        function it_converts_plain_text_to_html_paragraphs()
        {
            $markdown = new Markdown();
            $markdown->toHtml("Hi, there")->shouldReturn("<p>Hi, there</p>");
        }
    }

as you might with other tools, you write:

.. code-block:: php

    <?php

    namespace spec;

    use PhpSpec\ObjectBehavior;

    class MarkdownSpec extends ObjectBehavior
    {
        function it_converts_plain_text_to_html_paragraphs()
        {
            $this->toHtml("Hi, there")->shouldReturn("<p>Hi, there</p>");
        }
    }

One thing this means is that you do not construct the object you are
describing in the examples. **phpspec** instead handles creating the
object you are describing when you run the specs.

The default way **phpspec** does this is the same as ``new Markdown()``.
If it does not need any values or dependencies passing to it then this is
fine but for many objects this will not be good enough. You can tell **phpspec**
how you want it to create the object though.

Using the Constructor
---------------------

You can tell phpspec to pass values to the constructor when it constructs the object:

<?php

    namespace spec;

    use PhpSpec\ObjectBehavior;
    use Markdown\Writer;

    class MarkdownSpec extends ObjectBehavior
    {
        function it_outputs_converted_text(Writer $writer)
        {
            $this->beConstructedWith($writer);
            $writer->writeText("<p>Hi, there</p>")->shouldBeCalled();

            $this->outputHtml("Hi, there");
        }
    }

Using a Factory Method
----------------------

You may not want to use the constructor but use static factory methods to create the class.
This allows you to create it in different ways for different use cases since you can
only have a single constructor in PHP.

<?php

    use Markdown\Writer;

    class Markdown
    {
        static public function createForWriting(Writer $writer)
        {
            $markdown = new Self();
            $markdown->writer = $writer;

            return $markdown;
        }
    }

You can tell **phpspec** this is how you want to construct the object as follows:

<?php

    namespace spec;

    use PhpSpec\ObjectBehavior;
    use Markdown\Writer;

    class MarkdownSpec extends ObjectBehavior
    {
        function it_outputs_converted_text(Writer $writer)
        {
            $this->beConstructedThrough('createForWriting', [$writer]);
            $writer->writeText("<p>Hi, there</p>")->shouldBeCalled();

            $this->outputHtml("Hi, there");
        }
    }

Where the first argument is the method name and the second an array of the values
to pass to that method.

Overriding
----------

To avoid repetition you can tell **phpspec** how to construct the object in `let`.
However, you may have a single example that needs constructing in a different way.
You can do this by calling ``beConstructedWith`` again in the example. The last time you
call ``beConstructedWith`` will determine how **phpspec** constructs the object:


<?php

    namespace spec;

    use PhpSpec\ObjectBehavior;
    use Markdown\Writer;

    class MarkdownSpec extends ObjectBehavior
    {
{'fraction_non_alphanumeric': 0.10041363571530453, 'fraction_numerical': 0.0, 'mean_word_length': 2.7477284874398715, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1053224', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1834, 'n_words': 744}