Skip to content

Commit

Permalink
Events and listeners registered;
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-thomas committed Aug 30, 2023
1 parent d570ab8 commit 5e51e87
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 161 deletions.
120 changes: 63 additions & 57 deletions src/LyraServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,75 @@

namespace Hans\Lyra;

use Hans\Lyra\Events\TokenReceived;
use Hans\Lyra\Events\TransactionIdReceived;
use Hans\Lyra\Listeners\CheckTransIdIsUnique;
use Hans\Lyra\Listeners\StoreTokenOnDB;
use Hans\Lyra\Listeners\StoreTransIdOnDB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class LyraServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
class LyraServiceProvider extends ServiceProvider {
/**
* Register any application services.
*
* @return void
*/
public function register() {
//
}

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'lyra');
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot() {
$this->loadMigrationsFrom( __DIR__ . '/../database/migrations' );
$this->mergeConfigFrom( __DIR__ . '/../config/config.php', 'lyra' );

$this->registerRoutes();
if ($this->app->runningInConsole()) {
$this->registerCommands();
$this->registerPublishes();
}
}
$this->registerRoutes();
if ( $this->app->runningInConsole() ) {
$this->registerCommands();
$this->registerPublishes();
}

/**
* Define routes setup.
*
* @return void
*/
private function registerRoutes()
{
Route::prefix('lyra')->middleware('api')->group(__DIR__.'/../routes/api.php');
}
Event::listen( TokenReceived::class, [ StoreTokenOnDB::class ] );
Event::listen( TransactionIdReceived::class, [
CheckTransIdIsUnique::class,
StoreTransIdOnDB::class
] );
}

/**
* Register created commands.
*
* @return void
*/
private function registerCommands()
{
$this->commands([
// commands register here
]);
}
/**
* Define routes setup.
*
* @return void
*/
private function registerRoutes() {
Route::prefix( 'lyra' )->middleware( 'api' )->group( __DIR__ . '/../routes/api.php' );
}

/**
* Register publishable files.
*
* @return void
*/
private function registerPublishes()
{
$this->publishes([
__DIR__.'/../config/config.php' => config_path('lyra.php'),
], 'lyra-config');
}
/**
* Register created commands.
*
* @return void
*/
private function registerCommands() {
$this->commands( [
// commands register here
] );
}

/**
* Register publishable files.
*
* @return void
*/
private function registerPublishes() {
$this->publishes( [
__DIR__ . '/../config/config.php' => config_path( 'lyra.php' ),
], 'lyra-config' );
}
}
208 changes: 104 additions & 104 deletions tests/Unit/InvoiceModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,109 +2,109 @@

namespace Hans\Lyra\Tests\Unit;

use Hans\Lyra\Models\Invoicable;
use Hans\Lyra\Models\Invoice;
use Hans\Lyra\Tests\Core\Factories\PostFactory;
use Hans\Lyra\Tests\Core\Factories\ProductFactory;
use Hans\Lyra\Tests\Core\Models\Post;
use Hans\Lyra\Tests\Core\Models\Product;
use Hans\Lyra\Tests\TestCase;
use Illuminate\Support\Str;

class InvoiceModelTest extends TestCase
use Hans\Lyra\Models\Invoicable;
use Hans\Lyra\Models\Invoice;
use Hans\Lyra\Tests\Core\Factories\PostFactory;
use Hans\Lyra\Tests\Core\Factories\ProductFactory;
use Hans\Lyra\Tests\Core\Models\Post;
use Hans\Lyra\Tests\Core\Models\Product;
use Hans\Lyra\Tests\TestCase;
use Illuminate\Support\Str;

class InvoiceModelTest extends TestCase
{
/**
* @test
*
* @return void
*/
public function createWithNoParam(): void
{
/**
* @test
*
* @return void
*/
public function createWithNoParam(): void
{
$model = $this->makeInvoice();

self::assertInstanceOf(Invoice::class, $model);
self::assertIsInt($model->number);
self::assertLessThan(65535, $model->number);
self::assertNull($model->token);
self::assertNull($model->transaction_id);
}

/**
* @test
*
* @return void
*/
public function createWithParams(): void
{
$model = $this->makeInvoice([
'token' => $token = Str::random(),
'transaction_id' => $transId = Str::random(),
]);

self::assertIsString($model->token);
self::assertEquals($token, $model->token);

self::assertIsString($model->transaction_id);
self::assertEquals($transId, $model->transaction_id);
}

/**
* @test
*
* @return void
*/
public function itemsRelationship(): void
{
$model = $this->makeInvoice();

$product = $this->makeProduct();
$post = $this->makePost();

self::assertEmpty($model->items);

$product->invoices()->attach($model->id);
$post->invoices()->attach($model->id);
$model->refresh();

self::assertCount(2, $model->items);

self::assertInstanceOf(Invoicable::class, $model->items[0]);
self::assertEquals(
$model->items[0]->toArray(),
[
'invoice_id' => 1,
'invoicable_type' => "Hans\Lyra\Tests\Core\Models\Product",
'invoicable_id' => 1,
'created_at' => null,
'updated_at' => null,
]
);

self::assertInstanceOf(Invoicable::class, $model->items[1]);
self::assertEquals(
$model->items[1]->toArray(),
[
'invoice_id' => 1,
'invoicable_type' => "Hans\Lyra\Tests\Core\Models\Post",
'invoicable_id' => 1,
'created_at' => null,
'updated_at' => null,
]
);
}

protected function makeInvoice(array $data = []): Invoice
{
return Invoice::query()->create($data);
}

protected function makeProduct(array $data = []): Product
{
return ProductFactory::new()->create($data);
}

protected function makePost(array $data = []): Post
{
return PostFactory::new()->create($data);
}
$model = $this->makeInvoice();

self::assertInstanceOf(Invoice::class, $model);
self::assertIsInt($model->number);
self::assertLessThan(65535, $model->number);
self::assertNull($model->token);
self::assertNull($model->transaction_id);
}

/**
* @test
*
* @return void
*/
public function createWithParams(): void
{
$model = $this->makeInvoice([
'token' => $token = Str::random(),
'transaction_id' => $transId = Str::random(),
]);

self::assertIsString($model->token);
self::assertEquals($token, $model->token);

self::assertIsString($model->transaction_id);
self::assertEquals($transId, $model->transaction_id);
}

/**
* @test
*
* @return void
*/
public function itemsRelationship(): void
{
$model = $this->makeInvoice();

$product = $this->makeProduct();
$post = $this->makePost();

self::assertEmpty($model->items);

$product->invoices()->attach($model->id);
$post->invoices()->attach($model->id);
$model->refresh();

self::assertCount(2, $model->items);

self::assertInstanceOf(Invoicable::class, $model->items[0]);
self::assertEquals(
$model->items[0]->toArray(),
[
'invoice_id' => 1,
'invoicable_type' => "Hans\Lyra\Tests\Core\Models\Product",
'invoicable_id' => 1,
'created_at' => null,
'updated_at' => null,
]
);

self::assertInstanceOf(Invoicable::class, $model->items[1]);
self::assertEquals(
$model->items[1]->toArray(),
[
'invoice_id' => 1,
'invoicable_type' => "Hans\Lyra\Tests\Core\Models\Post",
'invoicable_id' => 1,
'created_at' => null,
'updated_at' => null,
]
);
}

protected function makeInvoice(array $data = []): Invoice
{
return Invoice::query()->create($data);
}

protected function makeProduct(array $data = []): Product
{
return ProductFactory::new()->create($data);
}

protected function makePost(array $data = []): Post
{
return PostFactory::new()->create($data);
}
}

0 comments on commit 5e51e87

Please sign in to comment.