Skip to content

Commit

Permalink
prepare tests, added justNow scope and fixed second scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sairahcaz committed Apr 15, 2023
1 parent b435390 commit aa2e652
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 21 deletions.
20 changes: 20 additions & 0 deletions database/factories/TransactionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace LaracraftTech\LaravelDateScopes\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use LaracraftTech\LaravelDateScopes\Tests\Models\Transaction;

class TransactionFactory extends Factory
{
protected $model = Transaction::class;

public function definition(): array
{
return [
'col1' => fake()->sentence(),
'col2' => fake()->randomNumber(),
'created_at' => fake()->date(),
];
}
}
97 changes: 97 additions & 0 deletions ray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

return [
/*
* This setting controls whether data should be sent to Ray.
*
* By default, `ray()` will only transmit data in non-production environments.
*/
'enable' => env('RAY_ENABLED', true),

/*
* When enabled, all cache events will automatically be sent to Ray.
*/
'send_cache_to_ray' => env('SEND_CACHE_TO_RAY', false),

/*
* When enabled, all things passed to `dump` or `dd`
* will be sent to Ray as well.
*/
'send_dumps_to_ray' => env('SEND_DUMPS_TO_RAY', true),

/*
* When enabled all job events will automatically be sent to Ray.
*/
'send_jobs_to_ray' => env('SEND_JOBS_TO_RAY', false),

/*
* When enabled, all things logged to the application log
* will be sent to Ray as well.
*/
'send_log_calls_to_ray' => env('SEND_LOG_CALLS_TO_RAY', false),

/*
* When enabled, all queries will automatically be sent to Ray.
*/
'send_queries_to_ray' => env('SEND_QUERIES_TO_RAY', false),

/**
* When enabled, all duplicate queries will automatically be sent to Ray.
*/
'send_duplicate_queries_to_ray' => env('SEND_DUPLICATE_QUERIES_TO_RAY', false),

/*
* When enabled, slow queries will automatically be sent to Ray.
*/
'send_slow_queries_to_ray' => env('SEND_SLOW_QUERIES_TO_RAY', false),

/*
* When enabled, all requests made to this app will automatically be sent to Ray.
*/
'send_requests_to_ray' => env('SEND_REQUESTS_TO_RAY', false),

/**
* When enabled, all Http Client requests made by this app will be automatically sent to Ray.
*/
'send_http_client_requests_to_ray' => env('SEND_HTTP_CLIENT_REQUESTS_TO_RAY', false),

/*
* When enabled, all views that are rendered automatically be sent to Ray.
*/
'send_views_to_ray' => env('SEND_VIEWS_TO_RAY', false),

/*
* When enabled, all exceptions will be automatically sent to Ray.
*/
'send_exceptions_to_ray' => env('SEND_EXCEPTIONS_TO_RAY', true),

/*
* The host used to communicate with the Ray app.
* When using Docker on Mac or Windows, you can replace localhost with 'host.docker.internal'
* When using Homestead with the VirtualBox provider, you can replace localhost with '10.0.2.2'
* When using Homestead with the Parallels provider, you can replace localhost with '10.211.55.2'
*/
'host' => env('RAY_HOST', 'host.docker.internal'),

/*
* The port number used to communicate with the Ray app.
*/
'port' => env('RAY_PORT', 23517),

/*
* Absolute base path for your sites or projects in Homestead,
* Vagrant, Docker, or another remote development server.
*/
'remote_path' => env('RAY_REMOTE_PATH', null),

/*
* Absolute base path for your sites or projects on your local
* computer where your IDE or code editor is running on.
*/
'local_path' => env('RAY_LOCAL_PATH', null),

/*
* When this setting is enabled, the package will not try to format values sent to Ray.
*/
'always_send_raw_values' => false,
];
6 changes: 2 additions & 4 deletions src/DateScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public function scopeOfLastUnit(Builder $query, string $dateUnit, int $value, Da

$applyNoOverflow = (! in_array($dateUnit, $this->fixedLengthDateUnits)) ? 'NoOverflow' : '' ;
$subFunc = 'sub'.ucfirst($dateUnit).'s'.$applyNoOverflow;

$sub = ($dateUnit === 'second') ? 0 : 1;
// $sub = 1;
$sub = 1;

if ($dateRange === DateRange::EXCLUSIVE) {
$range = [
Expand All @@ -57,7 +55,7 @@ public function scopeOfLastUnit(Builder $query, string $dateUnit, int $value, Da
];
}

// dump(collect($range)->transform(fn ($item) => $item->format('Y-m-d H:i:s'))->toArray());
//dump(collect($range)->transform(fn ($item) => $item->format('Y-m-d H:i:s'))->toArray());

return $query->whereBetween(config('date-scopes.created_column'), $range);
}
Expand Down
17 changes: 0 additions & 17 deletions tests/DataScopesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,8 @@ function getCreatedAtValues(): array
->count(count($createdAtValues))
->state(new Sequence(...$createdAtValues))
->create();

Transaction::ofJustNow()->get();
Transaction::ofLast15Seconds(DateRange::INCLUSIVE)->get();
Transaction::ofLast15Seconds(DateRange::EXCLUSIVE)->get();
dd();
});

it('retrieves transactions of last x seconds', function () {
// expect(Transaction::ofJustNow()->get())->toHaveCount(1);
// expect(Transaction::ofLastSecond()->get())->toHaveCount(1);
// expect(Transaction::ofLast15Seconds(DateRange::INCLUSIVE)->get())->toHaveCount(2);
// expect(Transaction::ofLast15Seconds(DateRange::EXCLUSIVE)->get())->toHaveCount(2);


// Transaction::ofToday()->get();
// Transaction::ofYesterday()->get();
// Transaction::ofLast7Days()->get();
});
//
it('covers all cases', function () {
//TODO
// Write tests for all kind of scopes
Expand Down
12 changes: 12 additions & 0 deletions tests/Models/Transaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace LaracraftTech\LaravelDateScopes\Tests\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use LaracraftTech\LaravelDateScopes\DateScopes;

class Transaction extends Model
{
use HasFactory, DateScopes;
}

0 comments on commit aa2e652

Please sign in to comment.