<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: farhan</title>
    <description>The latest articles on Forem by farhan (@farhanrosenborg).</description>
    <link>https://forem.com/farhanrosenborg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F515793%2Fc5a58a6b-9211-4e42-86b5-353ee4bfecaf.png</url>
      <title>Forem: farhan</title>
      <link>https://forem.com/farhanrosenborg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/farhanrosenborg"/>
    <language>en</language>
    <item>
      <title>Unit tests dos and don'ts</title>
      <dc:creator>farhan</dc:creator>
      <pubDate>Sat, 05 Dec 2020 00:04:09 +0000</pubDate>
      <link>https://forem.com/farhanrosenborg/unit-tests-dos-and-don-ts-1038</link>
      <guid>https://forem.com/farhanrosenborg/unit-tests-dos-and-don-ts-1038</guid>
      <description>&lt;p&gt;When writing a unit tests. There are couple of things to do and couple of things which should be avoided. In this article I have listed the dos and don'ts with regards to unit testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  dos
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Test only the public functions.&lt;/li&gt;
&lt;li&gt;Test each and every public function. In a ideal world a class should have only one public function and its dependencies.&lt;/li&gt;
&lt;li&gt;Mock every dependency.&lt;/li&gt;
&lt;li&gt;Test should cover success and failure, both aspects of a code.&lt;/li&gt;
&lt;li&gt;Test that every raised exception is tested.&lt;/li&gt;
&lt;li&gt;Always specify how many times a function is expected to be called while testing a piece of code.&lt;/li&gt;
&lt;li&gt;Test setup pain is a smell, which means your code needs to be refactored.&lt;/li&gt;
&lt;li&gt;Code coverage should be 100%, which means each and every line of the tested class was reached.&lt;/li&gt;
&lt;li&gt;Live deployment should not work with failing unit tests.&lt;/li&gt;
&lt;li&gt;Add tests to classes which are missing unit tests.&lt;/li&gt;
&lt;li&gt;Write meaningful names which explain what the test is doing example names testUserRegistrationWasSuccessful, testUserRegistrationWasNotSuccessful, testUserRegistrationRepositoryThrowsException&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  don'ts
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Tested class should never talk to database, file, network resources etc.&lt;/li&gt;
&lt;li&gt;Dependencies in tested class should not create real class objects.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="http://www.rosenborgsolutions.com/articles/phpunit/phpunit-best-practice"&gt;http://www.rosenborgsolutions.com/articles/phpunit/phpunit-best-practice&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>testing</category>
    </item>
    <item>
      <title>PHPUnit mock function with different values</title>
      <dc:creator>farhan</dc:creator>
      <pubDate>Sat, 21 Nov 2020 23:27:51 +0000</pubDate>
      <link>https://forem.com/farhanrosenborg/phpunit-mock-function-with-different-values-3h47</link>
      <guid>https://forem.com/farhanrosenborg/phpunit-mock-function-with-different-values-3h47</guid>
      <description>&lt;p&gt;When writing unit test, we sometimes mock a function with different&lt;br&gt;
parameter values and expect different return value(s). For example in the popular e-commerce system magento, it's product model class has a getData function. Using this function you can get different attribute(s) of a product.&lt;/p&gt;

&lt;h5&gt;
  
  
  Function signature
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getData($key = '', $index = null)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We wish to get the product color and product size attributes. The code $product-&amp;gt;getData('color') should return value "red" and the code $product-&amp;gt;getData('size') should return "s". The second parameter $index is null, you can change it to any other value depending on your requirement.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example mock code
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$product = $this-&amp;gt;createMock(Product::class);
$product-&amp;gt;method('getData')-&amp;gt;willReturnMap([
       ['color', null, 'red'],
       ['size', null, 's'],
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Learning objectives&lt;/strong&gt;&lt;br&gt;
Test your php classes&lt;br&gt;
Continuous integration&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
Experience with the basics of variables, data types, and running php scripts&lt;br&gt;
Experience setting up PHPUnit, ideally with Composer&lt;/p&gt;

&lt;p&gt;Article originally published on &lt;a href="http://www.rosenborgsolutions.com/articles/phpunit/phpunit-mock-same-function-with-different-parameters"&gt;http://www.rosenborgsolutions.com/articles/phpunit/phpunit-mock-same-function-with-different-parameters&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>testing</category>
      <category>beginners</category>
    </item>
    <item>
      <title>PHPUnit - How to unit test a method with no return value?</title>
      <dc:creator>farhan</dc:creator>
      <pubDate>Mon, 16 Nov 2020 23:09:50 +0000</pubDate>
      <link>https://forem.com/farhanrosenborg/phpunit-how-to-unit-test-a-method-with-no-return-value-42fc</link>
      <guid>https://forem.com/farhanrosenborg/phpunit-how-to-unit-test-a-method-with-no-return-value-42fc</guid>
      <description>&lt;p&gt;When writing unit test for one public function in my class. I was a bit confused about how to test a function which has no return value(s). Normally unit tests validate return of boolean value, arrays or some object. There are parts of code which test of Exceptions being thrown from a function. In this scenario I was not throwing any exception or returning a value. What I wanted to test though if certain functionality was executed or not executed when function was being called.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example code of a function returning void&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UpdateShipmentTracking
{
    private $tracking;

    /**
     * UpdateShipmentTracking constructor.
     * @param Tracking $tracking
     */
    public function __construct(
        Tracking $tracking
    ) {
        $this-&amp;gt;tracking = $tracking;
    }

    /**
     * @param array $orders
     */
    public function send(array $orders): void
    {
        /** @var Order $order */
        foreach ($orders as $order) {
            $this-&amp;gt;tracking-&amp;gt;track($order-&amp;gt;getRealOrderId());
        }
    }
}

// @note this class is being mocked in UpdateShipmentTrackingTest
class Tracking
{
    /**
     * @param string $orderID
     */
    public function track(string $orderID): void
    {
        // add shipment tracking number to order
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Class UpdateShipmentTracking has the following things&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It has a constructor injection of class Tracking.&lt;/li&gt;
&lt;li&gt;It has a function called send which does not return anything. If array is not empty it updates the shipment tracking number.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How can we test a function like this, to be sure that it is functioning properly? If we observe the code properly there are 2 possibilities that can happen when the send function will be called.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Possibility 1&lt;/strong&gt;&lt;br&gt;
If we pass empty array to the function. Nothing will be executed. The track function of class Tracking will never be called.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Possibility 2&lt;/strong&gt;&lt;br&gt;
If we pass 1 element in the array. The track function of class Tracking will be called 1 time.&lt;/p&gt;

&lt;p&gt;Now that we can see the possibilities. We can test the 2 scenarios. By doing so we cover that the function returning void is properly tested. If you do not believe my statement the screenshot of code coverage will prove that this is indeed true.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UpdateShipmentTrackingTest extends TestCase
{
    /** @var Tracking */
    private $tracking;

    protected function setUp(): void
    {
        $this-&amp;gt;tracking = $this-&amp;gt;createMock(Tracking::class);
    }

    public function testEmptyOrdersArrayIsPassedNothingHappens(): void
    {
        // @note we do not expect function track to be called
        $this-&amp;gt;tracking-&amp;gt;expects($this-&amp;gt;never())
            -&amp;gt;method('track');

        $object = $this-&amp;gt;getUpdateShipmentTracking();
        $object-&amp;gt;send([]);
    }

    public function testShipmentTrackingNumberWasAdded(): void
    {
        // @note we expect track to be called 1 time, since we pass 1 order
        $this-&amp;gt;tracking-&amp;gt;expects($this-&amp;gt;exactly(1))
            -&amp;gt;method('track');

        $order = $this-&amp;gt;createMock(Order::class);

        $order-&amp;gt;expects($this-&amp;gt;exactly(1))
            -&amp;gt;method('getRealOrderId')
            -&amp;gt;willReturn('100001');

        $object = $this-&amp;gt;getUpdateShipmentTracking();
        $object-&amp;gt;send([$order]);
    }

    /**
     * @return UpdateShipmentTracking
     */
    private function getUpdateShipmentTracking(): UpdateShipmentTracking
    {
        $objectManager = new ObjectManager($this);

        return $objectManager-&amp;gt;getObject(UpdateShipmentTracking::class,
            [
                'tracking' =&amp;gt; $this-&amp;gt;tracking,
            ]);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Article original published on: &lt;a href="http://www.rosenborgsolutions.com/articles/phpunit/phpunit-testing-method-return-void"&gt;http://www.rosenborgsolutions.com/articles/phpunit/phpunit-testing-method-return-void&lt;/a&gt;&lt;/p&gt;

</description>
      <category>unittesting</category>
      <category>phpunit</category>
      <category>php</category>
      <category>codecoverage</category>
    </item>
  </channel>
</rss>
