Welcome Guest, Not a member yet? Register   Sign In
help w/testing: variables not passed to view in 4.5.4
#1

Hi,
I'm using 4.5.4 and I'm trying to set up some tests but I'm having a difficult time. This is the code from my controller:
Code:
    $data['is_new'] = $is_new;
    return view('my_body', $data);
Then in the view I have this:
PHP Code:
<?php

?>
  if ($is_new) {
    <?php
      
print "Welcome, new user.";
    ?>
  } else {
      print "Welcome back.";
  } 


In my test, which I pretty much copied from the docs:
PHP Code:
<?php
  
namespace app\Controllers;
  use CodeIgniter\Test\CIUnitTestCase;
  use CodeIgniter\Test\FeatureTestTrait;
  use CodeIgniter\Test\DatabaseTestTrait;
  use PHPUnit\Framework\Attributes\TestDox;

  class MyControllerFeaturesTest extends CIUnitTestCase
  
{
    use FeatureTestTrait;
    use DatabaseTestTrait;
    /**
    * setup and teardown run before and after
    * the _entire_ test case
    * setup and tearDown run _between_ tests
    */
    public static function setUpBeforeClass(): void
    
{
      parent::setUpBeforeClass();
    }
    public static function tearDownAfterClass(): void
    
{
      parent::tearDownAfterClass();
    }
    protected function setUp(): void
    
{
      parent::setUp(); // Do not forget

      helper('text');
    }
    protected function tearDown(): void {
      parent::tearDown();
    }

    #[TestDox('A new user should get a welcome')]
    public function testViewingResults(): void
    
{
      $response $this->get('/forum/loggedin/79435')
                        ->call();
      $response->assertSee('Welcome, new user');
    }
  



The error I'm getting is that $is_new is not defined in the view:
Code:
ErrorException: Undefined variable $is_new

The page actually works just fine, no errors at all. I can go to the url, see the appropriate message based on whether or not I'm a new user. 

Any ideas?
Reply
#2

(This post was last modified: 03-10-2025, 08:22 AM by ozornick.)

All ok.
Code:
> phpunit './tests/system/IndexTest.php'
PHPUnit 11.5.12 by Sebastian Bergmann and contributors.

Runtime:      PHP 8.4.4 with Xdebug 3.4.1
Configuration: /home/www/codeigniter/development/phpunit.xml.dist

.                                                                                                                                                        1 / 1 (100%)

Time: 00:00.090, Memory: 22.00 MB

OK (1 test, 1 assertion)

PHP Code:
// app/Controllers/Home.php
<?php
namespace App\Controllers;
class 
Home extends BaseController
{
    public function index(): string
    
{
        $is_new        true;
        $data['is_new'] = $is_new;
        return view('welcome_message'$data);
    }
}
// app/Views/welcome_message.php
<?php
    
if ($is_new) {
        print "Welcome, new user.";
    } else {
        print "Welcome back.";
    }
?>

// Test file
    #[TestDox('A new user should get a welcome')]
    public function testViewingResults(): void
    {
        $response = $this->get('/');
        $response->assertSee('Welcome, new user');
    } 
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

(This post was last modified: 03-10-2025, 09:10 AM by rramsey.)

(03-10-2025, 08:21 AM)ozornick Wrote: All ok.

Hmm. So something I either missed or didn't get in the code when I was stripping out the junk.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB