Completed
Push — 1.x ( b07707...ef4154 )
by Joel
05:55 queued 03:08
created

ExecManagerTest::createContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4286
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Docker\Tests\Manager;
4
5
use Docker\API\Model\ContainerConfig;
6
use Docker\API\Model\ExecConfig;
7
use Docker\API\Model\ExecStartConfig;
8
use Docker\Manager\ContainerManager;
9
use Docker\Manager\ExecManager;
10
use Docker\Tests\TestCase;
11
12
class ExecManagerTest extends TestCase
13
{
14
    /**
15
     * Return the container manager
16
     *
17
     * @return ExecManager
18
     */
19
    private function getManager()
20
    {
21
        return self::getDocker()->getExecManager();
22
    }
23
24
    public function testStartStream()
25
    {
26
        $createContainerResult = $this->createContainer();
27
28
        $execConfig = new ExecConfig();
29
        $execConfig->setAttachStdout(true);
30
        $execConfig->setAttachStderr(true);
31
        $execConfig->setCmd(["echo", "output"]);
32
33
        $execCreateResult = $this->getManager()->create($createContainerResult->getId(), $execConfig);
34
35
        $execStartConfig = new ExecStartConfig();
36
        $execStartConfig->setDetach(false);
37
        $execStartConfig->setTty(false);
38
39
        $stream = $this->getManager()->start($execCreateResult->getId(), $execStartConfig, [], ExecManager::FETCH_STREAM);
0 ignored issues
show
Bug introduced by
The method getId does only exist in Docker\API\Model\ExecCreateResult, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
40
41
        $this->assertInstanceOf('Docker\Stream\DockerRawStream', $stream);
42
43
        $stdoutFull = "";
44
        $stream->onStdout(function ($stdout) use (&$stdoutFull) {
0 ignored issues
show
Bug introduced by
The method onStdout does only exist in Docker\Stream\DockerRawStream, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
45
            $stdoutFull .= $stdout;
46
        });
47
        $stream->wait();
0 ignored issues
show
Bug introduced by
The method wait does only exist in Docker\Stream\DockerRawStream, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
48
49
        $this->assertEquals("output\n", $stdoutFull);
50
51
        self::getDocker()->getContainerManager()->kill($createContainerResult->getId(), [
52
            'signal' => 'SIGKILL'
53
        ]);
54
    }
55
56
    private function createContainer()
57
    {
58
        $containerConfig = new ContainerConfig();
59
        $containerConfig->setImage('busybox:latest');
60
        $containerConfig->setCmd(['sh']);
61
        $containerConfig->setOpenStdin(true);
62
        $containerConfig->setLabels(new \ArrayObject(['docker-php-test' => 'true']));
63
64
        $containerCreateResult = self::getDocker()->getContainerManager()->create($containerConfig);
65
        self::getDocker()->getContainerManager()->start($containerCreateResult->getId());
66
67
        return $containerCreateResult;
68
    }
69
}
70