<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Angelos Giannis on Medium]]></title>
        <description><![CDATA[Stories by Angelos Giannis on Medium]]></description>
        <link>https://medium.com/@angelos.giannis?source=rss-73c490576327------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*vpEX99_JMU3diCH0.</url>
            <title>Stories by Angelos Giannis on Medium</title>
            <link>https://medium.com/@angelos.giannis?source=rss-73c490576327------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 11:06:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@angelos.giannis/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Auto generate builder for an object]]></title>
            <link>https://medium.com/@angelos.giannis/auto-generate-builder-for-an-object-f4b2c93b5d96?source=rss-73c490576327------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4b2c93b5d96</guid>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[builder-pattern]]></category>
            <category><![CDATA[phpstorm]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[bash-script]]></category>
            <dc:creator><![CDATA[Angelos Giannis]]></dc:creator>
            <pubDate>Fri, 03 Jul 2020 19:07:22 GMT</pubDate>
            <atom:updated>2020-09-04T10:54:40.058Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eoaXQSd-itbSYxsqdgnl1w.jpeg" /><figcaption>What’s hidden under the rock?</figcaption></figure><p>How about creating a builder for a specific class? It is something pretty straight forward, and is kind of a “boring” task as we simply have to create the same function (differentiating only on the field that each function is responsible of updating) multiple times. Why not automating this process and get the builder instantly?</p><h3>What is a builder of a class?</h3><p>Some details about what we actually describe with the term <strong>builder</strong>. As builder, we define a class that allows us to create an instance of a desired class without us accessing directly the inner properties of an object.</p><p>Let’s see this with an example. Assuming that we have the following class :</p><p><a href="https://gist.github.com/10b21950968a22a0859c1ff6527d71cf.git">https://gist.github.com/eujoy/10b21950968a22a0859c1ff6527d71cf</a></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec93ba043b260fc324bd54b631dc3fa3/href">https://medium.com/media/ec93ba043b260fc324bd54b631dc3fa3/href</a></iframe><p>A builder of this class is another class that creates a user instance and includes functions which assign a provided value to the respective object element. This allows the rest of the code to rely on builder class as abstraction, instead of the object definition itself. Here is an example function :</p><p><a href="https://gist.github.com/eujoy/3dff61e7b9f73beac3270c407dd15ece">https://gist.github.com/eujoy/3dff61e7b9f73beac3270c407dd15ece</a></p><h3>Motivation for automation</h3><p>As described above, the builder is a repetition of similar functions.</p><blockquote>Why not automating the generation of such a class?</blockquote><p>This was the question that came into mind when working on a task that required the creation of several builders, to be used for unit tests as fixtures. So, what I decided, instead of simply spending the time to create all those builders required, to create a shell script which will do this process for me, which will also be reusable in the future for performing such actions.</p><h3>What’s the idea?</h3><p>The idea for this has raised as a result of 2 questions :</p><ul><li>What is the input source?</li><li>What we want to achieve?</li></ul><p>These questions include the whole logic behind the automation. Getting a class definition as an input, we simply want to extract the variables of that class (input source) and based on them we want to prepare a builder that provides a collection of functions that create such an instance of the source and assign each value to the respective element of the object respectively.</p><p>That’s actually it! Why writing all that boilerplate code by hand while we can automate it and make our life easier?</p><h3>The flow of the script</h3><p>The creation of the builder class is actually achieved by following the steps as they are defined in the following list :</p><ul><li>Parse the input class.</li><li>Retrieve the namespace as well as the class name of the input.</li><li>Retrieve the public elements of that input class. Also, retrieve the type of each one of the elements (in case it is defined).</li><li>Create the <strong><em>withSomething</em></strong> functions for each one of the public variables.</li><li>Compose all the above in an output class and create the respective file in the desired location provided.</li></ul><p>That’s it! Though, there are some pieces that may need some clarification, such as the fact that the script expects that the variable names will be snake case and they get converted to camel case to be used in the <strong><em>withSomething</em></strong> functions. In case they are not snake case, the flow will continue normally, but the names of the functions will use the variable name as it was extracted, but after replacing the snake case parts in camel case.</p><p>There is also the option to create a function that allows the caller to create an array of instances of the base class, by simply providing an array including arrays that each one describes the values of each respective field of the object. This is an optional feature which is controlled by a flag when running the script.</p><blockquote>I found it really useful to mass create with providing an array, when there was the need to create a bunch of data for some test scenarios.</blockquote><p>Apart from the above mentioned optional flag, when running the script, there is also a bunch of other optional arguments, which allow the user to add extra constructor definition, set the namespace for the builder class, provide the dependencies of the builder class as well as add some private variables in the definition of the builder class.</p><h3>Time for code</h3><p>Finally, time for some code! The script that is described in this article can be found in :</p><blockquote><a href="https://github.com/Angelos-Giannis/php-builder-class">https://github.com/Angelos-Giannis/php-builder-class</a></blockquote><p>Feel free to enjoy it!!</p><h3>Example of auto-generated builder</h3><p>As far as an example of the execution, it can be found in the following gist :</p><p><a href="https://gist.github.com/eujoy/eb7c9c4e17c9fb79585b47f00c19ee17">https://gist.github.com/eujoy/eb7c9c4e17c9fb79585b47f00c19ee17</a></p><p>To produce this builder, the respective user class mentioned earlier has been used by simply running the following command :</p><pre><em>./createBuilderForClass.sh -b . -f User.php -r . -a</em></pre><h3>Extra: PHP Storm Integration</h3><p>Once we have something like this, why not adding it as a tool in PHP Storm so that we can use it in there as well with only two clicks? PHP Storm allows us to add scripts like this as an extension and we will do so by following some simple steps in order to add this tool in there.</p><p>The script itself provides the list of steps to follow to do so by simply running :</p><pre>./createBuilderForClass.sh -s</pre><p>Follow the steps… and voila… it is ready to be using in PHP Storm!!</p><h3>Conclusion</h3><p>Summarizing, automating such kind of tasks/actions allows us to focus on more interesting stuff without having to spend time on boilerplate code. But, before reaching the point of creating this script, I did the process manually several times in order to get familiar with the whole logic and have full understanding on what is actually required.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4b2c93b5d96" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Table Driven Tests in Go - Logic differentiation with functions]]></title>
            <link>https://medium.com/@angelos.giannis/table-driven-tests-in-go-logic-differentiation-with-functions-1b2fc578fc18?source=rss-73c490576327------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b2fc578fc18</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[unittest]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Angelos Giannis]]></dc:creator>
            <pubDate>Wed, 10 Jun 2020 12:41:31 GMT</pubDate>
            <atom:updated>2020-09-04T10:46:48.572Z</atom:updated>
            <content:encoded><![CDATA[<h3>First Things First.</h3><p>Testing our code is essential! Writing tests is part of our implementation and we want them to be of such quality as our code is - or (why not?) even better.</p><h3>What are table-driven tests?</h3><p>Writing unit, integration or any type of tests includes a lot of steps for :</p><ol><li>Setting up the required details.</li><li>Execute the actual test flow.</li><li>Cleanup after execution.</li></ol><p>All the above usually include duplication (or maybe more) of code across several test cases. As an example, just imagine that you need to test a component in your code where you have some dependencies and also a database layer as well. The actual flow of a simple test case could be something of the form :</p><ol><li>Initialize the database layer.</li><li>Initialize the dependencies of your component.</li><li>Initialize your component.</li><li>Call the component instance with some test data.</li><li>Assert that the actual returned values are the same as the expected.</li></ol><p>The above example includes a lot of duplication of code (which can be delegated to functions) and also requires all of the above steps to be executed again and again for each test.</p><p>Here come the table-driven tests!! They allow us to have a structure that describes the input and the expected output of each test scenario and allows us to get rid of all the duplication and setup/preparation from the test’s logic.</p><p>Here are some great articles providing more detailed information about the table-driven tests and how they can be structured and used in Go :</p><ol><li><a href="https://dave.cheney.net/2019/05/07/prefer-table-driven-tests">https://dave.cheney.net/2019/05/07/prefer-table-driven-tests</a></li><li><a href="https://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go">https://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go</a></li></ol><h3>Taking this one step forward…</h3><p>So, why not trying to get this one step forward and get some logic in the structure describing the test scenarios?</p><p>This is something that I found very useful when working on functions that can have a very large range of input and edge cases.</p><p>Here is the idea behind this differentiation :</p><ol><li>Setup of the test cases is the same.</li><li>Different dependencies setup is required for some cases (i.e. different mock responses are required).</li><li>Different assertions are required for some of the scenarios.</li><li>Keep the tests for a function under the same test function.</li></ol><p>So, here is a suggested structure for this kind of table-driven tests :</p><p><a href="https://gist.github.com/eujoy/723758a13971922fc9f40993679d85fb">https://gist.github.com/eujoy/723758a13971922fc9f40993679d85fb</a></p><h3>Conclusion.</h3><p>So, when and why can we use this? The basic idea behind this is to group all the tests for a specific unit under one test function by having a bit different setup and checks for each case. I would recommend using for a bit complex functions with dependencies to be mocked etc.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b2fc578fc18" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>