Skip to content

Commit

Permalink
Merge pull request #310 from nanasess/add-sc-customer-test
Browse files Browse the repository at this point in the history
SC_Customer のテストケースを追加
  • Loading branch information
chihiro-adachi committed Sep 30, 2019
2 parents b3d4808 + 4d7d0c5 commit 4389463
Show file tree
Hide file tree
Showing 2 changed files with 245 additions and 20 deletions.
79 changes: 59 additions & 20 deletions data/class/SC_Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,26 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

/* [名称] SC_Customer
* [概要] 会員管理クラス
/**
* 会員管理クラス
*/
class SC_Customer
{
/** 会員情報 */
/**
* 会員情報
* @var array
*/
public $customer_data;

/**
* @param string $email
* @param string $pass
* メールアドレスとパスワードを使用して認証結果を返す.
*
* 認証に成功した場合は SC_Customer::customer_data に会員情報を格納する
*
* @param string $email メールアドレス
* @param string $pass パスワード
* @param bool $mobile email_mobile も検索対象とする場合 true
* @return bool
*/
public function getCustomerDataFromEmailPass($pass, $email, $mobile = false)
{
Expand Down Expand Up @@ -96,6 +105,7 @@ public function checkMobilePhoneId()
* @param string $pass パスワード
* @return boolean 該当する会員が存在し、パスワードが合っている場合は true、
* それ以外の場合は false を返す。
* @deprecated
*/
public function getCustomerDataFromMobilePhoneIdPass($pass)
{
Expand Down Expand Up @@ -129,6 +139,7 @@ public function getCustomerDataFromMobilePhoneIdPass($pass)
* 携帯端末IDを登録する。
*
* @return void
* @deprecated
*/
public function updateMobilePhoneId()
{
Expand All @@ -148,38 +159,48 @@ public function updateMobilePhoneId()
$this->customer_data['mobile_phone_id'] = $_SESSION['mobile']['phone_id'];
}

// パスワードを確認せずにログイン
/**
* パスワードを確認せずにログイン
*
* @param string $email メールアドレス
*/
public function setLogin($email)
{
// 本登録された会員のみ
$sql = 'SELECT * FROM dtb_customer WHERE (email = ? OR email_mobile = ?) AND del_flg = 0 AND status = 2';
$objQuery = SC_Query_Ex::getSingletonInstance();
$result = $objQuery->getAll($sql, array($email, $email));
$data = isset($result[0]) ? $result[0] : '';
$data = isset($result[0]) ? $result[0] : array();
$this->customer_data = $data;
$this->startSession();
}

// セッション情報を最新の情報に更新する
/**
* セッション情報を最新の情報に更新する
*/
public function updateSession()
{
$sql = 'SELECT * FROM dtb_customer WHERE customer_id = ? AND del_flg = 0';
$customer_id = $this->getValue('customer_id');
$objQuery = SC_Query_Ex::getSingletonInstance();
$arrRet = $objQuery->getAll($sql, array($customer_id));
$this->customer_data = isset($arrRet[0]) ? $arrRet[0] : '';
$this->customer_data = isset($arrRet[0]) ? $arrRet[0] : array();
$_SESSION['customer'] = $this->customer_data;
}

// ログイン情報をセッションに登録し、ログに書き込む
/**
* ログイン情報をセッションに登録し、ログに書き込む
*/
public function startSession()
{
$_SESSION['customer'] = $this->customer_data;
// セッション情報の保存
GC_Utils_Ex::gfPrintLog('access : user='.$this->customer_data['customer_id'] ."\t".'ip='. $this->getRemoteHost(), CUSTOMER_LOG_REALFILE, false);
}

// ログアウト $_SESSION['customer']を解放し、ログに書き込む
/**
* ログアウト $_SESSION['customer']を解放し、ログに書き込む
*/
public function EndSession()
{
// セッション情報破棄の前にcustomer_idを保存
Expand All @@ -200,7 +221,12 @@ public function EndSession()
GC_Utils_Ex::gfPrintLog($log, CUSTOMER_LOG_REALFILE, false);
}

// ログインに成功しているか判定する。
/**
* ログインに成功しているか判定する。
*
* @param bool $dont_check_email_mobile
* @return bool ログインに成功している場合は true
*/
public function isLoginSuccess($dont_check_email_mobile = false)
{
// ログイン時のメールアドレスとDBのメールアドレスが一致している場合
Expand All @@ -225,7 +251,12 @@ public function isLoginSuccess($dont_check_email_mobile = false)
return false;
}

// パラメーターの取得
/**
* パラメーターの取得
*
* @param string $keyname パラメーターのキー名
* @return string|int|null パラメータの値
*/
public function getValue($keyname)
{
// ポイントはリアルタイム表示
Expand All @@ -240,9 +271,8 @@ public function getValue($keyname)
}
}

// パラメーターのセット

/**
* パラメーターのセット
* @param string $keyname
* @param string $val
*/
Expand All @@ -251,10 +281,11 @@ public function setValue($keyname, $val)
$_SESSION['customer'][$keyname] = $val;
}

// パラメーターがNULLかどうかの判定

/**
* パラメーターがNULLかどうかの判定
*
* @param string $keyname
* @return bool
*/
public function hasValue($keyname)
{
Expand All @@ -265,7 +296,11 @@ public function hasValue($keyname)
return false;
}

// 誕生日月であるかどうかの判定
/**
* 誕生日月であるかどうかの判定
*
* @return bool
*/
public function isBirthMonth()
{
if (isset($_SESSION['customer']['birth'])) {
Expand Down Expand Up @@ -300,7 +335,11 @@ public function getRemoteHost()
}
}

//受注関連の会員情報を更新
/**
* 受注関連の会員情報を更新
*
* @param int $customer_id
*/
public function updateOrderSummary($customer_id)
{
$objQuery = SC_Query_Ex::getSingletonInstance();
Expand Down Expand Up @@ -329,7 +368,7 @@ public function updateOrderSummary($customer_id)
*
* @param string $login_email ログインメールアドレス
* @param string $login_pass ログインパスワード
* @return boolean|null ログインに成功した場合 true; 失敗した場合 false
* @return bool ログインに成功した場合 true; 失敗した場合 false
*/
public function doLogin($login_email, $login_pass)
{
Expand Down
186 changes: 186 additions & 0 deletions tests/class/SC_CustomerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php

class SC_CustomerTest extends Common_TestCase
{
/** @var int */
protected $customer_id;

/** @var Faker\Generator $faker */
protected $faker;

/** @var FixtureGenerator */
protected $objGenerator;

/** @var string */
protected $email;

/** @var array */
protected $arrCustomer;

/** @var SC_Customer_Ex */
protected $objCustomer;

protected function setUp()
{
parent::setUp();
$this->objGenerator = new FixtureGenerator($this->objQuery);
$this->faker = Faker\Factory::create('ja_JP');
$this->setUpCustomer();
}

public function testGetCustomerDataFromEmailPass()
{
$this->assertTrue($this->objCustomer->getCustomerDataFromEmailPass('password', $this->email, true));

$this->expected = $this->arrCustomer;
$this->actual = $this->objCustomer->customer_data;
$this->verify();
}

public function testGetCustomerDataFromEmailPassWithFailure()
{
$this->assertFalse($this->objCustomer->getCustomerDataFromEmailPass('XXXXX', $this->email, true));
$this->assertNull($this->objCustomer->customer_data);
}

public function testGetCustomerDataFromEmailPassWithFailure2()
{
$this->assertFalse($this->objCustomer->getCustomerDataFromEmailPass('password', 'bad@example.com'));
$this->assertNull($this->objCustomer->customer_data);
}

public function testSetLogin()
{
$this->objCustomer->setLogin($this->email);
$this->assertNotEmpty($this->objCustomer->customer_data);
$this->assertTrue(is_array($this->objCustomer->customer_data));
$this->expected = $this->email;
$this->actual = $this->objCustomer->getValue('email');
$this->verify();
}

public function testSetLoginWithFailure()
{
$this->objCustomer->setLogin('XXX');
$this->assertEmpty($this->objCustomer->customer_data);
$this->assertTrue(is_array($this->objCustomer->customer_data));
}

public function testUpdateSession()
{
// do login
$this->objCustomer->setLogin($this->email);
$this->assertNotEmpty($this->objCustomer->customer_data);

// leave to customer
$this->objQuery->update('dtb_customer', ['del_flg' => 1], 'customer_id = ?', [$this->customer_id]);
$this->objCustomer->updateSession();
$this->assertEmpty($this->objCustomer->customer_data);
}

public function testEndSession()
{
// do login
$this->objCustomer->setLogin($this->email);
$this->assertNotEmpty($this->objCustomer->customer_data);

$this->objCustomer->EndSession();
$this->assertFalse($this->objCustomer->isLoginSuccess());
}

public function testIsLoginSuccess()
{
$this->assertFalse($this->objCustomer->isLoginSuccess());
$this->objCustomer->setLogin($this->email);
$this->assertTrue($this->objCustomer->isLoginSuccess());

$this->objQuery->update(
'dtb_customer',
['email' => microtime(true).'.'.$this->faker->safeEmail],
'customer_id = ?',
[$this->customer_id]
);
$this->assertFalse($this->objCustomer->isLoginSuccess());
}

public function testGetValue()
{
$this->objCustomer->setLogin($this->email);
$this->expected = $this->arrCustomer['point'];
$this->actual = $this->objCustomer->getValue('point');
$this->verify();

$this->assertEmpty($this->objCustomer->getValue('XXXXX'));
}

public function testSetValue()
{
$value = $this->faker->word;
$this->objCustomer->setLogin($this->email);
$this->assertFalse($this->objCustomer->hasValue('test'));

$this->objCustomer->setValue('test', $value);
$this->assertTrue($this->objCustomer->hasValue('test'));
$this->expected = $value;
$this->actual = $this->objCustomer->getValue('test');
$this->verify();
}

public function testIsBirth()
{
$this->objQuery->update(
'dtb_customer',
['birth' => date('Y-m-d H:i:s')],
'customer_id = ?',
[$this->customer_id]
);
$this->objCustomer->setLogin($this->email);
$this->assertTrue($this->objCustomer->isBirthMonth());
}

public function testIsBirthWithFalse()
{
$this->objQuery->update(
'dtb_customer',
['birth' => null],
'customer_id = ?',
[$this->customer_id]
);
$this->objCustomer->setLogin($this->email);
$this->assertFalse($this->objCustomer->isBirthMonth());
}

public function testGetRemoteHost()
{
$this->assertEmpty($this->objCustomer->getRemoteHost());
$_SERVER['REMOTE_ADDR'] = $this->faker->ipv4;
$this->assertEquals($_SERVER['REMOTE_ADDR'], $this->objCustomer->getRemoteHost());
$_SERVER['REMOTE_HOST'] = $this->faker->domainName;
$this->assertEquals($_SERVER['REMOTE_HOST'], $this->objCustomer->getRemoteHost());
}

public function testUpdateOrderSummary()
{
$this->assertEquals(0, $this->arrCustomer['buy_total']);
$this->assertEquals(0, $this->arrCustomer['buy_times']);
$this->assertEquals('', $this->arrCustomer['last_buy_date']);
$this->assertEquals('', $this->arrCustomer['first_buy_date']);

$this->objGenerator->createOrder($this->customer_id);
$this->objCustomer->updateOrderSummary($this->customer_id);
$this->objCustomer->setLogin($this->email);

$this->assertNotEquals(0, $this->objCustomer->getValue('buy_total'));
$this->assertNotEquals(0, $this->objCustomer->getValue('buy_times'));
$this->assertNotEquals('', $this->objCustomer->getValue('last_buy_date'));
$this->assertNotEquals('', $this->objCustomer->getValue('first_buy_date'));
}

private function setUpCustomer()
{
$this->email = $this->faker->safeEmail;
$this->customer_id = $this->objGenerator->createCustomer($this->email);
$this->arrCustomer = $this->objQuery->getRow('*', 'dtb_customer', 'customer_id = ?', [$this->customer_id]);
$this->objCustomer = new SC_Customer_Ex();
}
}

0 comments on commit 4389463

Please sign in to comment.