Home>

I am the first person to write unit tests in cakephp

I want to test a method that can only be executed by a user who is logged in to Facebook. I thought

Error message

Because i am not redirected to Facebook on the site, it will be redirected, so I want to test if the user is logged in and not, but both will have the same result

The first assumption that the user has not logged in is a response to the effect that it was redirected
The second person thinks that 200 must be returned. Is this premise different?

Applicable test code
<? php
namespace App \ Test \ TestCase \ Controller;
use App \ Controller \ PaymentsController;
use Cake \ TestSuite \ IntegrationTestCase;
class PaymentsControllerTest extends IntegrationTestCase
{
    / **
     * Fixtures
     *
     * @var array
     * /
    // public $fixtures = [
    // 'app.users',
    // 'app.clubs',
    // 'app.club_plans',
    // 'app.subscriptions',
    // 'app.user_infos',
    // 'app.clubs_users'
    //];

    public function testPayNoUser ()
    {
        $clubId = 'Club ID in test DB';
        $this->get ('/ payments/pay /'. $clubId);
        $this->assertResponseOk ();
        // no session
        $this->assertSession (null, 'Auth.User.id');
        $this->assertRedirectContains ("/ authentications/login_for_pay /". $clubId);}
    public function testPay ()
    {
        // Set session data
        $this->session ([
            'Auth' =>[
                'Users' =>[
                    'id' =>'User ID in test DB',
                ]
            ]
        ]);
        $clubId = 'Club ID in test DB';
        $this->get ('/ payments/pay /'. $clubId);
        $this->assertResponseOk ();
        $this->assertSession (1, 'Auth.User.id');
    }
}
Response
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.
FF 2/2 (100%)
Time: 356 ms, Memory: 10.00MB
There were 2 failures:
1) App \ Test \ TestCase \ Controller \ PaymentsControllerTest :: testPayNoUser
Status code is not between 200 and 204
Failed asserting that 302 is equal to 204 or is less than 204.
/Applications/MAMP/htdocs/app_name/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:794
/Applications/MAMP/htdocs/app_name/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:711
/Applications/MAMP/htdocs/app_name/tests/TestCase/Controller/PaymentsControllerTest.php:56
2) App \ Test \ TestCase \ Controller \ PaymentsControllerTest :: testPay
Status code is not between 200 and 204
Failed asserting that 302 is equal to 204 or is less than 204.
/Applications/MAMP/htdocs/app_name/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:794
/Applications/MAMP/htdocs/app_name/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestCase.php:711
/Applications/MAMP/htdocs/app_name/tests/TestCase/Controller/PaymentsControllerTest.php:76
FAILURES!
Tests: 2, Assertions: 8, Failures: 2.</Code></pre><strong>Original controller</strong>
<pre><code>public function pay ($clubId = null) {
    if (! $this->Auth->user ()) {
      return $this->redirect ("/ authentications/login_for_pay /". $clubId);
    }
    // Check club id
    if (empty ($clubId)) {
      $this->Flash->error (__ ('Invalid request.'));
      return $this->redirect ($this->referer ());
    }
    // Check club data
    $club = $this->clubs->find ()
      ->contain (['clubPlans'])
      ->where (['clubs.id' =>$clubId])
      ->first ();
    if (empty ($club)) {
      $this->Flash->error (__ ('There is no club data.'));
      return $this->redirect ($this->referer ());
    }
...

I am referring to the following articles
・ Official https: //book.cakephp. org/3/en/development/testing.html # integration-testing
・ About tests around the controller
https://qiita.com/o0h/items/ea8d1b3604c37c3a8287
・ Tests for various actions
https://pachicourse.com/archives/486
・ About test automation
https://insight.hiliberate.biz/?p=2313

However, I haven't really figured out what kind of response should be returned in the first place
If i think your test results are wrong or you misunderstood, please let us know

Supplemental information (FW/tool version etc.)

cakephp3.5
mysql
PHP 7.2.22
Server MAMP

  • Answer # 1

    I didn't understand how to check with assertions, and I didn't know how to use each method, but I was able to check the behavior when authenticating with the following syntax

    public function testPay ($clubId = null)
        {
            $this->session ([
                'Auth' =>[
                    'User' =>[
                        'id' =>'id-xxx',
                        'facebook_name' =>'Name',
                    ]
                ]
            ]);
            $this->get ('/ payments/pay /'. $clubId);
            $this->assertSession ('id-xxx', 'Auth.User.id');
        }