Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    OdelGreg
    @OdelGreg
    by the way, the system was already up, and we are just making a tests now
    Akihito Koriyama
    @koriym
    Sure. No prob for that.
    OdelGreg
    @OdelGreg
    the way the system was coded have no TDD in mind
    I think this is the difficult part
    Akihito Koriyama
    @koriym
    I think it's OK when the code follow style guide. http://bearsunday.github.io/manuals/1.0/en/coding-guide.html
    Ok, Back to the original and basic question, "How to mock"
    You have two options:
    One is provide special context like "test". You create TestModule in Module/ directory and bind it. This is the example.
    https://github.com/bearsunday/tutorial2/blob/master/src/Module/TestModule.php
    OdelGreg
    @OdelGreg
    I see
    Akihito Koriyama
    @koriym
    Override test database in the test context. Then use that context test-app.
    https://github.com/bearsunday/tutorial2/blob/master/tests/Resource/App/TicketsTest.php#L19
    test-app means TestModule override AppModule bindings.
    OdelGreg
    @OdelGreg
    I understand
    but is it possible to make a new tests for an existing app without modiying it's code?
    Akihito Koriyama
    @koriym
    But anything else stay same. It's simple.
    What did you do so far are same. prod-html-app menas ProdModule(in BEAR module) override HtmlModile which override AppModule.
    This is so-called "decoration pattern".
    The other method is more temporary way. See the "Bindings using anonymous classes" in the test section of the manual.
    image.png
    Akihito Koriyama
    @koriym
    In this example,FooInterface was bound to Foo (mock) class temporary.
    When you feel too much to provide TestModule for only one-time mock binding, This is the great way.
    OdelGreg
    @OdelGreg
    noted
    Akihito Koriyama
    @koriym

    but is it possible to make a new tests for an existing app without modiying it's code?

    Sure. That's the reason why we use Dependency Injectionall around and rely only interfaces not concrete class.

    "Do not use static calls" - is for this.
    But when your code use global variables like $_SEESION or static call all around against code style guide, You need to refactor it for mocking. - -- this is not great.
    Akihito Koriyama
    @koriym
    Could I answer your question ?
    OdelGreg
    @OdelGreg
    yes, we still need to check the existing codes to be able to make the test without errors and make it pass
    my second question is, how about forms?
    Akihito Koriyama
    @koriym
    Testing the form ?
    OdelGreg
    @OdelGreg
    how do we mock/use it on test? I believe you already have an example of it?
    Did you see the Demo ?
    OdelGreg
    @OdelGreg
    ok I will check this, thank you very much
    Akihito Koriyama
    @koriym
    These are examples. This works the form has little dependencies. But when the form needs more dependencies like data from database. You may need AppInjector here.
    Maybe I need to update these code with using AppInjector.
    Remember AppInjectoris the Swiss army knife in a BEAR.Sunday application testing. You can fetch any objects through interface with fake object by customized context. Very powerful.
    OdelGreg
    @OdelGreg
    noted
    Akihito Koriyama
    @koriym
    OdelGreg
    @OdelGreg
    thank you again, we were able to override objects (session object in this case) using the anonymous class (although some code we still need to modify due to the use of @session annotation on its method)
    Akihito Koriyama
    @koriym
    👍
    piotzkhider
    @piotzkhider
    @koriym
    チュートリアル試してみました!
    チュートリアル2においてこちらの環境で起きたエラーを書いておきます。
    ysato:MyVendor.Ticket ysato$ php bin/app.php post '/tickets?title=run'
    PHP Notice:  Undefined property: stdClass::$ticket in /Users/ysato/PhpStormProjects/MyVendor.Ticket/vendor/bear/resource/src/JsonSchema/Interceptor/JsonSchemaInterceptor.php on line 114
    
    Notice: Undefined property: stdClass::$ticket in /Users/ysato/PhpStormProjects/MyVendor.Ticket/vendor/bear/resource/src/JsonSchema/Interceptor/JsonSchemaInterceptor.php on line 114
    503 Service Unavailable
    content-type: application/vnd.error+json
    
    {
        "message": "Service Unavailable",
        "logref": "f52d88ce",
        "request": "post app://self/tickets?title=run",
        "exceptions": "BEAR\\Package\\Exception\\LocationHeaderRequestException(app://self/ticket?id=c642f957-908c-4584-9d6f-191587e522e7)",
        "file": "/Users/ysato/PhpStormProjects/MyVendor.Ticket/vendor/bear/package/src/Provide/Representation/CreatedResourceRenderer.php(49)"
    }
    piotzkhider
    @piotzkhider

    @koriym
    チュートリアル1において、こちらの環境で起きたエラーやドキュメントと異なる動作になっていた箇所を記載します。

    https://github.com/bearsunday/bearsunday.github.io/blame/master/manuals/1.0/ja/tutorial.md#L70

    期待する結果

    200 OK
    Content-Type: application/hal+json
    
    {
        "weekday": "Mon",
        "_links": {
            "self": {
                "href": "/weekday?year=2001&month=1&day=1"
            }
        }
    }

    実際の結果

    200 OK
    
    {
        "weekday": "Mon",
        "_links": {
            "self": {
                "href": "/weekday?year=2001&month=1&day=1"
            }
        }
    }

    https://github.com/bearsunday/bearsunday.github.io/blame/master/manuals/1.0/ja/tutorial.md#L99

    期待する結果

     HTTP/1.1 200 OK
     Host: 127.0.0.1:8080
     Date: Fri, 01 Sep 2017 09:31:13 +0200
     Connection: close
     X-Powered-By: PHP/7.1.8
     content-type: application/hal+json
    
     {
         "weekday": "Mon",
         "_links": {
             "self": {
                 "href": "/weekday/2001/1/1"
             }
         }
     }

    実際の結果

     HTTP/1.1 200 OK
     Host: 127.0.0.1:8080
     Date: Thu, 25 Jul 2019 01:43:41 GMT
     Connection: close
     X-Powered-By: PHP/7.2.19
     Content-type: text/html; charset=UTF-8
    
     {
         "weekday": "Mon",
         "_links": {
             "self": {
                 "href": "/weekday?year=2001&month=1&day=1"
             }
         }
     }

    https://github.com/bearsunday/bearsunday.github.io/blame/master/manuals/1.0/ja/tutorial.md#L695

    期待する結果

    200 OK
    content-type: text/html; charset=utf-8
    
    <!doctype html>
    ...

    実際の結果

    200 OK
    
    <!doctype html>
    ...

    https://github.com/bearsunday/bearsunday.github.io/blame/master/manuals/1.0/ja/tutorial.md#L939

    何回かリクエストしてLast-Modifiedの日付が変わらないことを確認しましょう。とあるが変更される

    https://github.com/bearsunday/bearsunday.github.io/blame/master/manuals/1.0/ja/tutorial.md#L972

    期待する結果

    HTTP/1.1 204 No Content
    ...

    実際の結果

    ysato:MyVendor.Weekday ysato$ curl -i http://127.0.0.1:8081/todos -X PUT -d "id=1&todo=think"
    HTTP/1.1 200 OK
    Host: 127.0.0.1:8081
    Date: Thu, 25 Jul 2019 06:22:23 GMT
    Connection: close
    X-Powered-By: PHP/7.2.19
    Content-type: text/html; charset=UTF-8
    
    <br />
    <b>Fatal error</b>:  Uncaught TypeError: Argument 1 passed to MyVendor\Weekday\Resource\App\Todos_lglGOJA::onGet() must be of the type integer, string given, called in /Users/ysato/PhpStormProjects/MyVendor.Weekday/vendor/bear/query-repository/src/RefreshSameCommand.php on line 40 and defined in /Users/ysato/PhpStormProjects/MyVendor.Weekday/var/tmp/hal-api-app/di/MyVendor_Weekday_Resource_App_Todos_lglGOJA.php:23
    Stack trace:
    #0 /Users/ysato/PhpStormProjects/MyVendor.Weekday/vendor/bear/query-repository/src/RefreshSameCommand.php(40): MyVendor\Weekday\Resource\App\Todos_lglGOJA-&gt;onGet('1')
    #1 /Users/ysato/PhpStormProjects/MyVendor.Weekday/vendor/bear/query-repository/src/CommandInterceptor.php(48): BEAR\QueryRepository\RefreshSameCommand-&gt;command(NULL, Object(MyVendor\Weekday\Resource\App\Todos_lglGOJA))
    #2 /Users/ysato/PhpStormProjects/MyVendor.Weekday/vendor/ray/aop/src/ReflectiveMethodInvocation.php(109): BEAR\QueryRepository\CommandInterceptor-&gt;invoke(Object(Ray\Aop\ReflectiveMethodInvocation))
    #3 /Users/ysato/PhpStormProjec in <b>/Users/ysato/PhpStormProjects/MyVendor.Weekday/var/tmp/hal-api-app/di/MyVendor_Weekday_Resource_App_Todos_lglGOJA.php</b> on line <b>23</b><br />
    Akihito Koriyama
    @koriym
    @piotzkhider 報告ありがとうございます!
    Akihito Koriyama
    @koriym
    @piotzkhider チュートリアル2の問題確認して修正中です。
    Akihito Koriyama
    @koriym
    @piotzkhider bearsunday/bearsunday.github.io#167 修正しました
    piotzkhider
    @piotzkhider
    @koriym ありがとうございます!確認します!
    Akihito Koriyama
    @koriym
    助かります!
    piotzkhider
    @piotzkhider
    @koriym 1点気になった点がありましたのでPR致しました。
    Akihito Koriyama
    @koriym
    PRどこでしょうか?マニュアルのPRにないような...
    piotzkhider
    @piotzkhider
    @koriym bearsunday/bearsunday.github.io#168
    こちらです!もうマージしていただいております〜
    Akihito Koriyama
    @koriym
    😊👍