Passed
Branch ci (072b24)
by litefeel
05:02
created

Writing_On_GitHub_Fetch_Client_Test   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 144
rs 10
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A test_should_fail_if_cant_fetch_tree() 0 5 1
A test_should_fail_if_malformed_repo() 0 4 1
A test_should_fail_if_missing_repo() 0 5 1
A test_should_return_files_with_tree() 0 6 1
A test_should_fail_if_missing_token() 0 5 1
A malformed_repo() 0 5 1
A setUp() 0 4 1
A test_should_return_commit_with_no_blobs_if_api_fails() 0 9 2
A test_compare() 0 6 1
1
<?php
2
use Writing_On_GitHub_Base_Client as Base;
3
4
/**
5
 * @group api
6
 */
7
class Writing_On_GitHub_Fetch_Client_Test extends Writing_On_GitHub_Base_Client_Test {
8
9
    public function setUp() {
0 ignored issues
show
Coding Style introduced by
The function name setUp is in camel caps, but expected set_up instead as per the coding standard.
Loading history...
10
        parent::setUp();
11
12
        $this->fetch = new Writing_On_GitHub_Fetch_Client( $this->app );
0 ignored issues
show
Bug introduced by
It seems like $this->app can also be of type Mockery\Mock; however, parameter $app of Writing_On_GitHub_Fetch_Client::__construct() does only seem to accept Writing_On_GitHub, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

12
        $this->fetch = new Writing_On_GitHub_Fetch_Client( /** @scrutinizer ignore-type */ $this->app );
Loading history...
13
    }
14
15
    public function test_should_fail_if_missing_token() {
16
        delete_option( Base::TOKEN_OPTION_KEY );
17
18
        $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() );
19
        $this->assertSame( 'missing_token', $error->get_error_code() );
20
    }
21
22
    public function test_should_fail_if_missing_repo() {
23
        delete_option( Base::REPO_OPTION_KEY );
24
25
        $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() );
26
        $this->assertSame( 'missing_repository', $error->get_error_code() );
27
    }
28
29
    public function test_should_fail_if_malformed_repo() {
30
        // If you find a particular name passing that shouldn't,
31
        // add it to the list here and make it pass.
32
        $this->malformed_repo( 'repositoryname' );
33
    }
34
35
    public function test_compare() {
36
        $this->set_get_trees( true, 'master' );
37
        $this->set_get_compare( true );
38
        $infos = $this->fetch->compare( '861f87e8851b8debb78db548269d29f8da4d94ac' );
39
        $this->assertCount( 1, $infos );
40
        $this->assertInstanceOf( 'Writing_On_GitHub_File_Info', $infos[0] );
41
    }
42
43
    public function test_should_return_files_with_tree() {
44
        // $this->set_get_refs_heads_master( true );
45
        // $this->set_get_commits( true );
46
        $this->set_get_trees( true, 'master' );
47
48
        $this->assertCount( 3, $this->fetch->tree_recursive() );
49
    }
50
51
    public function test_should_fail_if_cant_fetch_tree() {
52
        $this->set_get_trees( false, 'master' );
53
54
        $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() );
55
        $this->assertSame( '422_unprocessable_entity', $error->get_error_code() );
56
    }
57
58
    public function test_should_return_commit_with_no_blobs_if_api_fails() {
59
        $this->set_get_trees( true, 'master' );
60
        $this->set_get_blobs( false );
61
62
        $this->assertCount( 3, $files = $this->fetch->tree_recursive() );
63
64
        foreach ( $files as $file ) {
65
            $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->blob( $file ) );
66
            $this->assertSame( '404_not_found', $error->get_error_code() );
0 ignored issues
show
Bug introduced by
The method get_error_code() does not exist on Writing_On_GitHub_Blob. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
            $this->assertSame( '404_not_found', $error->/** @scrutinizer ignore-call */ get_error_code() );

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...
67
        }
68
    }
69
70
    // public function test_should_return_and_validate_full_commit() {
71
    //  $this->set_get_refs_heads_master( true );
72
    //  $this->set_get_commits( true );
73
    //  $this->set_get_trees( true );
74
    //  $this->set_get_blobs( true );
75
    //  $this->api_cache
76
    //      ->shouldReceive( 'set_blob' )
77
    //      ->times( 3 )
78
    //      ->with(
79
    //          Mockery::anyOf(
80
    //              '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25',
81
    //              '8d9b2e6fd93761211dc03abd71f4a9189d680fd0',
82
    //              '2d73165945b0ccbe4932f1363457986b0ed49f19'
83
    //          ),
84
    //          Mockery::type( 'Writing_On_GitHub_Blob' )
85
    //      )
86
    //      ->andReturnUsing( function ( $sha, $blob ) {
87
    //          return $blob;
88
    //      } );
89
    //  $this->api_cache
90
    //      ->shouldReceive( 'set_tree' )
91
    //      ->once()
92
    //      ->with( '9108868e3800bec6763e51beb0d33e15036c3626', Mockery::type( 'Writing_On_GitHub_Tree' ) )
93
    //      ->andReturnUsing( function ( $sha, $tree ) {
94
    //          return $tree;
95
    //      } );
96
    //  $this->api_cache
97
    //      ->shouldReceive( 'set_commit' )
98
    //      ->once()
99
    //      ->with( 'db2510854e6aeab68ead26b48328b19f4bdf926e', Mockery::type( 'Writing_On_GitHub_Commit' ) )
100
    //      ->andReturnUsing( function ( $sha, $commit ) {
101
    //          return $commit;
102
    //      } );
103
104
    //  $this->assertInstanceOf( 'Writing_On_GitHub_Commit', $master = $this->fetch->master() );
105
106
    //  /**
107
    //   * Validate the commit's api data mapped correctly.
108
    //   */
109
    //  $this->assertSame( '7497c0574b9430ff5e5521b4572b7452ea36a056', $master->sha() );
110
    //  $this->assertSame( '[email protected]', $master->author()->email );
111
    //  $this->assertSame( '2015-11-02T00:36:54Z', $master->author()->date );
112
    //  $this->assertSame( '[email protected]', $master->committer()->email );
113
    //  $this->assertSame( '2015-11-02T00:36:54Z', $master->committer()->date );
114
    //  $this->assertSame( 'Initial full site export - wogh', $master->message() );
115
    //  $this->assertCount( 1, $parents = $master->parents() );
116
    //  $this->assertSame( 'db2510854e6aeab68ead26b48328b19f4bdf926e', $parents[0]->sha );
117
118
    //  $this->assertInstanceOf( 'Writing_On_GitHub_Tree', $tree = $master->tree() );
119
120
    //  /**
121
    //   * Validate the tree's api data mapped correctly.
122
    //   */
123
    //  $this->assertSame( '9108868e3800bec6763e51beb0d33e15036c3626', $tree->sha() );
124
125
    //  $this->assertCount( 3, $blobs = $tree->blobs() );
126
127
    //  /**
128
    //   * Validate the blobs' api data mapped correctly.
129
    //   */
130
    //  $blobs = $tree->blobs();
131
    //  $this->assertCount( 3, $blobs );
132
    //  foreach ( $blobs as $blob ) {
133
    //      $this->assertTrue( in_array( $blob->sha(), array(
134
    //          '2d73165945b0ccbe4932f1363457986b0ed49f19',
135
    //          '8d9b2e6fd93761211dc03abd71f4a9189d680fd0',
136
    //          '9fa5c7537f8582b71028ff34b8c20dfd0f3b2a25',
137
    //      ) ) );
138
    //      $this->assertTrue( in_array( $blob->path(), array(
139
    //          '_pages/sample-page.md',
140
    //          '_posts/2015-11-02-hello-world.md',
141
    //          'README.md',
142
    //      ) ) );
143
    //  }
144
    // }
145
146
    protected function malformed_repo( $repo ) {
147
        update_option( Base::REPO_OPTION_KEY, $repo );
148
149
        $this->assertInstanceOf( 'WP_Error', $error = $this->fetch->tree_recursive() );
150
        $this->assertSame( 'malformed_repository', $error->get_error_code() );
151
    }
152
}
153