Completed
Push — dev/store-tests ( fd7aaa...348834 )
by Kiyotaka
05:41
created

EA10OwnersStoreCest::インストール()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 83
rs 8.3709
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2018 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
use Codeception\Util\Fixtures;
25
use Doctrine\ORM\EntityManager;
26
use Eccube\Entity\Plugin;
27
use Eccube\Repository\PluginRepository;
28
use Page\Admin\PluginManagePage;
29
use Page\Admin\PluginSearchPage;
30
31
class EA10OwnersStoreCest
32
{
33
34
    /** @var EntityManager */
35
    private $em;
36
37
    /** @var \Doctrine\DBAL\Connection */
38
    private $conn;
39
40
    /** @var PluginRepository */
41
    private $pluginRepository;
42
43
    public function _before(\AcceptanceTester $I)
44
    {
45
        $I->loginAsAdmin();
46
47
        $this->em = Fixtures::get('entityManager');
48
        $this->conn = $this->em->getConnection();
49
        $this->pluginRepository = $this->em->getRepository(Plugin::class);
50
    }
51
52
    public function インストール(\AcceptanceTester $I) {
53
54
        /*
55
         * インストール
56
         */
57
58
        $ManagePage = PluginSearchPage::go($I)
59
            ->入手する('SamplePayment')
60
            ->インストール();
61
62
        $I->assertFalse($this->tableExists('plg_sample_payment_config'));
63
        $I->assertFalse($this->columnExists('dtb_customer', 'sample_payment_cards'));
64
65
        $Plugin = $this->pluginRepository->findByCode('SamplePayment');
66
        $I->assertFalse($Plugin->isInitialized(), '初期化されていない');
67
        $I->assertFalse($Plugin->isEnabled(), '有効化されていない');
68
69
        /*
70
         * 有効化
71
         */
72
        $ManagePage->ストアプラグイン_有効化('SamplePayment');
73
74
        $I->see('「EC-CUBE Payment Sample Plugin」を有効にしました。', PluginManagePage::完了メーッセージ);
75
        $I->assertTrue($this->tableExists('plg_sample_payment_config'));
76
        $I->assertTrue($this->columnExists('dtb_customer', 'sample_payment_cards'));
77
78
79
        $this->em->refresh($Plugin);
0 ignored issues
show
Bug introduced by
It seems like $Plugin defined by $this->pluginRepository-...ByCode('SamplePayment') on line 65 can be null; however, Doctrine\ORM\EntityManager::refresh() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
80
        $I->assertTrue($Plugin->isInitialized(), '初期化されている');
81
        $I->assertTrue($Plugin->isEnabled(), '有効化されている');
82
83
        /*
84
         * 無効化
85
         */
86
        $ManagePage->ストアプラグイン_無効化('SamplePayment');
87
88
        $I->see('「EC-CUBE Payment Sample Plugin」を無効にしました。', PluginManagePage::完了メーッセージ);
89
        $I->assertTrue($this->tableExists('plg_sample_payment_config'));
90
        $I->assertTrue($this->columnExists('dtb_customer', 'sample_payment_cards'));
91
92
        $this->em->refresh($Plugin);
0 ignored issues
show
Bug introduced by
It seems like $Plugin defined by $this->pluginRepository-...ByCode('SamplePayment') on line 65 can be null; however, Doctrine\ORM\EntityManager::refresh() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
93
        $I->assertTrue($Plugin->isInitialized(), '初期化されている');
94
        $I->assertFalse($Plugin->isEnabled(), '無効化されている');
95
96
        /*
97
         * 再度有効化
98
         */
99
        $ManagePage->ストアプラグイン_有効化('SamplePayment');
100
101
        $I->see('「EC-CUBE Payment Sample Plugin」を有効にしました。', PluginManagePage::完了メーッセージ);
102
        $I->assertTrue($this->tableExists('plg_sample_payment_config'));
103
        $I->assertTrue($this->columnExists('dtb_customer', 'sample_payment_cards'));
104
105
        $this->em->refresh($Plugin);
0 ignored issues
show
Bug introduced by
It seems like $Plugin defined by $this->pluginRepository-...ByCode('SamplePayment') on line 65 can be null; however, Doctrine\ORM\EntityManager::refresh() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
106
        $I->assertTrue($Plugin->isInitialized(), '初期化されている');
107
        $I->assertTrue($Plugin->isEnabled(), '有効化されている');
108
109
110
        /*
111
         * 再度無効化
112
         */
113
        $ManagePage->ストアプラグイン_無効化('SamplePayment');
114
115
        $I->see('「EC-CUBE Payment Sample Plugin」を無効にしました。', PluginManagePage::完了メーッセージ);
116
        $I->assertTrue($this->tableExists('plg_sample_payment_config'));
117
        $I->assertTrue($this->columnExists('dtb_customer', 'sample_payment_cards'));
118
119
        $this->em->refresh($Plugin);
0 ignored issues
show
Bug introduced by
It seems like $Plugin defined by $this->pluginRepository-...ByCode('SamplePayment') on line 65 can be null; however, Doctrine\ORM\EntityManager::refresh() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
120
        $I->assertTrue($Plugin->isInitialized(), '初期化されている');
121
        $I->assertFalse($Plugin->isEnabled(), '無効化されている');
122
123
        /*
124
         * 削除
125
         */
126
        $ManagePage->ストアプラグイン_削除('SamplePayment');
127
128
        $I->assertFalse($this->tableExists('plg_sample_payment_config'));
129
        $I->assertFalse($this->columnExists('dtb_customer', 'sample_payment_cards'));
130
131
        $this->em->refresh($Plugin);
0 ignored issues
show
Bug introduced by
It seems like $Plugin defined by $this->pluginRepository-...ByCode('SamplePayment') on line 65 can be null; however, Doctrine\ORM\EntityManager::refresh() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
132
        $Plugin = $this->pluginRepository->findByCode('SamplePayment');
133
        $I->assertNull($Plugin);
134
    }
135
136
    private function tableExists($tableName)
137
    {
138
        return $this->conn->executeQuery("SELECT count(*) FROM information_schema.columns WHERE table_name = '${tableName}';")->fetch()['count'] > 0;
139
    }
140
141
    private function columnExists($tableName, $columnName)
142
    {
143
        return $this->conn->executeQuery("SELECT count(*) FROM information_schema.columns WHERE table_name = '${tableName}' AND column_name = '${columnName}';")->fetch()['count'] == 1;
144
    }
145
}