<?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: Fitri</title>
    <description>The latest articles on Forem by Fitri (@fitri).</description>
    <link>https://forem.com/fitri</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%2F50136%2F42cdc540-1f8d-461d-8c07-f7b4e554f67f.png</url>
      <title>Forem: Fitri</title>
      <link>https://forem.com/fitri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fitri"/>
    <language>en</language>
    <item>
      <title>Case Sensitive WHERE Clause</title>
      <dc:creator>Fitri</dc:creator>
      <pubDate>Wed, 12 Sep 2018 03:05:53 +0000</pubDate>
      <link>https://forem.com/fitri/case-sensitive-where-clause-4860</link>
      <guid>https://forem.com/fitri/case-sensitive-where-clause-4860</guid>
      <description>&lt;p&gt;Today I found a bug in my URL shortener app;&lt;/p&gt;

&lt;p&gt;It didn't occur to me that the fields in a table aren't automatically case sensitive. This is a problem for my app because the auto-generated URLs uses base62 (A-Za-z0-9). "a1b2" will forward to the same link as "A1B1".&lt;/p&gt;

&lt;p&gt;I learned that this is the expected behaviour for Laravel's default database collation. "UTF8_unicode_ci" isn't case sensitive. To change that, I can use "UTF8_bin" for the whole database. But that introduces another peculiarity to my database when it comes to ORDER BY.&lt;/p&gt;

&lt;p&gt;So in this case, I prefer to keep the default charset/collation and just use "UTF8_bin" for one specific table.&lt;/p&gt;

&lt;p&gt;To fix this behaviour. I've added these two lines to my migration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$table-&amp;gt;charset = 'utf8';
$table-&amp;gt;collation = 'utf8_bin';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now everything works as intended.&lt;/p&gt;

&lt;p&gt;Alternatively, if you're using MySQL version 8+, you only need the following line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$table-&amp;gt;collation = 'utf8mb4_0900_as_cs'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>sql</category>
      <category>laravel</category>
      <category>mysql</category>
    </item>
    <item>
      <title>Learn Test Driven Development with Laravel in One Day</title>
      <dc:creator>Fitri</dc:creator>
      <pubDate>Sat, 01 Sep 2018 03:01:17 +0000</pubDate>
      <link>https://forem.com/fitri/learn-test-driven-development-with-laravel-in-one-day-2aji</link>
      <guid>https://forem.com/fitri/learn-test-driven-development-with-laravel-in-one-day-2aji</guid>
      <description>&lt;p&gt;I am building an URL shortener with extra features. Initially, I started building both front-end and back-end simultaneously. But as the scope grew too large for a side project, I started only building back-end as to not disturb the flow. That's when I started using Postman to send GET/POST requests.&lt;/p&gt;

&lt;p&gt;For a while, this was fine until I grew tired of having to test the same thing over and over again. There has to be a better way? Fortunately, there is and it couldn't be any easier with PHPUnit which comes pre-packaged with Laravel (You still need to install PHPUnit on your computer though).&lt;/p&gt;

&lt;p&gt;The Laravel Docs cover pretty much all you need to know. To start building your Test, you just need to run:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan make:test ThingToTest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This creates a file called ThingsToTest inside the &lt;code&gt;tests/features&lt;/code&gt; folder. My application requires some &lt;em&gt;seeds&lt;/em&gt; and I need to &lt;em&gt;refresh database&lt;/em&gt; before running the test so I added these to my test file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use RefreshDatabase;

public function setUp() : void
{
    parent::setUp();
    $this-&amp;gt;artisan('db:seed');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This basically tells my Test to refresh migrations and seed database before running the test cases. I'm using the default database here but you can have a separate database for your test environment.&lt;/p&gt;

&lt;p&gt;The generated test file already has an example in it so you can go ahead and type &lt;code&gt;phpunit&lt;/code&gt; on your command line to start the Test. We haven't added any custom test so it should pass.&lt;/p&gt;

&lt;p&gt;Now we can start making our own test cases/method. Here's an example of how I test if my URL has been shortened:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function testCreateValidLink()
{
    // Perform Action
    $response = $this-&amp;gt;json('POST', '/create/link', [
        'url' =&amp;gt; 'https://dev.to/fitri',
        'name' =&amp;gt; 'fitri'
    ]);

    // Test Result
    $response-&amp;gt;assertJson(['created' =&amp;gt; true]);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;/create/link&lt;/code&gt; routes to my link controller Create method which returns a JSON object. In my case, the method returns an array with 'created' value set to either &lt;em&gt;true&lt;/em&gt; or &lt;em&gt;false&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I understand that I can use Faker to generate my &lt;em&gt;URL&lt;/em&gt; and &lt;em&gt;name&lt;/em&gt;. But my controller validates the URL and make sure it returns a 200/301/302 http_code. So I have to use a real URL in this case.&lt;/p&gt;

&lt;p&gt;Each method is the test class/file represents a Test. Each method prefixed with 'assert' are assertions which returns either a pass or an error when you run &lt;code&gt;phpunit&lt;/code&gt;. There are many kind of assert methods. These are the ones I've been using so far:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;$this-&amp;gt;assertJson(['key' =&amp;gt; 'value']);&lt;/li&gt;
&lt;li&gt;$this-&amp;gt;assertDatabaseHas('table', ['field' =&amp;gt; 'value']);&lt;/li&gt;
&lt;li&gt;$this-&amp;gt;assertRedirect('url');&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There's more listed in the official doc:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://laravel.com/docs/5.6/http-tests#available-assertions"&gt;https://laravel.com/docs/5.6/http-tests#available-assertions&lt;/a&gt;&lt;br&gt;
&lt;a href="https://laravel.com/docs/5.6/database-testing#available-assertions"&gt;https://laravel.com/docs/5.6/database-testing#available-assertions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I've just learned everything last night but here are some useful tips I gathered from my trial and errors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test file must end with Test (somethingTest.php). Otherwise, it won't be run by phpunit.&lt;/li&gt;
&lt;li&gt;Test case (methods) must start with test (testSomething()) or it will be skipped.&lt;/li&gt;
&lt;li&gt;alternatively you can add the comment /** &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;
 */ before your method and name your method anything.&lt;/li&gt;
&lt;li&gt;Errors spewed from your tests are friends.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, building a few tests aren't exactly TDD. But because I'm relying on Test classes instead of a front-end app. I have to either build my test or controller/methods first. Either way, they are not mutually exclusive.&lt;/p&gt;

&lt;p&gt;Good luck!&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>testing</category>
      <category>tdd</category>
    </item>
  </channel>
</rss>
