HackMD
  • Prime
    Prime  Full-text search on all paid plans
    Search anywhere and reach everything in a Workspace with Prime plan.
    Got it
      • Create new note
      • Create a note from template
    • Prime  Full-text search on all paid plans
      Prime  Full-text search on all paid plans
      Search anywhere and reach everything in a Workspace with Prime plan.
      Got it
      • Sharing Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • More (Comment, Invitee)
      • Publishing
        Everyone on the web can find and read all notes of this public team.
        After the note is published, everyone on the web can find and read this note.
        See all published notes on profile page.
      • Commenting Enable
        Disabled Forbidden Owners Signed-in users Everyone
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Invitee
      • No invitee
      • Options
      • Versions and GitHub Sync
      • Transfer ownership
      • Delete this note
      • Template
      • Save as template
      • Insert from template
      • Export
      • Dropbox
      • Google Drive
      • Gist
      • Import
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
      • Download
      • Markdown
      • HTML
      • Raw HTML
    Menu Sharing Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Versions and GitHub Sync Transfer ownership Delete this note
    Export
    Dropbox Google Drive Gist
    Import
    Dropbox Google Drive Gist Clipboard
    Download
    Markdown HTML Raw HTML
    Back
    Sharing
    Sharing Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    More (Comment, Invitee)
    Publishing
    Everyone on the web can find and read all notes of this public team.
    After the note is published, everyone on the web can find and read this note.
    See all published notes on profile page.
    More (Comment, Invitee)
    Commenting Enable
    Disabled Forbidden Owners Signed-in users Everyone
    Permission
    Owners
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Invitee
    No invitee
       owned this note    owned this note      
    Published Linked with GitHub
    Like1 BookmarkBookmarked
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # PHPerのための「PHPUnit について語り合う」PHP TechCafe ## PHPers News ::: info [PHPの各バージョンの計算速度を比較してみた](https://zenn.dev/foolishell/articles/2c2e26f8566b74) ::: ::: info [個人的におすすめしたいプログラムの技術サイト](https://qiita.com/SARDONYX/items/16cd2ef2ea4b842915a9) ::: ::: info [PHPerのための配列処理100本ノック](https://zenn.dev/ad5/articles/60ca8edadc236f) ::: ::: info [【PHP8.1?】PHPのあらゆる関数を部分適用できるようにするRFC](https://qiita.com/rana_kualu/items/788ebf4fef188bd24e03) ::: ::: info [【PHP8.1】PHPで交差型が使えるようになる](https://qiita.com/rana_kualu/items/f49582bc94e0bae23d9c) ::: ::: info [IE11がついに2022年6月15日で終了へ 日本でのシェアは?](https://www.itmedia.co.jp/pcuser/articles/2105/21/news053_2.html) ::: ::: info [Laravel is 10 years old!](https://laravel-news.com/laravel-is-10-years-old) ::: ::: info [業務用コードの書き方(ver1.0)](https://qiita.com/aruim/items/b6aa38c67e20f46ba561) ::: ::: info [補遺: Laravel でテスト駆動開発するときの守破離と序破急](https://qiita.com/nunulk/items/b466ca10a087f4cd7dc8) ::: ::: info [PhpStormに入れておくと作業が爆速になるプラグイン一覧](https://qiita.com/nagareboshi/items/69dc6d5b6a0b7cc4cf27) ::: ::: info [PHP8.1で追加された新しい戻り値の型「never」について](https://php.watch/versions/8.1/never-return-type) ::: ::: info [PHPカンファレンス沖縄2021【参加レポート】](https://tech-blog.rakus.co.jp/entry/20210601/it-event) ::: ::: info [ラッキービジター詐欺で使用されるPHPマルウェア](https://blogs.jpcert.or.jp/ja/2021/06/php_malware.html) ::: ::: info [Laravel大規模開発入門!MVC分離のFatModel問題に対する責任分離と依存管理、その設計と考え方について](https://eh-career.com/engineerhub/entry/2021/05/28/160000) ::: ::: info [PHPの現場 42. Laravel 本と開発現場(ytake / kurikazu / omoon)](https://php-genba.shin1x1.com/42) ::: ::: info [PHPフレームワーク Laravel Webアプリケーション開発 バージョン8.x対応](https://www.amazon.co.jp/dp/4802613008/) ::: ## PHPUnit ### 参考資料 [PHPUnitのアサーションメソッドを知ろう! - RAKUS Developers Blog](https://tech-blog.rakus.co.jp/entry/20200716/phpunit) ### テストとは * 品質を担保するための工程 * プログラムが期待通りに動いているかの確認 * たとえば... * 正しい挙動をしているか * 負荷をかけた時は正常に動くか * 部品を合わせた時に正しく動くか ### Unit テストとは * 単体テスト * クラスや関数などの単位で動作を確認するテスト * 画面遷移での動作確認が不要 ### PHPUnit とは PHPプログラムとして作成可能なユニットテスト [PHPUnit マニュアル](https://phpunit.readthedocs.io/ja/latest/#) [その他のPHP系のユニットテストツール](https://ja.wikipedia.org/wiki/%E3%83%A6%E3%83%8B%E3%83%83%E3%83%88%E3%83%86%E3%82%B9%E3%83%88%E3%83%BB%E3%83%95%E3%83%AC%E3%83%BC%E3%83%A0%E3%83%AF%E3%83%BC%E3%82%AF%E4%B8%80%E8%A6%A7#PHP) ### 実際にはどう使っている?? #### 利用方法 #### 参考書籍などの紹介 ### TDD - テスト駆動開発 - https://www.amazon.co.jp/%E3%83%86%E3%82%B9%E3%83%88%E9%A7%86%E5%8B%95%E9%96%8B%E7%99%BA-Kent-Beck/dp/4274217884 - TDD Boot Camp 2020 Online #1 基調講演/ライブコーディング - https://www.youtube.com/watch?v=Q-FJ3XmFlT8 ### PHPUnitアサーションメソッド [PHPUnitのアサーションメソッドを知ろう! - RAKUS Developers Blog](https://tech-blog.rakus.co.jp/entry/20200716/phpunit) `アサーションメソッド` と `アノテーション` を組み合わせることでテスト対象のクラスや関数が期待通りに動作しているかを確認する * アサーションメソッド * 値を比較、検査して想定通りの値になっているかを確認 ```php= <?php use PHPUnit\Framework\TestCase; // PHPUnit での配列操作のテスト(PHPUnit のドキュメントより転載) class StackTest extends TestCase { public function testPushAndPop() { $stack = []; $this->assertSame(0, count($stack)); array_push($stack, 'foo'); $this->assertSame('foo', $stack[count($stack)-1]); $this->assertSame(1, count($stack)); $this->assertSame('foo', array_pop($stack)); $this->assertSame(0, count($stack)); } } ``` * アノテーション * テストメソッド間の依存性を明示的に宣言する ```php= <?php use PHPUnit\Framework\TestCase; // @depends アノテーションを使った依存性の表現(PHPUnit のドキュメントより転載) class StackTest extends TestCase { public function testEmpty() { $stack = []; $this->assertEmpty($stack); return $stack; } /** * @depends testEmpty */ public function testPush(array $stack) { array_push($stack, 'foo'); $this->assertSame('foo', $stack[count($stack)-1]); $this->assertNotEmpty($stack); return $stack; } /** * @depends testPush */ public function testPop(array $stack) { $this->assertSame('foo', array_pop($stack)); $this->assertEmpty($stack); } } ``` ### アサーションメソッド #### assertEquals * ある値が期待した値と等しいかを判定 * 比較時に暗黙の型変換が行われるため、あまり利用しない方が良い ```php= // 以下のアサーションは全て成功になる $this->assertEquals(1, '1'); $this->assertEquals(null, ''); $this->assertEquals(null, false); $this->assertEquals(null, 0); $this->assertEquals('1', true); ``` #### assertSame * `assertEquals` とは異なり、型の厳密な比較が可能なアサーションメソッド * 基本的にこちらを利用する方針で問題ないはず ```php= $this->assertSame('hoge', 'hoge'); // OK $this->assertSame(0, 0); // OK $this->assertSame(false, false); // OK $this->assertSame('hoge', 'fuga'); // NG $this->assertSame(0, false); // NG $this->assertSame(7, '7'); // NG ``` #### assertTrue, assertFalse, assertNull * `treu`, `false`, `null` を判定するアサーション * テスト結果の確認には `assertSame` を利用するよりわかりやすい ```php= $actualValue = false; // 以下はいずれも失敗になる $this->assertTrue($actualValue); $this->assertSame(true, $actualValue); ``` 以下はテスト失敗時のメッセージ ``` # assertSame(true, false) のメッセージ Failed asserting that false is identical to true. # assertTrue(false) のメッセージ Failed asserting that false is true. ``` #### assertCount * 配列や `countable` なオブジェクトwの要素数を検査するアサーション * こちらも `assertSame` よりもテストの結果がわかりやすい ```php= // 以下はいずれも失敗になる $this->assertCount(1, []); $this->asserSame(1, count([])); ``` ``` # assertCount(1, []) のメッセージ Failed asserting that actual size 0 matches expected size 1. # assertSame(1, count([])) のメッセージ Failed asserting that 0 is identical to 1. ``` #### assertStringStartsWith, assertStringEndsWith, assertStringContainsString * 文字列検査系のアサーション * assertSameだと煩雑になるテストが簡単に書けるようになる ```php= // 以下2つは同じ内容のテスト $this->assertStringStartsWith('foo', $string); $this->assertSame(0, strpos($string, 'foo')); // 何をテストしたいのか理解しにくい // 以下2つは同じ内容のテスト $this->assertStringEndsWith('bar', $string); $this->assertSame('bar', substr($string, -3)); // これも理解しにくい ``` #### assertRegExp * 正規表現が利用できる文字列検査 * 上記の `assertStringContainsString` よりも複雑な上限のテストが作成可能 #### assertIsXXX, assertInstanceOf * 値が配列かどうかの検査方法 * assertTrue(is_array($value)) * assertIsArray($value) * その他にも以下が存在 * is_int * is_numeric * is_string * is_bool * is_callable * is_object * オブジェクトの型検査 * assertInstanceOf(SomeClass::class, $value)

    Import from clipboard

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lost their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template is not available.


    Upgrade

    All
    • All
    • Team
    No template found.

    Create custom template


    Upgrade

    Delete template

    Do you really want to delete this template?

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Tutorials

    Book Mode Tutorial

    Slide Mode Tutorial

    YAML Metadata

    Contacts

    Facebook

    Twitter

    Feedback

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions

    Versions and GitHub Sync

    Sign in to link this note to GitHub Learn more
    This note is not linked with GitHub Learn more
     
    Add badge Pull Push GitHub Link Settings
    Upgrade now

    Version named by    

    More Less
    • Edit
    • Delete

    Note content is identical to the latest version.
    Compare with
      Choose a version
      No search result
      Version not found

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub

        Please sign in to GitHub and install the HackMD app on your GitHub repo. Learn more

         Sign in to GitHub

        HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Available push count

        Upgrade

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Upgrade

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully