Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test homework #6

Open
wants to merge 2 commits into
base: exercises
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions tests/Unit/CheckoutServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace StdGroup\Tests\Unit;


use PHPUnit\Framework\TestCase;
use StdGroup\App\CheckoutService;

class CheckoutServiceTest extends TestCase
{
protected $checkoutService;

protected function setUp(): void
{
parent::setUp();
$this->checkoutService = new CheckoutService();
}

/**
* Test calculate shipping fee success
*
* @param $data
* @param $expected
* @dataProvider data_calculate_shipping_fee_successs
*
* @return void
*/
public function test_calculate_shipping_fee_success($data, $expected): void
{
$result = $this->checkoutService->calculateShippingFee($data);
$this->assertEquals($result, $expected);
}


/**
* Test data success.
*
* @return array
*/
public function data_calculate_shipping_fee_successs(): array
{
return [
[
[
'amount' => 10,
'premium_member' => true,
'shipping_express' => true,
],
500
],
[
[
'amount' => 'abcd',
'premium_member' => true,
'shipping_express' => true,
],
500
],
[
[
'amount' => -1000,
'premium_member' => true,
'shipping_express' => true,
],
500
],
[
[
'amount' => 10,
'premium_member' => false,
'shipping_express' => true,
],
1000
],
[
[
'amount' => 10,
'premium_member' => false,
'shipping_express' => false,
],
500
],
[
[
'amount' => 10,
'premium_member' => true,
'shipping_express' => false,
],
0
],
[
[
'amount' => 5005,
'premium_member' => true,
'shipping_express' => true,
],
500
],
[
[
'amount' => 5005,
'premium_member' => false,
'shipping_express' => true,
],
500
],
[
[
'amount' => 5005,
'premium_member' => false,
'shipping_express' => false,
],
0
],
[
[
'amount' => 5005,
'premium_member' => true,
'shipping_express' => false,
],
0
],
];
}
}
95 changes: 95 additions & 0 deletions tests/Unit/UsernameValidationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace StdGroup\Tests\Unit;

use PHPUnit\Framework\TestCase;
use StdGroup\App\UsernameValidation;

class UsernameValidationTest extends TestCase
{
protected $usernameValidation;

protected function setUp(): void
{
parent::setUp();
$this->usernameValidation = new UsernameValidation();
}

/**
* @dataProvider data_test_valid_true
*/
public function test_is_valid_true($username): void
{
$result = $this->usernameValidation->isValid($username);
$this->assertTrue($result);
}

/**
* @dataProvider data_test_valid_false
*/
public function test_is_valid_false($username, $message): void
{
$result = $this->usernameValidation->isValid($username);
$errorMessage = $this->usernameValidation->getMessage();

$this->assertFalse($result);
$this->assertEquals($message, $errorMessage);
}

public function data_test_valid_true(): array
{
return [
[
'1212415411'
],
[
'UserNamwe23'
],
[
'MyNameIs'
],
[
'Cover03924390'
],
[
'Coding-conve'
],
[
'fgdf-gfd-564'
],
[
'KJKDJSKJSK'
]
];
}

public function data_test_valid_false(): array
{
return [
[
'My--name',
'Only single - is allowed'
],
[
'My-name-is-very-very-very-long',
'Maximum length is ' . UsernameValidation::MAX_LENGTH
],
[
'',
'Minimum length is ' . UsernameValidation::MIN_LENGTH
],
[
'-Start-with',
'- cannot appear at begin or end of name'
],
[
'End-with-',
'- cannot appear at begin or end of name'
],
[
'(*(*^*%^&%&^',
'Invalid character. Use only letters, digits and -'
],
];
}
}