Powered by Blogger.
facebook twitter instagram pinterest bloglovin Email

Yasya Indra Blog

Éclairage et ingénierie

 

Kali ini saya akan merangkum sebuah pembahasan penting mengenai PHP Unit Test

Sebelum membahas PHP Uni Test secara spesifik. Saya akan menjelaskan apa itu unit test secara general

Unit Test merupakan sebuah langkah dimana kita memastikan kode kode yang kita mampu meminimalisir error error yang akan terjadi

Sewaktu kita menulis kode sendiri tanpa menggunakan PHP Unit Test, kadang hal tersebut masih aman aman saja untuk pengembangan jangka pendek. Namun untuk jangka panjang kita membutuhkan unit test, karena ketika kode itu dimantain, kita tidak mau error itu langsung berefek pada aplikasi yang sedang dijalankan

Sekarang kita tahu betapa pentingnya PHP unit testing pada aplikasi kita. Selanjutnya kita akan mempelajari apa saja sih perintilan perintilan unit testing pada PHP ini

Langkah Langkah

Menginstall phpunit dari packagist. Kita bisa menginstalnya menggunakan composer

composer install phpunit

Lalu kita akan mengatur composer.json dengan menambahkan script di bawah

  "autoload-dev": {
    "psr-4": {
      "YasyaIndraTech\\Test\\": "tests"
    }
  }

Untuk menjalankan kode kita harus membuat dua buah directory. Pertama folder src dan kedua tests

folder /src berisi file file class atau aplikasi yang digunakan sedangkan test berisi file file berisi testing dari file file yang berada di folder src

Ada beberapa hal yang harus diketahui saat kita ingin membuat unit test

Assertion, Annotation, Data Provider

Assertion merupakan proses dimana kita menginginkan sebuah method dari aplikasi kita memiliki value/return seperti yang diharapakan

        $this->assertEquals(1, self::$counter->getCounter());

Annotation merupakan kode yang menjadi identitas dari function function yang kita tulis

    /**
     * @test
     */

Data Provider merupakan fitur dalam unit test supaya nilai yang diharapkan bisa ditest secara menyeluruh dan bersamaan

    /**
     * @testWith [[5,5], 10]
     *      [[2,2,9,10], 23]
     *      [[], 0]
     *      [[0.5,0.5], 1]
     */
    public function testWith(array $values, int $expected){
        $this->assertEquals($expected, Math::sum($values));
    }

Fixture, Sharing Fixture

Fixture merupakan kemampuan dalam unit test saat kita ingin menginisiasi sebuah nilai, variabel atau value lainnya sebelum method test dijalankan.

    public function setUp(): void {
        $this->repository = $this->createMock(ProductRepository::class);
        $this->service = new ProductService($this->repository);
    }

Sedangkan Sharing Fixture merupakan inisiasi variabel/value pada masing masing method testing tanpa harus mengulang inisiasinya. Sharing Fixture menggunakan static function untuk bisa menerapkan proses ini

    public static function setUpBeforeClass():void {
        self::$counter = new Counter();
    }

Incomplete Test, Skipped

Incomplete Test dalam unit test merupakan sebuah fitur dimana kita memberikan tanda pada method testing bahwa proses yang dimaksud belum selesai

    public function increment()
    {
        $this->assertEquals(1, $this->counter->getCounter());
        $this->markTestIncomplete("TODO do increment");
    }

Skipped, kita melewati method testing tersebut

    public function increment()
    {
        self::markTestSkipped();
        $this->assertEquals(1, $this->counter->getCounter());
    }

Stub, Mock

Stub merupakan fitur dimana kita ingin mengaitkan object class lainnya dalam proses unit testing kita

    protected function setUp(): void
    {
        $this->product = new Product();
        $this->repository = $this->createStub(ProductRepository::class);
        $this->service = new ProductService($this->repository);
    }

    public function testStub()
    {
        $this->product = new Product();
        $this->product->setId("1");

        $this->repository->method('findById')->willReturnMap($product);

        $result = $this->repository->findById("1");
        self::assertSame($this->product, $result);
    }

Mock sama seperti Stub, namun pada Mock kita bisa mengetahui berapa kali method dari Stub/Mock dijalankan

    protected function setUp(): void
    {
        $this->product = new Product();
        $this->repository = $this->createMock(ProductRepository::class);
        $this->service = new ProductService($this->repository);
    }

    public function testMock()
    {
        $this->product = new Product();
        $this->product->setId("1");
        $this->repository->expects($this->once())->method('findById')->willReturnMap($product);
        $result = $this->repository->findById("1");
        self::assertSame($this->product, $result);
    }

Share
Tweet
Pin
Share
No comments
Newer Posts
Older Posts

About me

About Me

Techbros Writer. Educactor, you name it

Follow Us

  • instagram
  • youtube

Categories

Materi Kuliah Buku Internet Stuff

recent posts

Sponsor

Blog Archive

  • March 2025 (4)
  • February 2025 (1)
  • November 2024 (3)
  • October 2024 (1)
  • January 2024 (1)
  • December 2023 (12)
  • November 2023 (9)
  • October 2023 (1)
  • September 2023 (3)
  • August 2023 (14)
  • July 2023 (3)
  • June 2023 (11)
  • May 2023 (3)
  • April 2023 (1)
  • March 2023 (1)
  • February 2023 (8)
  • January 2023 (6)
  • December 2022 (3)
  • November 2022 (2)
  • October 2022 (3)
  • September 2022 (3)
  • August 2022 (1)
  • July 2022 (1)
  • June 2022 (1)
  • May 2022 (1)
  • March 2022 (4)
  • February 2022 (8)
  • January 2022 (8)
  • December 2021 (4)
  • November 2021 (11)
  • October 2021 (6)
  • August 2021 (9)
  • July 2021 (5)
  • June 2021 (5)
  • May 2021 (4)
  • April 2021 (4)
  • March 2021 (6)
  • February 2021 (2)
  • January 2021 (7)
  • December 2020 (5)
  • November 2020 (2)
  • October 2020 (5)
  • September 2020 (6)
  • July 2020 (1)
  • June 2020 (1)
  • May 2020 (6)
  • March 2020 (1)
  • January 2020 (3)
  • December 2019 (3)
  • November 2019 (12)
  • October 2019 (8)
  • September 2019 (6)
  • August 2019 (8)
  • July 2019 (6)
  • June 2019 (3)
  • May 2019 (8)
  • April 2019 (2)

Report Abuse

Created with by ThemeXpose