Completed
Pull Request — master (#326)
by Damian
02:15
created

FixtureContext::retry()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 2
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Tests\Behat\Context;
4
5
use Behat\Mink\Element\DocumentElement;
6
use Behat\Mink\Element\NodeElement;
7
use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext;
8
9
/**
10
 * Context used to create fixtures in the SilverStripe ORM.
11
 */
12
class FixtureContext extends BaseFixtureContext
13
{
14
15
    /**
16
     * Select a gallery item by type and name
17
     *
18
     * @Given /^I (?:(?:click on)|(?:select)) the "([^"]+)" named "([^"]+)" in the gallery$/
19
     * @param string $type
20
     * @param string $name
21
     */
22
    public function stepISelectGalleryItem($type, $name) {
23
        $item = $this->getGalleryItem($type, $name);
24
        assertNotNull($item, ucfirst($type) . " named $name could not be found");
25
        $item->click();
26
    }
27
28
    /**
29
     * Check the checkbox for a given gallery item
30
     * @Given /^I check the "([^"]+)" named "([^"]+)" in the gallery$/
31
     * @param string $type
32
     * @param string $name
33
     */
34
    public function stepICheckTheGalleryItem($type, $name) {
35
        $item = $this->getGalleryItem($type, $name);
36
        assertNotNull($item, ucfirst($type) . " named $name could not be found");
37
        $checkbox = $item->find('css', 'input[type="checkbox"]');
38
        assertNotNull($checkbox, "Could not find checkbox for file named {$name}");
39
        $checkbox->check();
40
    }
41
42
    /**
43
     * @Then /^I should see the "([^"]+)" named "([^"]+)" in the gallery$/
44
     * @param $type
45
     * @param $name
46
     */
47
    public function iShouldSeeTheGalleryItem($type, $name) {
48
        $item = $this->getGalleryItem($type, $name);
49
        assertNotNull($item, ucfirst($type) . " named {$name} could not be found");
50
    }
51
52
    /**
53
     * @Then /^I should not see the "([^"]+)" named "([^"]+)" in the gallery$/
54
     * @param $type
55
     * @param $name
56
     */
57
    public function iShouldNotSeeTheGalleryItem($type, $name) {
58
        $item = $this->getGalleryItem($type, $name, 0);
59
        assertNull($item, ucfirst($type) . " named {$name} was found when it should not be visible");
60
    }
61
62
    /**
63
     * @Then /^I should see the "([^"]*)" form$/
64
     * @param string $id HTML ID of form
65
     */
66
    public function iShouldSeeTheForm($id)
67
    {
68
        /** @var DocumentElement $page */
69
        $page = $this->getSession()->getPage();
70
        $form = $this->retry(function() use ($page, $id) {
71
            return $page->find('css', "form#{$id}");
72
        });
73
        assertNotNull($form, "form with id $id could not be found");
74
        assertTrue($form->isVisible(), "form with id $id is not visible");
75
    }
76
77
    /**
78
     * Helper for finding items in the visible gallery view
79
     *
80
     * @param string $type type. E.g. 'folder', 'image'
81
     * @param string $name Title of item
82
     * @param int $timeout
83
     * @return NodeElement
84
     */
85
    protected function getGalleryItem($type, $name, $timeout = 3)
86
    {
87
        /** @var DocumentElement $page */
88
        $page = $this->getSession()->getPage();
89
        return $this->retry(function() use ($page, $type, $name) {
90
            return $page->find(
91
                'xpath',
92
                "//div[contains(@class, 'gallery-item--{$type}')]//div[contains(text(), '{$name}')]"
93
            );
94
        }, $timeout);
95
    }
96
97
    /**
98
     * Invoke $try callback for a non-empty result with a given timeout
99
     *
100
     * @param callable $try
101
     * @param int $timeout Number of seconds to retry for
102
     * @return mixed Result of invoking $try, or null if timed out
103
     */
104
    protected function retry($try, $timeout = 3) {
105
        do {
106
            $result = $try();
107
            if ($result) {
108
                return $result;
109
            }
110
            sleep(1);
111
        } while(--$timeout >= 0);
112
        return null;
113
    }
114
}
115