How to use Configure::read within Fixtures / Controllers

I have serval Tests where i would like to read a specific value from app.php
Therefore i defined the following at app.php

app.php

'test' => [
    some other fields ...
   'email' => 'test@localhost.com'
],

MeasuresFixture.php

public function init()
{
    $this->records = [
        [
             some other fields ...
            'email' => Configure::read('test.email'),
        ],
    ];
    parent::init();
}

MeasuresControllerTest.php

public function testSendAgain()
{
    $this->setupUser();
    $this->get('/admin/measures/send-again/1');
    $this->assertRedirect("/");
    $MeasuresTable = TableRegistry::get('Measures');
    $measure = $MeasuresTable->get(1);
     $this->assertEquals(0, $measure->exported);
}

MeasuresController.php

public function sendAgain(int $id = null): Response
    {
        $measure = $this->Measures->get($id);
        if ($this->Measures->save($measure, $this->saveParameter())) {
            $measure->sendNewMailingConfirmation();
            $this->Flash->success(__('Email has been send'));
        } else {
            $this->Flash->error(__('Email could not been send'));
        }

        return $this->redirect($this->referer());
    }

Entity/Measure.php

public function sendNewMailingConfirmation()
    {
        if (!empty($this->id)) {
            $this->Email->viewBuilder()->setTemplate('confirmation');
            $this->Email->setEmailFormat('html');
            $this->Email->set('measure', $this);
            $this->Email->setFrom([Configure::read('Staff.email') => Configure::read('Staff.name')]);
            $this->Email->setSubject('TEXT');
            $this->Email->setTo($this->email);
            $this->Email->send();
        }
    }

But this Results in

App\Test\TestCase\Controller\Admin\MeasuresControllerTest::testSendAgain
InvalidArgumentException: The email set for "to" is empty.

If Email is hardcoded all Tests all working fine.
So how to use dynamic data?

Sounds like maybe your app.php isn’t being loaded at all during tests? You could confirm this by adding an echo or a die at the top of it.

Good Point. The CakePHP Docs gives me this anwser:
https://book.cakephp.org/3/en/development/testing.html#fixtures
“CakePHP uses the connection named test in your config/app.php configuration file. If this connection is not usable, an exception will be raised and you will not be able to use database fixtures.”

I’ve added dd('loaded'); at the top of app.php and it throws the message as defined

Any conditional stuff in your app.php that might result in this bit not being executed? Anything later that calls consume on the test config instead of read? You haven’t shown the “send again” function at all. When you said if works if you hardcode the email, did you mean in that function or the measures fixture?

i’ve updated my code.
The Problem just exists when using “Configure::read()” within Fixtures/Controllers not within the main App

You use Configure::read('Staff.email') in your sendNewMailingConfirmation function. Does that work okay? Wondering (but too lazy at the moment to check for myself) whether your fixture is being loaded before app.php.

It works when hardcoding Email like

$this->records = [
        [
             some other fields ...
            'email' => 'test@localhost.com',
        ],
    ];

That doesn’t answer my questions…

using Configure::read within Entities works.

@Zuluru finally i have hardcoded the Email Addresses at Fixtures and just use Configure::read at ControllerTests. This works. Problem solved

It looks very good. Thanks for sharing.