I'm making my first unit test. For the record, I have coded several functional tests and they work.
Here, there is a little detail, my table measure is hypertable on TimescaleDB ( Postgres ), so I couldn't manage to use migrations / seeds.
Anyway, as it is a unit test, I need no DB connection here.
here is my working config for functional tests:
<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
Here is my code:
<?php
namespace Tests\Unit;
use App\Measure;
use Carbon\Carbon;
use PHPUnit\Framework\TestCase;
class MeasureTest extends TestCase
{
public function setUp() : void
{
parent::setUp();
}
/** @test * */
public function is_new_timestamp()
{
$timestep = "10";
$targetTimestamp = "30";
$ts = Carbon::create(2020, 04, 16, 13, 00, 0, null);
$m1 = new Measure(['time' => $ts]);
$m2 = new Measure(['time' => (clone $ts)->addMinutes($timestep)]);
$m3 = new Measure(['time' => (clone $ts)->addMinutes(2 * $timestep)]);
$m4 = new Measure(['time' => (clone $ts)->addMinutes(3 * $timestep)]);
$m5 = new Measure(['time' => (clone $ts)->addMinutes(4 * $timestep)]);
$m6 = new Measure(['time' => (clone $ts)->addMinutes(5 * $timestep)]);
assertTrue(Measure::isNextTimestamp($m1->time, $targetTimestamp));
assertFalse(Measure::isNextTimestamp($m2->time, $targetTimestamp));
assertFalse(Measure::isNextTimestamp($m3->time, $targetTimestamp));
assertTrue(Measure::isNextTimestamp($m4->time, $targetTimestamp));
assertFalse(Measure::isNextTimestamp($m5->time, $targetTimestamp));
assertFalse(Measure::isNextTimestamp($m6->time, $targetTimestamp));
}
How should I get rid of this error ? Where am i invoking connection() ?