<commit_before><?php
namespace Alt3\Swagger\Test\TestCase\Lib;

use Alt3\Swagger\Controller\AppController;
use Alt3\Swagger\Lib\SwaggerTools;
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Filesystem\File;
use Cake\TestSuite\TestCase;
use StdClass;
use Swagger\Annotations\Swagger;

class SwaggerToolsTest extends TestCase
{

    /**
     * @var \Alt3\Swagger\Lib\SwaggerTools Swaggertools instance.
     */
    protected $lib;

    /**
     * @var array Default AppController settings every test will start with.
     */
    protected static $defaultConfig = [
        'docs' => [
            'crawl' => true
        ],
        'ui' => [
            'title' => 'cakephp-swagger'
        ]
    ];

    /**
     * setUp method executed before every testMethod.
     */
    public function setUp()
    {
        parent::setUp();
        $this->lib = new SwaggerTools();
    }

    /**
     * tearDown method executed after every testMethod.
     */
    public function tearDown()
    {
        parent::tearDownAfterClass();
        $testDoc = CACHE . 'cakephp_swagger_testdoc.json';
        if (file_exists($testDoc)) {
            unlink($testDoc);
        }
    }

    /**
     * Make sure our tests are using the expected configuration settings.
     */
    public function testDefaultSettings()
    {
        $this->assertSame(self::$defaultConfig, AppController::$defaultConfig);
    }

    /**
     * Make sure swagger documents can be crawl-generated successfully.
     */
    public function testMethodGetSwaggerDocumentCrawlSuccess()
    {
        $reflection = self::getReflection($this->lib);
        Configure::write('Swagger', array_merge(self::$defaultConfig, [
            'docs' => [
                'crawl' => true
            ],
            'library' => [
                'testdoc' => [
                    'include' => APP . 'src', // all files in dir
                ]
            ]
        ]));

        // make sure all files are being crawled
        $result = $reflection->methods->getSwaggerDocument->invokeArgs($this->lib, ['testdoc', 'www.test.app']);
        $this->assertSame($result->info->description, 'cakephp-swagger test document'); // IncludeController
        $this->assertSame($result->paths[0]->path, '/taxis'); // ExcludeController

        // make sure exclusions are actually being excluded from crawling.
        Configure::write('Swagger', array_merge(self::$defaultConfig, [
            'docs' => [
                'crawl' => true
            ],
            'library' => [
                'testdoc' => [
                    'include' => APP . 'src',
                    'exclude' => APP . 'src' . DS . 'Controller' . DS . 'DummyExcludeController'
                ]
            ],
            'analyser' => new \Swagger\StaticAnalyser()
        ]));

        $result = $reflection->methods->getSwaggerDocument->invokeArgs($this->lib, ['testdoc', 'www.test.app']);
        $this->assertSame($result->info->description, 'cakephp-swagger test document'); // IncludeController
        $this->assertNotSame($result->paths[0]->path, '/taxis'); // In ExcludeController so should not be present
    }

    /**
     * Make sure an exception is thrown when swagger document cannot be
     * written to the filesystem.
     *
     * @expectedException \Cake\Network\Exception\InternalErrorException
     * @expectedExceptionMessage Error writing Swagger json document to filesystem
     */
    public function testMethodWriteSwaggerDocumentToFileFail()
    {
        $reflection = self::getReflection($this->lib);
        $reflection->methods->writeSwaggerDocumentToFile->invokeArgs($this->lib, ['////failing-doc-path', 'dummy-file-content']);
    }

    /**
     * Make sure requesting a document that does not exist on the filesystem
     * throws an exception in non-development mode.
     *
     * @expectedException \Cake\Network\Exception\NotFoundException
     * @expectedExceptionMessageRegExp #Swagger json document was not found on filesystem: *#
     */
    public function testMethodGetSwaggerDocumentFromFileFail()
    {
        $reflection = self::getReflection($this->lib);
        Configure::write('Swagger', array_merge(self::$defaultConfig, [
            'docs' => [
                'crawl' => false // force loading doc from filesystem
            ],
            'library' => [
                'nonexisting' => []
            ]
        ]));
        $reflection->methods->getSwaggerDocument->invokeArgs($this->lib, ['nonexisting', 'www.test.app']);
    }

    /**
     * Make sure swagger documents are successfully served from filesystem.
     */
    public function testMethodGetSwaggerDocumentFromFileSuccess()
    {
        // make sure test file does not exist
        $filePath = CACHE . 'cakephp_swagger_testdoc.json';<commit_msg>Fixing deprecated Cake Network Exceptions in PHPUnit tests<commit_after><?php
namespace Alt3\Swagger\Test\TestCase\Lib;

use Alt3\Swagger\Controller\AppController;
use Alt3\Swagger\Lib\SwaggerTools;
use Cake\Core\Configure;
use Cake\Filesystem\File;
use Cake\TestSuite\TestCase;
use StdClass;

class SwaggerToolsTest extends TestCase
{

    /**
     * @var \Alt3\Swagger\Lib\SwaggerTools Swaggertools instance.
     */
    protected $lib;

    /**
     * @var array Default AppController settings every test will start with.
     */
    protected static $defaultConfig = [
        'docs' => [
            'crawl' => true
        ],
        'ui' => [
            'title' => 'cakephp-swagger'
        ]
    ];

    /**
     * setUp method executed before every testMethod.
     */
    public function setUp()
    {
        parent::setUp();
        $this->lib = new SwaggerTools();
    }

    /**
     * tearDown method executed after every testMethod.
     */
    public function tearDown()
    {
        parent::tearDownAfterClass();
        $testDoc = CACHE . 'cakephp_swagger_testdoc.json';
        if (file_exists($testDoc)) {
            unlink($testDoc);
        }
    }

    /**
     * Make sure our tests are using the expected configuration settings.
     */
    public function testDefaultSettings()
    {
        $this->assertSame(self::$defaultConfig, AppController::$defaultConfig);
    }

    /**
     * Make sure swagger documents can be crawl-generated successfully.
     */
    public function testMethodGetSwaggerDocumentCrawlSuccess()
    {
        $reflection = self::getReflection($this->lib);
        Configure::write('Swagger', array_merge(self::$defaultConfig, [
            'docs' => [
                'crawl' => true
            ],
            'library' => [
                'testdoc' => [
                    'include' => APP . 'src', // all files in dir
                ]
            ]
        ]));

        // make sure all files are being crawled
        $result = $reflection->methods->getSwaggerDocument->invokeArgs($this->lib, ['testdoc', 'www.test.app']);
        $this->assertSame($result->info->description, 'cakephp-swagger test document'); // IncludeController
        $this->assertSame($result->paths[0]->path, '/taxis'); // ExcludeController

        // make sure exclusions are actually being excluded from crawling.
        Configure::write('Swagger', array_merge(self::$defaultConfig, [
            'docs' => [
                'crawl' => true
            ],
            'library' => [
                'testdoc' => [
                    'include' => APP . 'src',
                    'exclude' => APP . 'src' . DS . 'Controller' . DS . 'DummyExcludeController'
                ]
            ],
            'analyser' => new \Swagger\StaticAnalyser()
        ]));

        $result = $reflection->methods->getSwaggerDocument->invokeArgs($this->lib, ['testdoc', 'www.test.app']);
        $this->assertSame($result->info->description, 'cakephp-swagger test document'); // IncludeController
        $this->assertNotSame($result->paths[0]->path, '/taxis'); // In ExcludeController so should not be present
    }

    /**
     * Make sure an exception is thrown when swagger document cannot be
     * written to the filesystem.
     *
     * @expectedException \Cake\Http\Exception\InternalErrorException
     * @expectedExceptionMessage Error writing Swagger json document to filesystem
     */
    public function testMethodWriteSwaggerDocumentToFileFail()
    {
        $reflection = self::getReflection($this->lib);
        $reflection->methods->writeSwaggerDocumentToFile->invokeArgs($this->lib, ['////failing-doc-path', 'dummy-file-content']);
    }

    /**
     * Make sure requesting a document that does not exist on the filesystem
     * throws an exception in non-development mode.
     *
     * @expectedException \Cake\Http\Exception\NotFoundException
     * @expectedExceptionMessageRegExp #Swagger json document was not found on filesystem: *#
     */
    public function testMethodGetSwaggerDocumentFromFileFail()
    {
        $reflection = self::getReflection($this->lib);
        Configure::write('Swagger', array_merge(self::$defaultConfig, [
            'docs' => [
                'crawl' => false // force loading doc from filesystem
            ],
            'library' => [
                'nonexisting' => []
            ]
        ]));
        $reflection->methods->getSwaggerDocument->invokeArgs($this->lib, ['nonexisting', 'www.test.app']);
    }

    /**
     * Make sure swagger documents are successfully served from filesystem.
     */
    public function testMethodGetSwaggerDocumentFromFileSuccess()
    {
        // make sure test file does not exist
        $filePath = CACHE . 'cakephp_swagger_testdoc.json';
{'fraction_non_alphanumeric': 0.12787296280819055, 'fraction_numerical': 0.0012536564981195152, 'mean_word_length': 2.258339006126617, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1872414', 'n_tokens_mistral': 2653, 'n_tokens_neox': 2555, 'n_words': 582}