Completed
Push — master ( 48a5d3...028360 )
by Aydin
03:14
created

test/Exercise/TimeServerTest.php (9 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 PhpSchool\PhpWorkshop\Solution\SolutionInterface;
17
use PHPUnit_Framework_TestCase;
18
use PhpSchool\LearnYouPhp\Exercise\MyFirstIo;
19
use Symfony\Component\Filesystem\Filesystem;
20
21
/**
22
 * Class TimeServerTest
23
 * @package PhpSchool\LearnYouPhpTest\Exercise
24
 * @author Michael Woodward <[email protected]>
25
 */
26
class TimeServerTest extends PHPUnit_Framework_TestCase
27
{
28
29
    /**
30
     * @var TimeServer
31
     */
32
    private $exercise;
33
34
    /**
35
     * @var TcpSocketFactory
36
     */
37
    private $socketFactory;
38
39
    public function setUp()
40
    {
41
        $this->socketFactory = $this->getMock(TcpSocketFactory::class);
42
        $this->exercise = new TimeServer($this->socketFactory);
43
    }
44
45
    public function testGetters()
46
    {
47
        $this->assertEquals('Time Server', $this->exercise->getName());
48
        $this->assertEquals('Build a Time Server!', $this->exercise->getDescription());
49
50
        $this->assertInstanceOf(SolutionInterface::class, $this->exercise->getSolution());
51
        $this->assertFileExists(realpath($this->exercise->getProblem()));
52
        $this->assertNull($this->exercise->tearDown());
53
    }
54
55
    public function testFailureIsReturnedIfCannotConnect()
56
    {
57
        $this->socketFactory
0 ignored issues
show
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...
58
            ->expects($this->once())
59
            ->method('createClient')
60
            ->with('127.0.0.1', $this->logicalAnd(
61
                $this->greaterThan(1024),
62
                $this->lessThan(655356)
63
            ))
64
            ->will($this->returnValue(new Client('tcp://127.0.0.1:655355')));
65
        
66
        $failure = $this->exercise->check('program.php');
67
        
68
        $this->assertInstanceOf(Failure::class, $failure);
69
70
71
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
72
            $reason  = '/^Client returns an error \(number \d+\): No connection could be made because';
73
            $reason .= ' the target machine actively refused it\.\r\n';
74
            $reason .= ' while trying to join tcp:\/\/127\.0\.0\.1:655355\.$/';
75
        } else {
76
            $reason  = '/^Client returns an error \(number \d+\): Connection refused';
77
            $reason .= ' while trying to join tcp:\/\/127\.0\.0\.1:655355\.$/';
78
        }
79
80
        $this->assertRegExp($reason, $failure->getReason());
0 ignored issues
show
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...
81
        $this->assertEquals('Time Server', $failure->getCheckName());
82
    }
83
    
84 View Code Duplication
    public function testFailureIsReturnedIfOutputWasNotCorrect()
0 ignored issues
show
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...
85
    {
86
        $e = new TimeServer(new TcpSocketFactory);
87
        $failure = $e->check(__DIR__ . '/../res/time-server/solution-wrong.php');
88
        $this->assertInstanceOf(StdOutFailure::class, $failure);
89
        $this->assertNotEquals($failure->getExpectedOutput(), $failure->getActualOutput());
0 ignored issues
show
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...
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...
90
        $this->assertEquals('Time Server', $failure->getCheckName());
91
    }
92
93
    public function testSuccessIsReturnedIfOutputIsCorrect()
94
    {
95
        $e = new TimeServer(new TcpSocketFactory);
96
        $success = $e->check(__DIR__ . '/../res/time-server/solution.php');
97
        $this->assertInstanceOf(Success::class, $success);
98
    }
99
100 View Code Duplication
    public function testProcessIsStoppedIfStillRunning()
0 ignored issues
show
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...
101
    {
102
        $e = new TimeServer(new TcpSocketFactory);
103
        $failure = $e->check(__DIR__ . '/../res/time-server/solution-keep-running.php');
104
        $this->assertInstanceOf(Failure::class, $failure);
105
        $this->assertEquals('', $failure->getReason());
0 ignored issues
show
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...
106
        $this->assertEquals('Time Server', $failure->getCheckName());
107
    }
108
109 View Code Duplication
    public function testFailureIsReturnedIfProcessWasNotSuccessful()
0 ignored issues
show
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...
110
    {
111
        $e = new TimeServer(new TcpSocketFactory);
112
        $failure = $e->check(__DIR__ . '/../res/time-server/solution-bad-exit.php');
113
        $this->assertInstanceOf(Failure::class, $failure);
114
        $this->assertEquals('', $failure->getReason());
0 ignored issues
show
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...
115
        $this->assertEquals('Time Server', $failure->getCheckName());
116
    }
117
}
118