Completed
Push — 4.0 ( 9c8a0d...69eff6 )
by Hideki
04:59 queued 10s
created

AcceptanceTester   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 24
lcom 0
cbo 6
dl 0
loc 197
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getScenario() 0 4 1
A loginAsAdmin() 0 18 3
A logoutAsAdmin() 0 12 2
A goToAdminPage() 0 10 2
A loginAsMember() 0 11 1
A logoutAsMember() 0 11 2
A setStock() 0 35 4
A buyThis() 0 6 1
A makeEmptyCart() 0 6 1
A getLastDownloadFile() 0 25 4
A switchToNewWindow() 0 9 1
A dontSeeElements() 0 10 1
A dragAndDropBy() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
use Codeception\Util\Fixtures;
15
use Eccube\Common\Constant;
16
use Interactions\DragAndDropBy;
17
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
18
19
/**
20
 * Inherited Methods
21
 *
22
 * @method void wantToTest($text)
23
 * @method void wantTo($text)
24
 * @method void execute($callable)
25
 * @method void expectTo($prediction)
26
 * @method void expect($prediction)
27
 * @method void amGoingTo($argumentation)
28
 * @method void am($role)
29
 * @method void lookForwardTo($achieveValue)
30
 * @method void comment($description)
31
 * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = null)
32
 *
33
 * @SuppressWarnings(PHPMD)
34
 */
35
class AcceptanceTester extends \Codeception\Actor
36
{
37
    use _generated\AcceptanceTesterActions;
38
39
    public function getScenario()
40
    {
41
        return $this->scenario;
42
    }
43
44
    public function loginAsAdmin($user = '', $password = '', $dir = '')
45
    {
46
        if (!$user || !$password) {
47
            $account = Fixtures::get('admin_account');
48
            $user = $account['member'];
49
            $password = $account['password'];
50
        }
51
52
        $I = $this;
53
        $this->goToAdminPage($dir);
54
55
        $I->submitForm('#form1', [
56
            'login_id' => $user,
57
            'password' => $password,
58
        ]);
59
60
        $I->see('ホーム', '.c-contentsArea .c-pageTitle > .c-pageTitle__titles');
61
    }
62
63
    public function logoutAsAdmin()
64
    {
65
        $I = $this;
66
        $isLogin = $I->grabTextFrom('header.c-headerBar div.c-headerBar__container a.c-headerBar__userMenu span');
67
        if ($isLogin == '管理者 様') {
68
            $I->click('header.c-headerBar div.c-headerBar__container a.c-headerBar__userMenu');
69
            $I->click('#page_admin_homepage div.popover .popover-body a:last-child');
70
            $config = Fixtures::get('config');
71
            $I->amOnPage('/'.$config['eccube_admin_route'].'/logout');
72
            $I->see('ログイン', '#form1 > button');
73
        }
74
    }
75
76
    public function goToAdminPage($dir = '')
77
    {
78
        $I = $this;
79
        if ($dir == '') {
80
            $config = Fixtures::get('config');
81
            $I->amOnPage('/'.$config['eccube_admin_route']);
82
        } else {
83
            $I->amOnPage('/'.$dir);
84
        }
85
    }
86
87
    public function loginAsMember($email = '', $password = '')
88
    {
89
        $I = $this;
90
        $I->amOnPage('/mypage/login');
91
        $I->submitForm('#login_mypage', [
92
            'login_email' => $email,
93
            'login_pass' => $password,
94
        ]);
95
        $I->see('新着情報', '.ec-secHeading__ja');
96
        $I->see('ログアウト', ['css' => 'div.ec-layoutRole__header > div.ec-headerNaviRole > div.ec-headerNaviRole__right > div.ec-headerNaviRole__nav > div > div:nth-child(3) > a > span']);
97
    }
98
99
    public function logoutAsMember()
100
    {
101
        $I = $this;
102
        $I->amOnPage('/');
103
        $isLogin = $I->grabTextFrom('.ec-headerNaviRole .ec-headerNav .ec-headerNav__item:nth-child(3) a');
104
        if ($isLogin == 'ログアウト') {
105
            $I->wait(1);
106
            $I->click('.ec-headerNaviRole .ec-headerNav .ec-headerNav__item:nth-child(3) a');
107
            $I->see('ログイン', '.ec-headerNaviRole .ec-headerNav .ec-headerNav__item:nth-child(3) a');
108
        }
109
    }
110
111
    public function setStock($pid, $stock = 0)
112
    {
113
        if (!$pid) {
114
            return;
115
        }
116
        $entityManager = Fixtures::get('entityManager');
117
118
        if (!is_array($stock)) {
119
            $pc = $entityManager->getRepository('Eccube\Entity\ProductClass')->findOneBy(['Product' => $pid]);
120
            $pc->setStock($stock);
121
            $pc->setStockUnlimited(Constant::DISABLED);
122
            $ps = $entityManager->getRepository('Eccube\Entity\ProductStock')->findOneBy(['ProductClass' => $pc->getId()]);
123
            $ps->setStock($stock);
124
            $entityManager->persist($pc);
125
            $entityManager->persist($ps);
126
            $entityManager->flush();
127
        } else {
128
            $pcs = $entityManager->getRepository('Eccube\Entity\ProductClass')
129
                ->createQueryBuilder('o')
130
                ->where('o.Product = '.$pid)
131
                ->andwhere('o.ClassCategory1 > 0')
132
                ->getQuery()
133
                ->getResult();
134
            foreach ($pcs as $key => $pc) {
135
                $pc->setStock($stock[$key]);
136
                $pc->setStockUnlimited(Constant::DISABLED);
137
                $pc->setSaleLimit(2);
138
                $ps = $entityManager->getRepository('Eccube\Entity\ProductStock')->findOneBy(['ProductClass' => $pc->getId()]);
139
                $ps->setStock($stock[$key]);
140
                $entityManager->persist($pc);
141
                $entityManager->persist($ps);
142
                $entityManager->flush();
143
            }
144
        }
145
    }
146
147
    public function buyThis($num = 1)
148
    {
149
        $I = $this;
150
        $I->fillField(['id' => 'quantity'], $num);
151
        $I->click('#form1 .btn_area button');
152
    }
153
154
    public function makeEmptyCart()
155
    {
156
        $I = $this;
157
        $I->click('#form_cart .item_box .icon_edit a');
158
        $I->acceptPopup();
159
    }
160
161
    /**
162
     * @param string|$fileNameRegex ファイル名のパターン(CI環境で同時実行したときに区別するため)
163
     *
164
     * @return string ファイルパス
165
     *
166
     * @throws FileNotFoundException 指定したパターンにマッチするファイルがない場合
167
     */
168
    public function getLastDownloadFile($fileNameRegex, $retryCount = 3)
169
    {
170
        $downloadDir = __DIR__.'/_downloads/';
171
        $files = scandir($downloadDir);
172
        $files = array_map(function ($fileName) use ($downloadDir) {
173
            return $downloadDir.$fileName;
174
        }, $files);
175
        $files = array_filter($files, function ($f) use ($fileNameRegex) {
176
            return is_file($f) && preg_match($fileNameRegex, basename($f));
177
        });
178
        usort($files, function ($l, $r) {
179
            return filemtime($l) - filemtime($r);
180
        });
181
182
        if (empty($files)) {
183
            if ($retryCount > 0) {
184
                $this->wait(3);
185
186
                return $this->getLastDownloadFile($fileNameRegex, $retryCount - 1);
187
            }
188
            throw new FileNotFoundException($fileNameRegex);
189
        }
190
191
        return end($files);
192
    }
193
194
    /**
195
     * _blankで開いたウィンドウに切り替え
196
     */
197
    public function switchToNewWindow()
198
    {
199
        $this->wait(1);
200
        $this->executeInSelenium(function ($webdriver) {
201
            $handles = $webdriver->getWindowHandles();
202
            $last_window = end($handles);
203
            $webdriver->switchTo()->window($last_window);
204
        });
205
    }
206
207
    /**
208
     * dontSeeElementが遅いのでJSで存在チェックを行う。
209
     *
210
     * @param array|$arrayOfSelector IDセレクタの配列
211
     */
212
    public function dontSeeElements($arrayOfSelector)
213
    {
214
        $self = $this;
215
        $result = array_filter($arrayOfSelector, function ($element) use ($self) {
216
            $id = $element['id'];
217
218
            return $self->executeJS("return document.getElementById('${id}') != null;");
219
        });
220
        $this->assertTrue(empty($result));
221
    }
222
223
    public function dragAndDropBy($selector, $x_offset, $y_offset)
224
    {
225
        $this->executeInSelenium(function (\Facebook\WebDriver\Remote\RemoteWebDriver $webDriver) use ($selector, $x_offset, $y_offset) {
226
            $node = $webDriver->findElement(WebDriverBy::cssSelector($selector));
227
            $action = new DragAndDropBy($webDriver, $node, $x_offset, $y_offset);
228
            $action->perform();
229
        });
230
    }
231
}
232