Completed
Push — master ( 3338f6...2ff2f4 )
by Aydin
02:03
created

testFailureIsReturnedIfProcessWasNotSuccessful()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
rs 9.4286
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
4
namespace PhpSchool\LearnYouPhpTest\Exercise;
5
6
use Faker\Factory;
7
use Faker\Generator;
8
use Hoa\Core\Exception\Exception;
9
use Hoa\Socket\Client;
10
use PhpSchool\LearnYouPhp\Exercise\ArrayWeGo;
11
use PhpSchool\LearnYouPhp\Exercise\TimeServer;
12
use PhpSchool\LearnYouPhp\TcpSocketFactory;
13
use PhpSchool\PhpWorkshop\Result\Failure;
14
use PhpSchool\PhpWorkshop\Result\StdOutFailure;
15
use PhpSchool\PhpWorkshop\Result\Success;
16
use PHPUnit_Framework_TestCase;
17
use PhpSchool\LearnYouPhp\Exercise\MyFirstIo;
18
use Symfony\Component\Filesystem\Filesystem;
19
20
/**
21
 * Class TimeServerTest
22
 * @package PhpSchool\LearnYouPhpTest\Exercise
23
 * @author Michael Woodward <[email protected]>
24
 */
25
class TimeServerTest extends PHPUnit_Framework_TestCase
26
{
27
28
    /**
29
     * @var TimeServer
30
     */
31
    private $exercise;
32
33
    /**
34
     * @var TcpSocketFactory
35
     */
36
    private $socketFactory;
37
38
    public function setUp()
39
    {
40
        $this->socketFactory = $this->getMock(TcpSocketFactory::class);
41
        $this->exercise = new TimeServer($this->socketFactory);
42
    }
43
44
    public function testGetters()
45
    {
46
        $this->assertEquals('Time Server', $this->exercise->getName());
47
        $this->assertEquals('Build a Time Server!', $this->exercise->getDescription());
48
49
        $this->assertFileExists(realpath($this->exercise->getSolution()));
50
        $this->assertFileExists(realpath($this->exercise->getProblem()));
51
        $this->assertNull($this->exercise->tearDown());
52
    }
53
54
    public function testFailureIsReturnedIfCannotConnect()
55
    {
56
        $this->socketFactory
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<PhpSchool\LearnYouPhp\TcpSocketFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
            ->expects($this->once())
58
            ->method('createClient')
59
            ->with('127.0.0.1', $this->logicalAnd(
60
                $this->greaterThan(1024),
61
                $this->lessThan(655356)
62
            ))
63
            ->will($this->returnValue(new Client('tcp://127.0.0.1:655355')));
64
        
65
        $failure = $this->exercise->check('program.php');
66
        
67
        $this->assertInstanceOf(Failure::class, $failure);
68
69
70
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
71
            $reason  = '/^Client returns an error \(number \d+\): No connection could be made because';
72
            $reason .= ' the target machine actively refused it\.\r\n';
73
            $reason .= ' while trying to join tcp:\/\/127\.0\.0\.1:655355\.$/';
74
        } else {
75
            $reason  = '/^Client returns an error \(number \d+\): Connection refused';
76
            $reason .= ' while trying to join tcp:\/\/127\.0\.0\.1:655355\.$/';
77
        }
78
79
        $this->assertRegExp($reason, $failure->getReason());
0 ignored issues
show
Bug introduced by
The method getReason does only exist in PhpSchool\PhpWorkshop\Result\Failure, but not in PhpSchool\PhpWorkshop\Re...Workshop\Result\Success.

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...
80
        $this->assertEquals('Time Server', $failure->getCheckName());
81
    }
82
    
83 View Code Duplication
    public function testFailureIsReturnedIfOutputWasNotCorrect()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        $e = new TimeServer(new TcpSocketFactory);
86
        $failure = $e->check(__DIR__ . '/../res/time-server/solution-wrong.php');
87
        $this->assertInstanceOf(StdOutFailure::class, $failure);
88
        $this->assertNotEquals($failure->getExpectedOutput(), $failure->getActualOutput());
0 ignored issues
show
Bug introduced by
The method getExpectedOutput does only exist in PhpSchool\PhpWorkshop\Result\StdOutFailure, but not in PhpSchool\PhpWorkshop\Re...Workshop\Result\Success.

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...
Bug introduced by
The method getActualOutput does only exist in PhpSchool\PhpWorkshop\Result\StdOutFailure, but not in PhpSchool\PhpWorkshop\Re...Workshop\Result\Success.

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...
89
        $this->assertEquals('Time Server', $failure->getCheckName());
90
    }
91
92
    public function testSuccessIsReturnedIfOutputIsCorrect()
93
    {
94
        $e = new TimeServer(new TcpSocketFactory);
95
        $success = $e->check(__DIR__ . '/../res/time-server/solution.php');
96
        $this->assertInstanceOf(Success::class, $success);
97
    }
98
99 View Code Duplication
    public function testProcessIsStoppedIfStillRunning()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $e = new TimeServer(new TcpSocketFactory);
102
        $failure = $e->check(__DIR__ . '/../res/time-server/solution-keep-running.php');
103
        $this->assertInstanceOf(Failure::class, $failure);
104
        $this->assertEquals('', $failure->getReason());
0 ignored issues
show
Bug introduced by
The method getReason does only exist in PhpSchool\PhpWorkshop\Result\Failure, but not in PhpSchool\PhpWorkshop\Re...Workshop\Result\Success.

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...
105
        $this->assertEquals('Time Server', $failure->getCheckName());
106
    }
107
108 View Code Duplication
    public function testFailureIsReturnedIfProcessWasNotSuccessful()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
        $e = new TimeServer(new TcpSocketFactory);
111
        $failure = $e->check(__DIR__ . '/../res/time-server/solution-bad-exit.php');
112
        $this->assertInstanceOf(Failure::class, $failure);
113
        $this->assertEquals('', $failure->getReason());
0 ignored issues
show
Bug introduced by
The method getReason does only exist in PhpSchool\PhpWorkshop\Result\Failure, but not in PhpSchool\PhpWorkshop\Re...Workshop\Result\Success.

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...
114
        $this->assertEquals('Time Server', $failure->getCheckName());
115
    }
116
}
117