Welcome Guest, Not a member yet? Register   Sign In
Testing CRUD operations
#1

Hello All !

I am learning about PhpUnit and Php testing.

I tried to find some documentation about testing CRUD operations but I couldn't find anything related to CI.

I believe that the "problem" is that testing CRUD is a integration test, right ?

A basic "script" for testing a Add (POST) operation would be:

1 -> Send a Post request with data to a URL
2 -> Check if the return is right
3 -> Check if the record was inserted into the database

Let's start with the first step:

Question: What is the best way to send POST data from PHP for testing ? Does CI have something to help us with this ?
Reply
#2

CI4 does have something to help you. There's probably some features that are missing but it's should cover your basic needs.
Reply
#3

Awesome !

Thank you my friend !

I got that working !

Now, is there a way to easily check if the recorded was inserted into the database ?

Do you believe that we should be testing for something else in a basic CRUD operation ?
Reply
#4

There's a number of database testing methods. You might want to peruse the testing portion of the docs a bit more...

PHP Code:
$this->seeInDatabase('table', ['foo' => 'bar']); 

You should check both success and failure conditions, like when the wrong type of data was sent, etc. Test any edge cases you can think of, etc. However, just getting the basic success/fail conditions tested on those goes a long way when you're building your app out to make sure you don't break things.
Reply
#5

(This post was last modified: 10-30-2019, 08:40 PM by Poetawd.)

(10-30-2019, 08:16 PM)kilishan Wrote: There's a number of database testing methods. You might want to peruse the testing portion of the docs a bit more...

PHP Code:
$this->seeInDatabase('table', ['foo' => 'bar']); 

You should check both success and failure conditions, like when the wrong type of data was sent, etc. Test any edge cases you can think of, etc. However, just getting the basic success/fail conditions tested on those goes a long way when you're building your app out to make sure you don't break things.

Thank you Sir !

BTW, I miss your posts in your patreon page ... The last one was 2 months ago ! It would be awesome to see some CI testing there....

Specially on how to make all system tests PASS... haHaha

Also, I am playing around with project-tests. https://github.com/codeigniter4projects/project-tests
Reply
#6

MGatner did a great job with the project tests repo. That's what Myth:Auth is using currently (though I'm in the middle of trying to beef up tests on that).

RE: Patreon - I know! And I feel horrible. In the middle of recording some, actually, so will have some very soon.
Reply
#7

(10-30-2019, 08:40 PM)kilishan Wrote: MGatner did a great job with the project tests repo. That's what Myth:Auth is using currently (though I'm in the middle of trying to beef up tests on that).

RE: Patreon - I know! And I feel horrible. In the middle of recording some, actually, so will have some very soon.

Great to know that Myth:Auth is using that ! I will take a look and use it as a example ! I love to learn from practical examples...

About Patreon - Don't fell bad about it ! We all know that you are working hard and spending a lot of time in Myth:Auth !

Btw, do you believe that you will have a stable release anytime soon ? I wanted to use it in my current project but, I got scared with:


Quote:NOTE: This package is under early development and is not ready for prime-time.

Big Grin
Reply
#8

I put that note up there when it was in the middle of being ported from older codes a while back and just haven't taken it down yet, since we haven't hit a final release, yet. It's in a beta release, though.

Myth:Auth is very close to ready. A couple of people are already using it in projects. I don't expect any breaking changes to happen. Well - except one that I found a couple of nights ago doing testing that deals with where user-specific permissions are stored. Well - the api for that doesn't change, but it seems there's two locations it could store it currently so I need to whittle that down to one. Smile
Reply
#9

(This post was last modified: 10-31-2019, 02:00 PM by Poetawd.)

(10-31-2019, 06:27 AM)kilishan Wrote: I put that note up there when it was in the middle of being ported from older codes a while back and just haven't taken it down yet, since we haven't hit a final release, yet. It's in a beta release, though.

Myth:Auth is very close to ready. A couple of people are already using it in projects. I don't expect any breaking changes to happen. Well - except one that I found a couple of nights ago doing testing that deals with where user-specific permissions are stored. Well - the api for that doesn't change, but it seems there's two locations it could store it currently so I need to whittle that down to one. Smile

Really ??? Consider yourself forgiven for not posting in Patreon !  Heart Heart Heart ... and Please remove that statement that keeps people far away from Myth Auth....

Angel  ... I will create a new Support post if I have any questions... And please, don´t be mad if the are newbie questions... I am note afraid of asking the Jedi Masters when I am not 100% sure...
Reply
#10

Hi Codeigniter community
Is there anyone who can help me with Codeigniter4 and phpunit testing. I'm working on feature and unit testing of CRUD modules but there am facing issue that when when I run tests with debug option vendor/bin/phpunit --debug it does not show any output.
Code:
<?php

namespace App;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\ControllerTestTrait;
use CodeIgniter\Test\FeatureTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
class JobsControllerTest extends FeatureTestCase
{
   use ControllerTestTrait,DatabaseTestTrait;
  
    /**
     * Test IndexPage
     */
  
    public function testIndexPage()
    {
      
        $result = $this->withUri('http://localhost/codeigniter4/institutes/jobs')
        ->controller(\App\Controllers\JobsController::class)
        ->execute('Index');
        // Check if success
        $this->assertTrue($result->isOK());
        // Check if redirect
        $this->assertFalse($result->isRedirect());
        // check the content of page
        $this->assertFalse($result->see('job'));
    }
    
    /**
     * Test Submit Job Data
     * is without form
     */
    function testSubmitJObData(){
        $body=[];
        $result = $this->withBody($body)
        ->controller(\App\Controllers\JobsController::class)
        ->execute('Index');
        // Check if success i
        $this->assertTrue($result->isOK());
        // Check if redirect
        $this->assertFalse($result->isRedirect());
        // check the content of page
        $this->assertFalse($result->see('Add'));
    }
    /**
     * Test Delete Job
     */
    public function testDeleteJobData()
    {
        $_POST['id']=3;
        $controller = new \App\Controllers\JobsController;
        $result=$controller->deleteRowData();
        $this->assertTrue($result);
    }
    
}
there is an issue with firt test. if I comment first one Second one runs successfully.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB