From 90262abffb81468af5a7f07c2e19b80a3bb4099f Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Fri, 6 Sep 2024 19:39:04 -0500 Subject: [PATCH] wip testing --- database/factories/PortfolioFactory.php | 26 +++++++ database/factories/TransactionFactory.php | 62 ++++++++++++++++ database/factories/UserFactory.php | 24 ------ phpunit.xml | 7 +- .../{Feature => }/ApiTokenPermissionsTest.php | 8 +- tests/{Feature => }/AuthenticationTest.php | 2 +- tests/{Feature => }/BrowserSessionsTest.php | 2 +- tests/{Feature => }/DeleteAccountTest.php | 2 +- tests/{Feature => }/EmailVerificationTest.php | 4 +- tests/Feature/ExampleTest.php | 19 ----- .../PasswordConfirmationTest.php | 4 +- tests/{Feature => }/PasswordResetTest.php | 2 +- .../{Feature => }/ProfileInformationTest.php | 2 +- tests/{Feature => }/RegistrationTest.php | 13 +--- tests/TestCase.php | 15 +++- tests/TransactionsTest.php | 73 +++++++++++++++++++ .../TwoFactorAuthenticationSettingsTest.php | 2 +- tests/Unit/ExampleTest.php | 16 ---- tests/{Feature => }/UpdatePasswordTest.php | 2 +- 19 files changed, 193 insertions(+), 92 deletions(-) create mode 100644 database/factories/PortfolioFactory.php create mode 100644 database/factories/TransactionFactory.php rename tests/{Feature => }/ApiTokenPermissionsTest.php (91%) rename tests/{Feature => }/AuthenticationTest.php (97%) rename tests/{Feature => }/BrowserSessionsTest.php (95%) rename tests/{Feature => }/DeleteAccountTest.php (98%) rename tests/{Feature => }/EmailVerificationTest.php (95%) delete mode 100644 tests/Feature/ExampleTest.php rename tests/{Feature => }/PasswordConfirmationTest.php (92%) rename tests/{Feature => }/PasswordResetTest.php (99%) rename tests/{Feature => }/ProfileInformationTest.php (97%) rename tests/{Feature => }/RegistrationTest.php (75%) create mode 100644 tests/TransactionsTest.php rename tests/{Feature => }/TwoFactorAuthenticationSettingsTest.php (98%) delete mode 100644 tests/Unit/ExampleTest.php rename tests/{Feature => }/UpdatePasswordTest.php (98%) diff --git a/database/factories/PortfolioFactory.php b/database/factories/PortfolioFactory.php new file mode 100644 index 0000000..0d64dcd --- /dev/null +++ b/database/factories/PortfolioFactory.php @@ -0,0 +1,26 @@ + + */ +class PortfolioFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'title' => $this->faker->word, + 'created_at' => now(), + 'updated_at' => now(), + ]; + } +} diff --git a/database/factories/TransactionFactory.php b/database/factories/TransactionFactory.php new file mode 100644 index 0000000..a0c3cdc --- /dev/null +++ b/database/factories/TransactionFactory.php @@ -0,0 +1,62 @@ + + */ +class TransactionFactory extends Factory +{ + protected static ?string $transaction_type; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'symbol' => $this->faker->randomElement(['AAPL', 'GOOGL', 'AMZN']), + 'transaction_type' => static::$transaction_type = $this->faker->randomElement(['BUY', 'SELL']), + 'portfolio_id' => Portfolio::factory()->create(), + 'date' => $this->faker->date('Y-m-d'), + 'quantity' => $this->faker->randomFloat(2, 1, 100), + 'cost_basis' => $this->faker->randomFloat(2, 10, 500), + 'sale_price' => static::$transaction_type == 'SELL' + ? $this->faker->randomFloat(2, 10, 500) + : null, + ]; + } + + public function symbol($symbol): static + { + return $this->state(fn (array $attributes) => [ + 'symbol' => $symbol, + ]); + } + + public function portfolios($portfolio_id): static + { + return $this->state(fn (array $attributes) => [ + 'portfolio_id' => $portfolio_id, + ]); + } + + public function buy(): static + { + return $this->state(fn (array $attributes) => [ + 'transaction_type' => 'BUY', + ]); + } + + public function sell(): static + { + return $this->state(fn (array $attributes) => [ + 'transaction_type' => 'SELL', + ]); + } +} diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index ab7e369..76c211a 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -2,12 +2,9 @@ namespace Database\Factories; -use App\Models\Team; -use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; -use Laravel\Jetstream\Features; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User> @@ -47,25 +44,4 @@ class UserFactory extends Factory 'email_verified_at' => null, ]); } - - /** - * Indicate that the user should have a personal team. - */ - public function withPersonalTeam(?callable $callback = null): static - { - if (! Features::hasTeamFeatures()) { - return $this->state([]); - } - - return $this->has( - Team::factory() - ->state(fn (array $attributes, User $user) => [ - 'name' => $user->name.'\'s Team', - 'user_id' => $user->id, - 'personal_team' => true, - ]) - ->when(is_callable($callback), $callback), - 'ownedTeams' - ); - } } diff --git a/phpunit.xml b/phpunit.xml index 61c031c..6dd89fc 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -5,11 +5,8 @@ colors="true" > - - tests/Unit - - - tests/Feature + + tests diff --git a/tests/Feature/ApiTokenPermissionsTest.php b/tests/ApiTokenPermissionsTest.php similarity index 91% rename from tests/Feature/ApiTokenPermissionsTest.php rename to tests/ApiTokenPermissionsTest.php index da33eb4..70ac458 100644 --- a/tests/Feature/ApiTokenPermissionsTest.php +++ b/tests/ApiTokenPermissionsTest.php @@ -1,6 +1,6 @@ markTestSkipped('API support is not enabled.'); // } - // $this->actingAs($user = User::factory()->withPersonalTeam()->create()); + // $this->actingAs($user = User::factory()->create()); // $token = $user->tokens()->create([ // 'name' => 'Test Token', @@ -41,7 +41,7 @@ class ApiTokenPermissionsTest extends TestCase // $this->markTestSkipped('API support is not enabled.'); // } - // $this->actingAs($user = User::factory()->withPersonalTeam()->create()); + // $this->actingAs($user = User::factory()->create()); // Livewire::test(ApiTokenManager::class) // ->set(['createApiTokenForm' => [ @@ -65,7 +65,7 @@ class ApiTokenPermissionsTest extends TestCase // $this->markTestSkipped('API support is not enabled.'); // } - // $this->actingAs($user = User::factory()->withPersonalTeam()->create()); + // $this->actingAs($user = User::factory()->create()); // $token = $user->tokens()->create([ // 'name' => 'Test Token', diff --git a/tests/Feature/AuthenticationTest.php b/tests/AuthenticationTest.php similarity index 97% rename from tests/Feature/AuthenticationTest.php rename to tests/AuthenticationTest.php index 7bfcae2..97f35d1 100644 --- a/tests/Feature/AuthenticationTest.php +++ b/tests/AuthenticationTest.php @@ -1,6 +1,6 @@ markTestSkipped('Email verification not enabled.'); } - $user = User::factory()->withPersonalTeam()->unverified()->create(); + $user = User::factory()->unverified()->create(); $response = $this->actingAs($user)->get('/email/verify'); diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php deleted file mode 100644 index 8364a84..0000000 --- a/tests/Feature/ExampleTest.php +++ /dev/null @@ -1,19 +0,0 @@ -get('/'); - - $response->assertStatus(200); - } -} diff --git a/tests/Feature/PasswordConfirmationTest.php b/tests/PasswordConfirmationTest.php similarity index 92% rename from tests/Feature/PasswordConfirmationTest.php rename to tests/PasswordConfirmationTest.php index 34c860a..8ba8ad7 100644 --- a/tests/Feature/PasswordConfirmationTest.php +++ b/tests/PasswordConfirmationTest.php @@ -1,6 +1,6 @@ withPersonalTeam()->create(); + $user = User::factory()->create(); $response = $this->actingAs($user)->get('/user/confirm-password'); diff --git a/tests/Feature/PasswordResetTest.php b/tests/PasswordResetTest.php similarity index 99% rename from tests/Feature/PasswordResetTest.php rename to tests/PasswordResetTest.php index a30e8cd..1335434 100644 --- a/tests/Feature/PasswordResetTest.php +++ b/tests/PasswordResetTest.php @@ -1,6 +1,6 @@ assertStatus(200); } - // public function test_registration_screen_cannot_be_rendered_if_support_is_disabled(): void - // { - // if (Features::enabled(Features::registration())) { - // $this->markTestSkipped('Registration support is enabled.'); - // } - - // $response = $this->get('/register'); - - // $response->assertStatus(404); - // } - public function test_new_users_can_register(): void { if (! Features::enabled(Features::registration())) { diff --git a/tests/TestCase.php b/tests/TestCase.php index fe1ffc2..a169a5a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,22 @@ namespace Tests; +use Illuminate\Support\Facades\Artisan; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { - // + protected function setUp(): void + { + parent::setUp(); + + Artisan::call('migrate'); + } + + protected function tearDown(): void + { + parent::tearDown(); + + // + } } diff --git a/tests/TransactionsTest.php b/tests/TransactionsTest.php new file mode 100644 index 0000000..5beb9a2 --- /dev/null +++ b/tests/TransactionsTest.php @@ -0,0 +1,73 @@ +create(); + $this->actingAs($user); + + $transactions = Transaction::factory()->create(); + } +} + + + + + // static::saving(function ($transaction) { + + // if ($transaction->transaction_type == 'SELL') { + + // $transaction->ensureCostBasisIsAddedToSale(); + // } + // }); + + // static::saved(function ($transaction) { + + // $transaction->syncToHolding(); + + // $transaction->refreshMarketData(); + + // cache()->tags(['metrics', $transaction->portfolio_id])->flush(); + // }); + + // public function update() + // { + + // $this->transaction->update($this->validate()); + // // $this->transaction->owner_id = auth()->user()->id; + // $this->transaction->save(); + + // $this->success(__('Transaction updated')); + + // $this->dispatch('toggle-manage-transaction'); + // $this->dispatch('transaction-updated'); + // } + + // public function save() + // { + // $validated = $this->validate(); + + // if (!isset($this->portfolio)) { + // $this->portfolio = Portfolio::find($this->portfolio_id); + // } + + // $transaction = $this->portfolio->transactions()->create($validated); + // $transaction->save(); + + // $this->dispatch('transaction-saved'); + + // $this->success(__('Transaction created'), redirectTo: route('holding.show', ['portfolio' => $this->portfolio->id, 'symbol' => $this->symbol])); + // } diff --git a/tests/Feature/TwoFactorAuthenticationSettingsTest.php b/tests/TwoFactorAuthenticationSettingsTest.php similarity index 98% rename from tests/Feature/TwoFactorAuthenticationSettingsTest.php rename to tests/TwoFactorAuthenticationSettingsTest.php index 1321659..c380b23 100644 --- a/tests/Feature/TwoFactorAuthenticationSettingsTest.php +++ b/tests/TwoFactorAuthenticationSettingsTest.php @@ -1,6 +1,6 @@ assertTrue(true); - } -} diff --git a/tests/Feature/UpdatePasswordTest.php b/tests/UpdatePasswordTest.php similarity index 98% rename from tests/Feature/UpdatePasswordTest.php rename to tests/UpdatePasswordTest.php index 57dbe2d..6f5bbde 100644 --- a/tests/Feature/UpdatePasswordTest.php +++ b/tests/UpdatePasswordTest.php @@ -1,6 +1,6 @@