<?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: Omar Malas</title>
    <description>The latest articles on Forem by Omar Malas (@omarmalas).</description>
    <link>https://forem.com/omarmalas</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%2F1144862%2F423b36ea-e44b-416f-b894-521e92ab260f.png</url>
      <title>Forem: Omar Malas</title>
      <link>https://forem.com/omarmalas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/omarmalas"/>
    <language>en</language>
    <item>
      <title>Laravel Reverb: make a chatbox component for your site!</title>
      <dc:creator>Omar Malas</dc:creator>
      <pubDate>Wed, 03 Apr 2024 22:31:25 +0000</pubDate>
      <link>https://forem.com/omarmalas/laravel-reverb-make-a-chatbox-component-for-your-site-26n3</link>
      <guid>https://forem.com/omarmalas/laravel-reverb-make-a-chatbox-component-for-your-site-26n3</guid>
      <description>&lt;p&gt;Laravel 11 was released a few weeks ago and with it is laravel reverb, a first-party websocket package to help with real-time applications.&lt;br&gt;
In this article I'll demonstrate how to make a simple chatbox component for support-related purposes...&lt;/p&gt;

&lt;p&gt;first off we'll create a new laravel application using laravel new&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;laravel new chatbox
cd chatbox
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then after all installation is complete we can use the new command to install laravel reverb&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan install:broadcasting
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;make sure the .env file has this line set to sync&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;QUEUE_CONNECTION=sync
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;next all we have to do is to create an event and listen on its channel&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:event MessageSent
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and make it look something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class MessageSent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     */
    public function __construct( public string $name, public string $text)
    {
        //
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array&amp;lt;int, \Illuminate\Broadcasting\Channel&amp;gt;
     */
    public function broadcastOn(): array
    {
        return [
            new Channel('messages'),
        ];
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;next we create the livewire component to listen on the channel&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:volt ChatBox --class
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and make it like so...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
use App\Events\MessageSent;
use Livewire\Volt\Component;
new class extends Component
{
    /**
     * @var string[]
     */
    public array $messages = [];
    public string $message = '';
    protected $listeners = ['echo:messages,MessageSent' =&amp;gt; 'onMessageSent'];
    public function addMessage()
    {
        MessageSent::dispatch(auth()-&amp;gt;user()-&amp;gt;name, $this-&amp;gt;message);
        $this-&amp;gt;reset('message');
    }
    #[On('echo:messages,MessageSent')]
    public function onMessageSent($event)
    {
        $this-&amp;gt;messages[] = $event;
    }
}
?&amp;gt;

&amp;lt;div x-data="{ open: true }" &amp;gt;
    &amp;lt;div :class="{'-translate-y-0': open, 'translate-y-full': !open}" class="fixed transition-all duration-300 transform bottom-10 right-12 h-60 w-80"&amp;gt;
        &amp;lt;div class="mb-2"&amp;gt;
            &amp;lt;button @click="open = !open" type="button" :class="{ 'text-indigo-600 dark:text-white hover:bg-transparent': open }" class="w-full text-start flex items-center gap-x-3.5 py-2 px-2.5 text-sm text-white rounded-lg hover:bg-indigo-400 dark:bg-indigo-600 dark:hover:bg-indigo-400"&amp;gt;
                Chat

                &amp;lt;x-heroicon-o-chevron-up x-show="!open" x-cloak class="ms-auto block size-4" /&amp;gt;
                &amp;lt;x-heroicon-o-chevron-down x-show="open" x-cloak class="ms-auto block size-4" /&amp;gt;

            &amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div class="w-full h-full bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-700 rounded overflow-auto flex flex-col px-2 py-4"&amp;gt;
            &amp;lt;div x-ref="chatBox" class="flex-1 p-4 text-sm flex flex-col gap-y-1"&amp;gt;
                @foreach($messages as $message)
                    &amp;lt;div&amp;gt;&amp;lt;span class="text-indigo-600"&amp;gt;{{ $message['name'] }}:&amp;lt;/span&amp;gt; &amp;lt;span class="dark:text-white"&amp;gt;{{ $message['text'] }}&amp;lt;/span&amp;gt;&amp;lt;/div&amp;gt;
                @endforeach
            &amp;lt;/div&amp;gt;
            &amp;lt;div&amp;gt;
                &amp;lt;form wire:submit.prevent="addMessage" class="flex gap-2"&amp;gt;
                    &amp;lt;x-text-input wire:model="message" x-ref="messageInput" name="message" id="message" class="block w-full" /&amp;gt;
                    &amp;lt;x-primary-button&amp;gt;
                        Send
                    &amp;lt;/x-primary-button&amp;gt;
                &amp;lt;/form&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;next we add it anywhere to the welcome page of the default laravel, and we need to start the reverb server...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan serve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and on another terminal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan reverb:start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and we are all set!&lt;br&gt;
the chatbox should be working and all should be complete&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
    </item>
    <item>
      <title>Testing in Laravel: Types and Setup</title>
      <dc:creator>Omar Malas</dc:creator>
      <pubDate>Wed, 23 Aug 2023 20:06:12 +0000</pubDate>
      <link>https://forem.com/omarmalas/testing-in-laravel-types-and-setup-2gp</link>
      <guid>https://forem.com/omarmalas/testing-in-laravel-types-and-setup-2gp</guid>
      <description>&lt;p&gt;For Laravel Developers Testing is an essential part of development, it can ensure your function is doing what it’s supposed to.&lt;br&gt;
To ensure that you need to have a robust testing suite in your Laravel applications, your testing techniques may differ from your co-worker, but the goal is always the same.&lt;/p&gt;

&lt;p&gt;Testing can be difficult when you don’t have a UI to work with, as lots of errors appear later when bootstrapping with your frontend team.&lt;/p&gt;

&lt;p&gt;Without further ado, here’s the top kinds of Testing in Laravel:&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;1. Unit Tests&lt;/strong&gt;&lt;br&gt;
Unit tests focus on isolating and testing individual components or units of your code in isolation. These components can be functions, methods, or classes. Unit tests ensure that each piece of code behaves as expected and produces the correct output for a given input.&lt;/p&gt;

&lt;p&gt;When to Use Unit Tests:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Testing individual methods or functions.&lt;/li&gt;
&lt;li&gt;Validating core business logic.&lt;/li&gt;
&lt;li&gt;Ensuring that edge cases and error handling are correctly implemented.
For example:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function testUserCanChangePassword()
{
    $user = User::factory()-&amp;gt;create();

    $user-&amp;gt;changePassword('newpassword');

    $this-&amp;gt;assertTrue(Hash::check('newpassword', $user-&amp;gt;password));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;2. Feature Tests&lt;/strong&gt;&lt;br&gt;
Feature tests take a broader approach by testing the interaction between various components of your application. These tests simulate a user’s interaction with the application, such as visiting a webpage and performing actions like form submissions.&lt;/p&gt;

&lt;p&gt;When to Use Feature Tests:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Testing user interactions with your application.&lt;/li&gt;
&lt;li&gt;Validating entire user flows and user interface behavior.&lt;/li&gt;
&lt;li&gt;Ensuring that routes, controllers, and views work harmoniously.
For example:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function testUserCanLogin()
{
    $user = User::factory()-&amp;gt;create();

    $response = $this-&amp;gt;post('/login', [
        'email' =&amp;gt; $user-&amp;gt;email,
        'password' =&amp;gt; 'password',
    ]);

    $response-&amp;gt;assertRedirect('/dashboard');
    $this-&amp;gt;assertAuthenticatedAs($user);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;3. Integration Tests&lt;/strong&gt;&lt;br&gt;
Integration tests examine how different parts of your application interact with each other. These tests help ensure that components integrate correctly and work seamlessly as a cohesive unit.&lt;br&gt;
When to Use Integration Tests:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Testing interactions between different layers (e.g., database, external APIs).&lt;/li&gt;
&lt;li&gt;Validating database operations and data persistence.&lt;/li&gt;
&lt;li&gt;Ensuring proper communication between controllers, models, and views.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function testUserCanCreatePost()
{
    $user = User::factory()-&amp;gt;create();

    $response = $this-&amp;gt;actingAs($user)
                     -&amp;gt;post('/posts', ['title' =&amp;gt; 'New Post']);

    $response-&amp;gt;assertStatus(201);
    $this-&amp;gt;assertDatabaseHas('posts', ['title' =&amp;gt; 'New Post']);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;These three types of tests form the foundation of Laravel’s testing suite. By employing a combination of unit, feature, and integration tests, you can thoroughly assess your application’s functionality, codebase, and user interactions. A comprehensive testing strategy contributes to code quality, reduces bugs, and enables smoother development and maintenance.&lt;/p&gt;

&lt;p&gt;In the next sections, we’ll delve into setting up your testing environment.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Setting Up Testing Environment&lt;/strong&gt;&lt;br&gt;
Before you embark on your journey of writing tests for your Laravel applications, it’s essential to set up a proper testing environment. Laravel makes this process straightforward, providing tools and configurations that facilitate effective testing. Here’s how to get started:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Configure Testing Environment&lt;/strong&gt;&lt;br&gt;
Laravel comes with predefined environment configurations for testing. In your .env file, ensure the APP_ENV is set to testing when running tests. This helps Laravel automatically use the testing configuration settings.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;APP_ENV=testing&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Artisan Commands&lt;/strong&gt;&lt;br&gt;
Laravel’s Artisan command-line tool provides essential commands to assist you in testing:&lt;/p&gt;

&lt;p&gt;To run all tests in the tests directory:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan test&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;To run tests in a specific file or directory:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan test path/to/YourTest.php&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Database Configuration&lt;/strong&gt;&lt;br&gt;
Laravel’s testing environment uses an in-memory SQLite database by default, allowing tests to be isolated and perform operations without affecting your development or production databases. You can modify this configuration in &lt;code&gt;config/database.php&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'testing' =&amp;gt; [
    'driver' =&amp;gt; 'sqlite',
    'database' =&amp;gt; ':memory:',
    // ...
],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Factories and Seeders&lt;/strong&gt;&lt;br&gt;
Factories and seeders are essential for generating test data. Laravel’s factories help you create test models with predefined attributes. Use the database/factories directory to define your factories and the database/seeders directory to set up your database seeders.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. PHPUnit Configuration&lt;/strong&gt;&lt;br&gt;
Customize PHPUnit configurations in the phpunit.xml file in your project root. You can set up code coverage reports, specify test directories, and configure various testing-related options.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Testing Traits&lt;/strong&gt;&lt;br&gt;
Laravel provides testing traits to assist you in writing tests with common functionality. For example, the RefreshDatabase trait automatically resets the database after each test, ensuring a clean slate for every test case.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;use Illuminate\Foundation\Testing\RefreshDatabase;&lt;/code&gt;&lt;br&gt;
By properly configuring your testing environment, you create a solid foundation for writing tests that provide accurate results and simulate real-world scenarios. In the next sections, we’ll dive into writing unit tests, feature tests, and integration tests to demonstrate how Laravel’s testing framework can be used effectively.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
In the world of software development, testing is not just a process; it’s a practice that can significantly elevate the quality and reliability of your Laravel applications. The Laravel framework, equipped with the powerful PHPUnit testing framework, empowers developers to create tests that validate everything from individual components to entire user flows. By embracing a comprehensive testing strategy, you’re investing in the long-term success of your projects.&lt;/p&gt;

&lt;p&gt;We explored the fundamental types of tests that Laravel offers: unit tests, feature tests, and integration tests. Each type serves a specific purpose in ensuring your application’s functionality, user interactions, and component integration are thoroughly assessed. Remember, testing isn’t just about catching bugs; it’s about crafting code that’s resilient, maintainable, and adaptable.&lt;/p&gt;

&lt;p&gt;We also walked through the process of setting up your testing environment. Properly configuring your environment, leveraging Laravel’s testing traits, and utilizing testing tools like factories and seeders are crucial steps to ensure accurate and efficient testing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy Coding! :)&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>testing</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
