diff --git a/codeception.yml b/codeception.yml deleted file mode 100644 index 3a8fca8145..0000000000 --- a/codeception.yml +++ /dev/null @@ -1,21 +0,0 @@ -actor: Tester -paths: - tests: tests - log: tests/_output - data: tests/_data - support: tests/_support - envs: tests/_envs -settings: - bootstrap: _bootstrap.php - colors: true - memory_limit: 1024M -extensions: - enabled: - - Codeception\Extension\RunFailed -modules: - config: - Db: - dsn: '' - user: '' - password: '' - dump: tests/_data/dump.sql diff --git a/composer.json b/composer.json index 14e15576fd..b1134d9cd1 100644 --- a/composer.json +++ b/composer.json @@ -39,11 +39,7 @@ "mockery/mockery": "0.9.*", "league/factory-muffin": "~2.1", "codeclimate/php-test-reporter": "^0.1.2", - "fzaninotto/faker": "^1.4", - "codeception/codeception": "*" - - - + "fzaninotto/faker": "^1.4" }, "autoload": { "classmap": [ diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000000..6b3ab70282 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,35 @@ + + + + + ./tests + + + + + ./app + + + + + + + + + + + + diff --git a/pu.sh b/pu.sh new file mode 100755 index 0000000000..5daa2f6f0d --- /dev/null +++ b/pu.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# set testing environment +cp .env.testing .env + +# test! +if [ -z "$1" ] +then + phpunit --verbose +fi + +# directories to look in: +dirs=("controllers" "database" "factories" "generators" "helpers" "models" "middleware" "repositories" "support") + +if [ ! -z "$1" ] +then + for i in "${dirs[@]}" + do + firstFile="./tests/$i/$1.php" + secondFile="./tests/$i/$1Test.php" + if [ -f "$firstFile" ] + then + # run it! + phpunit --verbose $firstFile + exit $? + fi + if [ -f "$secondFile" ] + then + # run it! + phpunit --verbose $secondFile + exit $? + fi + + + done + +fi + +# restore .env file +cp .env.local .env diff --git a/tests/TestCase.php b/tests/TestCase.php index c5f2c8b4ad..8b972b79d0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -33,6 +33,40 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase { parent::setUp(); + // if the database copy does not exist, call migrate. + $copy = __DIR__ . '/../storage/database/testing-copy.db'; + $original = __DIR__ . '/../storage/database/testing.db'; + + FactoryMuffin::loadFactories(__DIR__ . '/factories'); + + if (!file_exists($copy)) { + touch($original); + Artisan::call('migrate'); + + + // create EUR currency + /** @var TransactionCurrency $currency */ + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $currency->code = 'EUR'; + $currency->save(); + copy($original, $copy); + } else { + + if (file_exists($copy)) { + copy($copy, $original); + } + } + // if the database copy does exists, copy back as original. + + $this->session( + [ + 'start' => Carbon::now()->startOfMonth(), + 'end' => Carbon::now()->endOfMonth(), + 'first' => Carbon::now()->startOfYear() + ] + ); + + } /** @@ -53,6 +87,10 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase { parent::tearDown(); + // delete copy original. + //$original = __DIR__.'/../storage/database/testing.db'; + //unlink($original); + } /** diff --git a/tests/_bootstrap.php b/tests/_bootstrap.php deleted file mode 100644 index 243f9c85bc..0000000000 --- a/tests/_bootstrap.php +++ /dev/null @@ -1,2 +0,0 @@ -setHeader('X-Requested-With', 'Codeception'); - * $I->amOnPage('test-headers.php'); - * ?> - * ``` - * - * @param string $name the name of the request header - * @param string $value the value to set it to for subsequent - * requests - * @see \Codeception\Module\PhpBrowser::setHeader() - */ - public function setHeader($name, $value) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('setHeader', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Deletes the header with the passed name. Subsequent requests - * will not have the deleted header in its request. - * - * Example: - * ```php - * setHeader('X-Requested-With', 'Codeception'); - * $I->amOnPage('test-headers.php'); - * // ... - * $I->deleteHeader('X-Requested-With'); - * $I->amOnPage('some-other-page.php'); - * ?> - * ``` - * - * @param string $name the name of the header to delete. - * @see \Codeception\Module\PhpBrowser::deleteHeader() - */ - public function deleteHeader($name) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('deleteHeader', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Authenticates user for HTTP_AUTH - * - * @param $username - * @param $password - * @see \Codeception\Module\PhpBrowser::amHttpAuthenticated() - */ - public function amHttpAuthenticated($username, $password) { - return $this->getScenario()->runStep(new \Codeception\Step\Condition('amHttpAuthenticated', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Open web page at the given absolute URL and sets its hostname as the base host. - * - * ``` php - * amOnUrl('http://codeception.com'); - * $I->amOnPage('/quickstart'); // moves to http://codeception.com/quickstart - * ?> - * ``` - * @see \Codeception\Module\PhpBrowser::amOnUrl() - */ - public function amOnUrl($url) { - return $this->getScenario()->runStep(new \Codeception\Step\Condition('amOnUrl', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Changes the subdomain for the 'url' configuration parameter. - * Does not open a page; use `amOnPage` for that. - * - * ``` php - * amOnSubdomain('user'); - * $I->amOnPage('/'); - * // moves to http://user.mysite.com/ - * ?> - * ``` - * - * @param $subdomain - * - * @return mixed - * @see \Codeception\Module\PhpBrowser::amOnSubdomain() - */ - public function amOnSubdomain($subdomain) { - return $this->getScenario()->runStep(new \Codeception\Step\Condition('amOnSubdomain', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Low-level API method. - * If Codeception commands are not enough, use [Guzzle HTTP Client](http://guzzlephp.org/) methods directly - * - * Example: - * - * ``` php - * executeInGuzzle(function (\GuzzleHttp\Client $client) { - * $client->get('/get', ['query' => ['foo' => 'bar']]); - * }); - * ?> - * ``` - * - * It is not recommended to use this command on a regular basis. - * If Codeception lacks important Guzzle Client methods, implement them and submit patches. - * - * @param callable $function - * @see \Codeception\Module\PhpBrowser::executeInGuzzle() - */ - public function executeInGuzzle($function) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('executeInGuzzle', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Opens the page for the given relative URI. - * - * ``` php - * amOnPage('/'); - * // opens /register page - * $I->amOnPage('/register'); - * ?> - * ``` - * - * @param $page - * @see \Codeception\Lib\InnerBrowser::amOnPage() - */ - public function amOnPage($page) { - return $this->getScenario()->runStep(new \Codeception\Step\Condition('amOnPage', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Perform a click on a link or a button, given by a locator. - * If a fuzzy locator is given, the page will be searched for a button, link, or image matching the locator string. - * For buttons, the "value" attribute, "name" attribute, and inner text are searched. - * For links, the link text is searched. - * For images, the "alt" attribute and inner text of any parent links are searched. - * - * The second parameter is a context (CSS or XPath locator) to narrow the search. - * - * Note that if the locator matches a button of type `submit`, the form will be submitted. - * - * ``` php - * click('Logout'); - * // button of form - * $I->click('Submit'); - * // CSS button - * $I->click('#form input[type=submit]'); - * // XPath - * $I->click('//form/*[@type=submit]'); - * // link in context - * $I->click('Logout', '#nav'); - * // using strict locator - * $I->click(['link' => 'Login']); - * ?> - * ``` - * - * @param $link - * @param $context - * @see \Codeception\Lib\InnerBrowser::click() - */ - public function click($link, $context = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('click', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the current page contains the given string. - * Specify a locator as the second parameter to match a specific region. - * - * ``` php - * see('Logout'); // I can suppose user is logged in - * $I->see('Sign Up','h1'); // I can suppose it's a signup page - * $I->see('Sign Up','//body/h1'); // with XPath - * ?> - * ``` - * - * @param $text - * @param null $selector - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::see() - */ - public function canSee($text, $selector = null) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('see', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the current page contains the given string. - * Specify a locator as the second parameter to match a specific region. - * - * ``` php - * see('Logout'); // I can suppose user is logged in - * $I->see('Sign Up','h1'); // I can suppose it's a signup page - * $I->see('Sign Up','//body/h1'); // with XPath - * ?> - * ``` - * - * @param $text - * @param null $selector - * @see \Codeception\Lib\InnerBrowser::see() - */ - public function see($text, $selector = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('see', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the current page doesn't contain the text specified. - * Give a locator as the second parameter to match a specific region. - * - * ```php - * dontSee('Login'); // I can suppose user is already logged in - * $I->dontSee('Sign Up','h1'); // I can suppose it's not a signup page - * $I->dontSee('Sign Up','//body/h1'); // with XPath - * ?> - * ``` - * - * @param $text - * @param null $selector - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::dontSee() - */ - public function cantSee($text, $selector = null) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('dontSee', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the current page doesn't contain the text specified. - * Give a locator as the second parameter to match a specific region. - * - * ```php - * dontSee('Login'); // I can suppose user is already logged in - * $I->dontSee('Sign Up','h1'); // I can suppose it's not a signup page - * $I->dontSee('Sign Up','//body/h1'); // with XPath - * ?> - * ``` - * - * @param $text - * @param null $selector - * @see \Codeception\Lib\InnerBrowser::dontSee() - */ - public function dontSee($text, $selector = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('dontSee', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that there's a link with the specified text. - * Give a full URL as the second parameter to match links with that exact URL. - * - * ``` php - * seeLink('Logout'); // matches Logout - * $I->seeLink('Logout','/logout'); // matches Logout - * ?> - * ``` - * - * @param $text - * @param null $url - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seeLink() - */ - public function canSeeLink($text, $url = null) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeLink', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that there's a link with the specified text. - * Give a full URL as the second parameter to match links with that exact URL. - * - * ``` php - * seeLink('Logout'); // matches Logout - * $I->seeLink('Logout','/logout'); // matches Logout - * ?> - * ``` - * - * @param $text - * @param null $url - * @see \Codeception\Lib\InnerBrowser::seeLink() - */ - public function seeLink($text, $url = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeLink', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the page doesn't contain a link with the given string. - * If the second parameter is given, only links with a matching "href" attribute will be checked. - * - * ``` php - * dontSeeLink('Logout'); // I suppose user is not logged in - * $I->dontSeeLink('Checkout now', '/store/cart.php'); - * ?> - * ``` - * - * @param $text - * @param null $url - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::dontSeeLink() - */ - public function cantSeeLink($text, $url = null) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('dontSeeLink', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the page doesn't contain a link with the given string. - * If the second parameter is given, only links with a matching "href" attribute will be checked. - * - * ``` php - * dontSeeLink('Logout'); // I suppose user is not logged in - * $I->dontSeeLink('Checkout now', '/store/cart.php'); - * ?> - * ``` - * - * @param $text - * @param null $url - * @see \Codeception\Lib\InnerBrowser::dontSeeLink() - */ - public function dontSeeLink($text, $url = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('dontSeeLink', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that current URI contains the given string. - * - * ``` php - * seeInCurrentUrl('home'); - * // to match: /users/1 - * $I->seeInCurrentUrl('/users/'); - * ?> - * ``` - * - * @param $uri - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seeInCurrentUrl() - */ - public function canSeeInCurrentUrl($uri) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeInCurrentUrl', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that current URI contains the given string. - * - * ``` php - * seeInCurrentUrl('home'); - * // to match: /users/1 - * $I->seeInCurrentUrl('/users/'); - * ?> - * ``` - * - * @param $uri - * @see \Codeception\Lib\InnerBrowser::seeInCurrentUrl() - */ - public function seeInCurrentUrl($uri) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeInCurrentUrl', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the current URI doesn't contain the given string. - * - * ``` php - * dontSeeInCurrentUrl('/users/'); - * ?> - * ``` - * - * @param $uri - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::dontSeeInCurrentUrl() - */ - public function cantSeeInCurrentUrl($uri) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('dontSeeInCurrentUrl', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the current URI doesn't contain the given string. - * - * ``` php - * dontSeeInCurrentUrl('/users/'); - * ?> - * ``` - * - * @param $uri - * @see \Codeception\Lib\InnerBrowser::dontSeeInCurrentUrl() - */ - public function dontSeeInCurrentUrl($uri) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('dontSeeInCurrentUrl', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the current URL is equal to the given string. - * Unlike `seeInCurrentUrl`, this only matches the full URL. - * - * ``` php - * seeCurrentUrlEquals('/'); - * ?> - * ``` - * - * @param $uri - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seeCurrentUrlEquals() - */ - public function canSeeCurrentUrlEquals($uri) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeCurrentUrlEquals', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the current URL is equal to the given string. - * Unlike `seeInCurrentUrl`, this only matches the full URL. - * - * ``` php - * seeCurrentUrlEquals('/'); - * ?> - * ``` - * - * @param $uri - * @see \Codeception\Lib\InnerBrowser::seeCurrentUrlEquals() - */ - public function seeCurrentUrlEquals($uri) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeCurrentUrlEquals', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the current URL doesn't equal the given string. - * Unlike `dontSeeInCurrentUrl`, this only matches the full URL. - * - * ``` php - * dontSeeCurrentUrlEquals('/'); - * ?> - * ``` - * - * @param $uri - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::dontSeeCurrentUrlEquals() - */ - public function cantSeeCurrentUrlEquals($uri) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('dontSeeCurrentUrlEquals', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the current URL doesn't equal the given string. - * Unlike `dontSeeInCurrentUrl`, this only matches the full URL. - * - * ``` php - * dontSeeCurrentUrlEquals('/'); - * ?> - * ``` - * - * @param $uri - * @see \Codeception\Lib\InnerBrowser::dontSeeCurrentUrlEquals() - */ - public function dontSeeCurrentUrlEquals($uri) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('dontSeeCurrentUrlEquals', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the current URL matches the given regular expression. - * - * ``` php - * seeCurrentUrlMatches('~$/users/(\d+)~'); - * ?> - * ``` - * - * @param $uri - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seeCurrentUrlMatches() - */ - public function canSeeCurrentUrlMatches($uri) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeCurrentUrlMatches', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the current URL matches the given regular expression. - * - * ``` php - * seeCurrentUrlMatches('~$/users/(\d+)~'); - * ?> - * ``` - * - * @param $uri - * @see \Codeception\Lib\InnerBrowser::seeCurrentUrlMatches() - */ - public function seeCurrentUrlMatches($uri) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeCurrentUrlMatches', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that current url doesn't match the given regular expression. - * - * ``` php - * dontSeeCurrentUrlMatches('~$/users/(\d+)~'); - * ?> - * ``` - * - * @param $uri - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::dontSeeCurrentUrlMatches() - */ - public function cantSeeCurrentUrlMatches($uri) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('dontSeeCurrentUrlMatches', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that current url doesn't match the given regular expression. - * - * ``` php - * dontSeeCurrentUrlMatches('~$/users/(\d+)~'); - * ?> - * ``` - * - * @param $uri - * @see \Codeception\Lib\InnerBrowser::dontSeeCurrentUrlMatches() - */ - public function dontSeeCurrentUrlMatches($uri) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('dontSeeCurrentUrlMatches', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Executes the given regular expression against the current URI and returns the first match. - * If no parameters are provided, the full URI is returned. - * - * ``` php - * grabFromCurrentUrl('~$/user/(\d+)/~'); - * $uri = $I->grabFromCurrentUrl(); - * ?> - * ``` - * - * @param null $uri - * - * @internal param $url - * @return mixed - * @see \Codeception\Lib\InnerBrowser::grabFromCurrentUrl() - */ - public function grabFromCurrentUrl($uri = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('grabFromCurrentUrl', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the specified checkbox is checked. - * - * ``` php - * seeCheckboxIsChecked('#agree'); // I suppose user agreed to terms - * $I->seeCheckboxIsChecked('#signup_form input[type=checkbox]'); // I suppose user agreed to terms, If there is only one checkbox in form. - * $I->seeCheckboxIsChecked('//form/input[@type=checkbox and @name=agree]'); - * ?> - * ``` - * - * @param $checkbox - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seeCheckboxIsChecked() - */ - public function canSeeCheckboxIsChecked($checkbox) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeCheckboxIsChecked', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the specified checkbox is checked. - * - * ``` php - * seeCheckboxIsChecked('#agree'); // I suppose user agreed to terms - * $I->seeCheckboxIsChecked('#signup_form input[type=checkbox]'); // I suppose user agreed to terms, If there is only one checkbox in form. - * $I->seeCheckboxIsChecked('//form/input[@type=checkbox and @name=agree]'); - * ?> - * ``` - * - * @param $checkbox - * @see \Codeception\Lib\InnerBrowser::seeCheckboxIsChecked() - */ - public function seeCheckboxIsChecked($checkbox) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeCheckboxIsChecked', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Check that the specified checkbox is unchecked. - * - * ``` php - * dontSeeCheckboxIsChecked('#agree'); // I suppose user didn't agree to terms - * $I->seeCheckboxIsChecked('#signup_form input[type=checkbox]'); // I suppose user didn't check the first checkbox in form. - * ?> - * ``` - * - * @param $checkbox - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::dontSeeCheckboxIsChecked() - */ - public function cantSeeCheckboxIsChecked($checkbox) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('dontSeeCheckboxIsChecked', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Check that the specified checkbox is unchecked. - * - * ``` php - * dontSeeCheckboxIsChecked('#agree'); // I suppose user didn't agree to terms - * $I->seeCheckboxIsChecked('#signup_form input[type=checkbox]'); // I suppose user didn't check the first checkbox in form. - * ?> - * ``` - * - * @param $checkbox - * @see \Codeception\Lib\InnerBrowser::dontSeeCheckboxIsChecked() - */ - public function dontSeeCheckboxIsChecked($checkbox) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('dontSeeCheckboxIsChecked', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the given input field or textarea contains the given value. - * For fuzzy locators, fields are matched by label text, the "name" attribute, CSS, and XPath. - * - * ``` php - * seeInField('Body','Type your comment here'); - * $I->seeInField('form textarea[name=body]','Type your comment here'); - * $I->seeInField('form input[type=hidden]','hidden_value'); - * $I->seeInField('#searchform input','Search'); - * $I->seeInField('//form/*[@name=search]','Search'); - * $I->seeInField(['name' => 'search'], 'Search'); - * ?> - * ``` - * - * @param $field - * @param $value - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seeInField() - */ - public function canSeeInField($field, $value) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeInField', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the given input field or textarea contains the given value. - * For fuzzy locators, fields are matched by label text, the "name" attribute, CSS, and XPath. - * - * ``` php - * seeInField('Body','Type your comment here'); - * $I->seeInField('form textarea[name=body]','Type your comment here'); - * $I->seeInField('form input[type=hidden]','hidden_value'); - * $I->seeInField('#searchform input','Search'); - * $I->seeInField('//form/*[@name=search]','Search'); - * $I->seeInField(['name' => 'search'], 'Search'); - * ?> - * ``` - * - * @param $field - * @param $value - * @see \Codeception\Lib\InnerBrowser::seeInField() - */ - public function seeInField($field, $value) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeInField', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that an input field or textarea doesn't contain the given value. - * For fuzzy locators, the field is matched by label text, CSS and XPath. - * - * ``` php - * dontSeeInField('Body','Type your comment here'); - * $I->dontSeeInField('form textarea[name=body]','Type your comment here'); - * $I->dontSeeInField('form input[type=hidden]','hidden_value'); - * $I->dontSeeInField('#searchform input','Search'); - * $I->dontSeeInField('//form/*[@name=search]','Search'); - * $I->dontSeeInField(['name' => 'search'], 'Search'); - * ?> - * ``` - * - * @param $field - * @param $value - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::dontSeeInField() - */ - public function cantSeeInField($field, $value) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('dontSeeInField', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that an input field or textarea doesn't contain the given value. - * For fuzzy locators, the field is matched by label text, CSS and XPath. - * - * ``` php - * dontSeeInField('Body','Type your comment here'); - * $I->dontSeeInField('form textarea[name=body]','Type your comment here'); - * $I->dontSeeInField('form input[type=hidden]','hidden_value'); - * $I->dontSeeInField('#searchform input','Search'); - * $I->dontSeeInField('//form/*[@name=search]','Search'); - * $I->dontSeeInField(['name' => 'search'], 'Search'); - * ?> - * ``` - * - * @param $field - * @param $value - * @see \Codeception\Lib\InnerBrowser::dontSeeInField() - */ - public function dontSeeInField($field, $value) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('dontSeeInField', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks if the array of form parameters (name => value) are set on the form matched with the - * passed selector. - * - * ``` php - * seeInFormFields('form[name=myform]', [ - * 'input1' => 'value', - * 'input2' => 'other value', - * ]); - * ?> - * ``` - * - * For multi-select elements, or to check values of multiple elements with the same name, an - * array may be passed: - * - * ``` php - * seeInFormFields('.form-class', [ - * 'multiselect' => [ - * 'value1', - * 'value2', - * ], - * 'checkbox[]' => [ - * 'a checked value', - * 'another checked value', - * ], - * ]); - * ?> - * ``` - * - * Additionally, checkbox values can be checked with a boolean. - * - * ``` php - * seeInFormFields('#form-id', [ - * 'checkbox1' => true, // passes if checked - * 'checkbox2' => false, // passes if unchecked - * ]); - * ?> - * ``` - * - * Pair this with submitForm for quick testing magic. - * - * ``` php - * 'value', - * 'field2' => 'another value', - * 'checkbox1' => true, - * // ... - * ]; - * $I->submitForm('//form[@id=my-form]', $form, 'submitButton'); - * // $I->amOnPage('/path/to/form-page') may be needed - * $I->seeInFormFields('//form[@id=my-form]', $form); - * ?> - * ``` - * - * @param $formSelector - * @param $params - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seeInFormFields() - */ - public function canSeeInFormFields($formSelector, $params) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeInFormFields', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks if the array of form parameters (name => value) are set on the form matched with the - * passed selector. - * - * ``` php - * seeInFormFields('form[name=myform]', [ - * 'input1' => 'value', - * 'input2' => 'other value', - * ]); - * ?> - * ``` - * - * For multi-select elements, or to check values of multiple elements with the same name, an - * array may be passed: - * - * ``` php - * seeInFormFields('.form-class', [ - * 'multiselect' => [ - * 'value1', - * 'value2', - * ], - * 'checkbox[]' => [ - * 'a checked value', - * 'another checked value', - * ], - * ]); - * ?> - * ``` - * - * Additionally, checkbox values can be checked with a boolean. - * - * ``` php - * seeInFormFields('#form-id', [ - * 'checkbox1' => true, // passes if checked - * 'checkbox2' => false, // passes if unchecked - * ]); - * ?> - * ``` - * - * Pair this with submitForm for quick testing magic. - * - * ``` php - * 'value', - * 'field2' => 'another value', - * 'checkbox1' => true, - * // ... - * ]; - * $I->submitForm('//form[@id=my-form]', $form, 'submitButton'); - * // $I->amOnPage('/path/to/form-page') may be needed - * $I->seeInFormFields('//form[@id=my-form]', $form); - * ?> - * ``` - * - * @param $formSelector - * @param $params - * @see \Codeception\Lib\InnerBrowser::seeInFormFields() - */ - public function seeInFormFields($formSelector, $params) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeInFormFields', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks if the array of form parameters (name => value) are not set on the form matched with - * the passed selector. - * - * ``` php - * dontSeeInFormFields('form[name=myform]', [ - * 'input1' => 'non-existent value', - * 'input2' => 'other non-existent value', - * ]); - * ?> - * ``` - * - * To check that an element hasn't been assigned any one of many values, an array can be passed - * as the value: - * - * ``` php - * dontSeeInFormFields('.form-class', [ - * 'fieldName' => [ - * 'This value shouldn\'t be set', - * 'And this value shouldn\'t be set', - * ], - * ]); - * ?> - * ``` - * - * Additionally, checkbox values can be checked with a boolean. - * - * ``` php - * dontSeeInFormFields('#form-id', [ - * 'checkbox1' => true, // fails if checked - * 'checkbox2' => false, // fails if unchecked - * ]); - * ?> - * ``` - * - * @param $formSelector - * @param $params - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::dontSeeInFormFields() - */ - public function cantSeeInFormFields($formSelector, $params) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('dontSeeInFormFields', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks if the array of form parameters (name => value) are not set on the form matched with - * the passed selector. - * - * ``` php - * dontSeeInFormFields('form[name=myform]', [ - * 'input1' => 'non-existent value', - * 'input2' => 'other non-existent value', - * ]); - * ?> - * ``` - * - * To check that an element hasn't been assigned any one of many values, an array can be passed - * as the value: - * - * ``` php - * dontSeeInFormFields('.form-class', [ - * 'fieldName' => [ - * 'This value shouldn\'t be set', - * 'And this value shouldn\'t be set', - * ], - * ]); - * ?> - * ``` - * - * Additionally, checkbox values can be checked with a boolean. - * - * ``` php - * dontSeeInFormFields('#form-id', [ - * 'checkbox1' => true, // fails if checked - * 'checkbox2' => false, // fails if unchecked - * ]); - * ?> - * ``` - * - * @param $formSelector - * @param $params - * @see \Codeception\Lib\InnerBrowser::dontSeeInFormFields() - */ - public function dontSeeInFormFields($formSelector, $params) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('dontSeeInFormFields', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Submits the given form on the page, optionally with the given form - * values. Give the form fields values as an array. - * - * Skipped fields will be filled by their values from the page. - * You don't need to click the 'Submit' button afterwards. - * This command itself triggers the request to form's action. - * - * You can optionally specify what button's value to include - * in the request with the last parameter as an alternative to - * explicitly setting its value in the second parameter, as - * button values are not otherwise included in the request. - * - * Examples: - * - * ``` php - * submitForm('#login', [ - * 'login' => 'davert', - * 'password' => '123456' - * ]); - * // or - * $I->submitForm('#login', [ - * 'login' => 'davert', - * 'password' => '123456' - * ], 'submitButtonName'); - * - * ``` - * - * For example, given this sample "Sign Up" form: - * - * ``` html - *
- * Login: - *
- * Password: - *
- * Do you agree to out terms? - *
- * Select pricing plan: - * - * - *
- * ``` - * - * You could write the following to submit it: - * - * ``` php - * submitForm( - * '#userForm', - * [ - * 'user' => [ - * 'login' => 'Davert', - * 'password' => '123456', - * 'agree' => true - * ] - * ], - * 'submitButton' - * ); - * ``` - * Note that "2" will be the submitted value for the "plan" field, as it is - * the selected option. - * - * You can also emulate a JavaScript submission by not specifying any - * buttons in the third parameter to submitForm. - * - * ```php - * submitForm( - * '#userForm', - * [ - * 'user' => [ - * 'login' => 'Davert', - * 'password' => '123456', - * 'agree' => true - * ] - * ] - * ); - * ``` - * - * Pair this with seeInFormFields for quick testing magic. - * - * ``` php - * 'value', - * 'field2' => 'another value', - * 'checkbox1' => true, - * // ... - * ]; - * $I->submitForm('//form[@id=my-form]', $form, 'submitButton'); - * // $I->amOnPage('/path/to/form-page') may be needed - * $I->seeInFormFields('//form[@id=my-form]', $form); - * ?> - * ``` - * - * Parameter values can be set to arrays for multiple input fields - * of the same name, or multi-select combo boxes. For checkboxes, - * either the string value can be used, or boolean values which will - * be replaced by the checkbox's value in the DOM. - * - * ``` php - * submitForm('#my-form', [ - * 'field1' => 'value', - * 'checkbox' => [ - * 'value of first checkbox', - * 'value of second checkbox, - * ], - * 'otherCheckboxes' => [ - * true, - * false, - * false - * ], - * 'multiselect' => [ - * 'first option value', - * 'second option value' - * ] - * ]); - * ?> - * ``` - * - * Mixing string and boolean values for a checkbox's value is not supported - * and may produce unexpected results. - * - * Field names ending in "[]" must be passed without the trailing square - * bracket characters, and must contain an array for its value. This allows - * submitting multiple values with the same name, consider: - * - * ```php - * $I->submitForm('#my-form', [ - * 'field[]' => 'value', - * 'field[]' => 'another value', // 'field[]' is already a defined key - * ]); - * ``` - * - * The solution is to pass an array value: - * - * ```php - * // this way both values are submitted - * $I->submitForm('#my-form', [ - * 'field' => [ - * 'value', - * 'another value', - * ] - * ]); - * ``` - * - * @param $selector - * @param $params - * @param $button - * @see \Codeception\Lib\InnerBrowser::submitForm() - */ - public function submitForm($selector, $params, $button = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('submitForm', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Fills a text field or textarea with the given string. - * - * ``` php - * fillField("//input[@type='text']", "Hello World!"); - * $I->fillField(['name' => 'email'], 'jon@mail.com'); - * ?> - * ``` - * - * @param $field - * @param $value - * @see \Codeception\Lib\InnerBrowser::fillField() - */ - public function fillField($field, $value) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('fillField', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Selects an option in a select tag or in radio button group. - * - * ``` php - * selectOption('form select[name=account]', 'Premium'); - * $I->selectOption('form input[name=payment]', 'Monthly'); - * $I->selectOption('//form/select[@name=account]', 'Monthly'); - * ?> - * ``` - * - * Provide an array for the second argument to select multiple options: - * - * ``` php - * selectOption('Which OS do you use?', array('Windows','Linux')); - * ?> - * ``` - * - * @param $select - * @param $option - * @see \Codeception\Lib\InnerBrowser::selectOption() - */ - public function selectOption($select, $option) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('selectOption', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Ticks a checkbox. For radio buttons, use the `selectOption` method instead. - * - * ``` php - * checkOption('#agree'); - * ?> - * ``` - * - * @param $option - * @see \Codeception\Lib\InnerBrowser::checkOption() - */ - public function checkOption($option) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('checkOption', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Unticks a checkbox. - * - * ``` php - * uncheckOption('#notify'); - * ?> - * ``` - * - * @param $option - * @see \Codeception\Lib\InnerBrowser::uncheckOption() - */ - public function uncheckOption($option) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('uncheckOption', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Attaches a file relative to the Codeception data directory to the given file upload field. - * - * ``` php - * attachFile('input[@type="file"]', 'prices.xls'); - * ?> - * ``` - * - * @param $field - * @param $filename - * @see \Codeception\Lib\InnerBrowser::attachFile() - */ - public function attachFile($field, $filename) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('attachFile', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * If your page triggers an ajax request, you can perform it manually. - * This action sends a GET ajax request with specified params. - * - * See ->sendAjaxPostRequest for examples. - * - * @param $uri - * @param $params - * @see \Codeception\Lib\InnerBrowser::sendAjaxGetRequest() - */ - public function sendAjaxGetRequest($uri, $params = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('sendAjaxGetRequest', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * If your page triggers an ajax request, you can perform it manually. - * This action sends a POST ajax request with specified params. - * Additional params can be passed as array. - * - * Example: - * - * Imagine that by clicking checkbox you trigger ajax request which updates user settings. - * We emulate that click by running this ajax request manually. - * - * ``` php - * sendAjaxPostRequest('/updateSettings', array('notifications' => true)); // POST - * $I->sendAjaxGetRequest('/updateSettings', array('notifications' => true)); // GET - * - * ``` - * - * @param $uri - * @param $params - * @see \Codeception\Lib\InnerBrowser::sendAjaxPostRequest() - */ - public function sendAjaxPostRequest($uri, $params = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('sendAjaxPostRequest', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * If your page triggers an ajax request, you can perform it manually. - * This action sends an ajax request with specified method and params. - * - * Example: - * - * You need to perform an ajax request specifying the HTTP method. - * - * ``` php - * sendAjaxRequest('PUT', '/posts/7', array('title' => 'new title')); - * - * ``` - * - * @param $method - * @param $uri - * @param $params - * @see \Codeception\Lib\InnerBrowser::sendAjaxRequest() - */ - public function sendAjaxRequest($method, $uri, $params = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('sendAjaxRequest', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Finds and returns the text contents of the given element. - * If a fuzzy locator is used, the element is found using CSS, XPath, and by matching the full page source by regular expression. - * - * ``` php - * grabTextFrom('h1'); - * $heading = $I->grabTextFrom('descendant-or-self::h1'); - * $value = $I->grabTextFrom('~ - * ``` - * - * @param $cssOrXPathOrRegex - * - * @return mixed - * @see \Codeception\Lib\InnerBrowser::grabTextFrom() - */ - public function grabTextFrom($cssOrXPathOrRegex) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('grabTextFrom', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Grabs the value of the given attribute value from the given element. - * Fails if element is not found. - * - * ``` php - * grabAttributeFrom('#tooltip', 'title'); - * ?> - * ``` - * - * - * @param $cssOrXpath - * @param $attribute - * @internal param $element - * @return mixed - * @see \Codeception\Lib\InnerBrowser::grabAttributeFrom() - */ - public function grabAttributeFrom($cssOrXpath, $attribute) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('grabAttributeFrom', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * - * @see \Codeception\Lib\InnerBrowser::grabMultiple() - */ - public function grabMultiple($cssOrXpath, $attribute = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('grabMultiple', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * @param $field - * - * @return array|mixed|null|string - * @see \Codeception\Lib\InnerBrowser::grabValueFrom() - */ - public function grabValueFrom($field) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('grabValueFrom', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Sets a cookie with the given name and value. - * You can set additional cookie params like `domain`, `path`, `expire`, `secure` in array passed as last argument. - * - * ``` php - * setCookie('PHPSESSID', 'el4ukv0kqbvoirg7nkp4dncpk3'); - * ?> - * ``` - * - * @param $name - * @param $val - * @param array $params - * - * @return mixed - * @see \Codeception\Lib\InnerBrowser::setCookie() - */ - public function setCookie($name, $val, $params = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('setCookie', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Grabs a cookie value. - * You can set additional cookie params like `domain`, `path` in array passed as last argument. - * - * @param $cookie - * - * @param array $params - * @return mixed - * @see \Codeception\Lib\InnerBrowser::grabCookie() - */ - public function grabCookie($cookie, $params = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('grabCookie', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that a cookie with the given name is set. - * You can set additional cookie params like `domain`, `path` as array passed in last argument. - * - * ``` php - * seeCookie('PHPSESSID'); - * ?> - * ``` - * - * @param $cookie - * @param array $params - * @return mixed - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seeCookie() - */ - public function canSeeCookie($cookie, $params = null) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeCookie', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that a cookie with the given name is set. - * You can set additional cookie params like `domain`, `path` as array passed in last argument. - * - * ``` php - * seeCookie('PHPSESSID'); - * ?> - * ``` - * - * @param $cookie - * @param array $params - * @return mixed - * @see \Codeception\Lib\InnerBrowser::seeCookie() - */ - public function seeCookie($cookie, $params = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeCookie', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that there isn't a cookie with the given name. - * You can set additional cookie params like `domain`, `path` as array passed in last argument. - * - * @param $cookie - * - * @param array $params - * @return mixed - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::dontSeeCookie() - */ - public function cantSeeCookie($cookie, $params = null) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('dontSeeCookie', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that there isn't a cookie with the given name. - * You can set additional cookie params like `domain`, `path` as array passed in last argument. - * - * @param $cookie - * - * @param array $params - * @return mixed - * @see \Codeception\Lib\InnerBrowser::dontSeeCookie() - */ - public function dontSeeCookie($cookie, $params = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('dontSeeCookie', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Unsets cookie with the given name. - * You can set additional cookie params like `domain`, `path` in array passed as last argument. - * - * @param $cookie - * - * @param array $params - * @return mixed - * @see \Codeception\Lib\InnerBrowser::resetCookie() - */ - public function resetCookie($name, $params = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('resetCookie', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the given element exists on the page and is visible. - * You can also specify expected attributes of this element. - * - * ``` php - * seeElement('.error'); - * $I->seeElement('//form/input[1]'); - * $I->seeElement('input', ['name' => 'login']); - * $I->seeElement('input', ['value' => '123456']); - * - * // strict locator in first arg, attributes in second - * $I->seeElement(['css' => 'form input'], ['name' => 'login']); - * ?> - * ``` - * - * @param $selector - * @param array $attributes - * @return - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seeElement() - */ - public function canSeeElement($selector, $attributes = null) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeElement', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the given element exists on the page and is visible. - * You can also specify expected attributes of this element. - * - * ``` php - * seeElement('.error'); - * $I->seeElement('//form/input[1]'); - * $I->seeElement('input', ['name' => 'login']); - * $I->seeElement('input', ['value' => '123456']); - * - * // strict locator in first arg, attributes in second - * $I->seeElement(['css' => 'form input'], ['name' => 'login']); - * ?> - * ``` - * - * @param $selector - * @param array $attributes - * @return - * @see \Codeception\Lib\InnerBrowser::seeElement() - */ - public function seeElement($selector, $attributes = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeElement', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the given element is invisible or not present on the page. - * You can also specify expected attributes of this element. - * - * ``` php - * dontSeeElement('.error'); - * $I->dontSeeElement('//form/input[1]'); - * $I->dontSeeElement('input', ['name' => 'login']); - * $I->dontSeeElement('input', ['value' => '123456']); - * ?> - * ``` - * - * @param $selector - * @param array $attributes - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::dontSeeElement() - */ - public function cantSeeElement($selector, $attributes = null) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('dontSeeElement', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the given element is invisible or not present on the page. - * You can also specify expected attributes of this element. - * - * ``` php - * dontSeeElement('.error'); - * $I->dontSeeElement('//form/input[1]'); - * $I->dontSeeElement('input', ['name' => 'login']); - * $I->dontSeeElement('input', ['value' => '123456']); - * ?> - * ``` - * - * @param $selector - * @param array $attributes - * @see \Codeception\Lib\InnerBrowser::dontSeeElement() - */ - public function dontSeeElement($selector, $attributes = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('dontSeeElement', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that there are a certain number of elements matched by the given locator on the page. - * - * ``` php - * seeNumberOfElements('tr', 10); - * $I->seeNumberOfElements('tr', [0,10]); //between 0 and 10 elements - * ?> - * ``` - * @param $selector - * @param mixed $expected : - * - string: strict number - * - array: range of numbers [0,10] - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seeNumberOfElements() - */ - public function canSeeNumberOfElements($selector, $expected) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeNumberOfElements', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that there are a certain number of elements matched by the given locator on the page. - * - * ``` php - * seeNumberOfElements('tr', 10); - * $I->seeNumberOfElements('tr', [0,10]); //between 0 and 10 elements - * ?> - * ``` - * @param $selector - * @param mixed $expected : - * - string: strict number - * - array: range of numbers [0,10] - * @see \Codeception\Lib\InnerBrowser::seeNumberOfElements() - */ - public function seeNumberOfElements($selector, $expected) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeNumberOfElements', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the given option is selected. - * - * ``` php - * seeOptionIsSelected('#form input[name=payment]', 'Visa'); - * ?> - * ``` - * - * @param $selector - * @param $optionText - * - * @return mixed - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seeOptionIsSelected() - */ - public function canSeeOptionIsSelected($selector, $optionText) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeOptionIsSelected', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the given option is selected. - * - * ``` php - * seeOptionIsSelected('#form input[name=payment]', 'Visa'); - * ?> - * ``` - * - * @param $selector - * @param $optionText - * - * @return mixed - * @see \Codeception\Lib\InnerBrowser::seeOptionIsSelected() - */ - public function seeOptionIsSelected($selector, $optionText) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeOptionIsSelected', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the given option is not selected. - * - * ``` php - * dontSeeOptionIsSelected('#form input[name=payment]', 'Visa'); - * ?> - * ``` - * - * @param $selector - * @param $optionText - * - * @return mixed - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::dontSeeOptionIsSelected() - */ - public function cantSeeOptionIsSelected($selector, $optionText) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('dontSeeOptionIsSelected', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the given option is not selected. - * - * ``` php - * dontSeeOptionIsSelected('#form input[name=payment]', 'Visa'); - * ?> - * ``` - * - * @param $selector - * @param $optionText - * - * @return mixed - * @see \Codeception\Lib\InnerBrowser::dontSeeOptionIsSelected() - */ - public function dontSeeOptionIsSelected($selector, $optionText) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('dontSeeOptionIsSelected', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Asserts that current page has 404 response status code. - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seePageNotFound() - */ - public function canSeePageNotFound() { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seePageNotFound', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Asserts that current page has 404 response status code. - * @see \Codeception\Lib\InnerBrowser::seePageNotFound() - */ - public function seePageNotFound() { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seePageNotFound', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that response code is equal to value provided. - * - * @param $code - * - * @return mixed - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seeResponseCodeIs() - */ - public function canSeeResponseCodeIs($code) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeResponseCodeIs', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that response code is equal to value provided. - * - * @param $code - * - * @return mixed - * @see \Codeception\Lib\InnerBrowser::seeResponseCodeIs() - */ - public function seeResponseCodeIs($code) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeResponseCodeIs', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the page title contains the given string. - * - * ``` php - * seeInTitle('Blog - Post #1'); - * ?> - * ``` - * - * @param $title - * - * @return mixed - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::seeInTitle() - */ - public function canSeeInTitle($title) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('seeInTitle', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the page title contains the given string. - * - * ``` php - * seeInTitle('Blog - Post #1'); - * ?> - * ``` - * - * @param $title - * - * @return mixed - * @see \Codeception\Lib\InnerBrowser::seeInTitle() - */ - public function seeInTitle($title) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('seeInTitle', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the page title does not contain the given string. - * - * @param $title - * - * @return mixed - * Conditional Assertion: Test won't be stopped on fail - * @see \Codeception\Lib\InnerBrowser::dontSeeInTitle() - */ - public function cantSeeInTitle($title) { - return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion('dontSeeInTitle', func_get_args())); - } - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that the page title does not contain the given string. - * - * @param $title - * - * @return mixed - * @see \Codeception\Lib\InnerBrowser::dontSeeInTitle() - */ - public function dontSeeInTitle($title) { - return $this->getScenario()->runStep(new \Codeception\Step\Assertion('dontSeeInTitle', func_get_args())); - } -} diff --git a/tests/_support/_generated/FunctionalTesterActions.php b/tests/_support/_generated/FunctionalTesterActions.php deleted file mode 100644 index 0d6c61fe91..0000000000 --- a/tests/_support/_generated/FunctionalTesterActions.php +++ /dev/null @@ -1,18 +0,0 @@ -getScenario()->runStep(new \Codeception\Step\Action('assertEquals', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that two variables are not equal - * - * @param $expected - * @param $actual - * @param string $message - * @see \Codeception\Module\Asserts::assertNotEquals() - */ - public function assertNotEquals($expected, $actual, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotEquals', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that two variables are same - * - * @param $expected - * @param $actual - * @param string $message - * - * @return mixed - * @see \Codeception\Module\Asserts::assertSame() - */ - public function assertSame($expected, $actual, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertSame', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that two variables are not same - * - * @param $expected - * @param $actual - * @param string $message - * @see \Codeception\Module\Asserts::assertNotSame() - */ - public function assertNotSame($expected, $actual, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotSame', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that actual is greater than expected - * - * @param $expected - * @param $actual - * @param string $message - * @see \Codeception\Module\Asserts::assertGreaterThan() - */ - public function assertGreaterThan($expected, $actual, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertGreaterThan', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * @deprecated - * @see \Codeception\Module\Asserts::assertGreaterThen() - */ - public function assertGreaterThen($expected, $actual, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertGreaterThen', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that actual is greater or equal than expected - * - * @param $expected - * @param $actual - * @param string $message - * @see \Codeception\Module\Asserts::assertGreaterThanOrEqual() - */ - public function assertGreaterThanOrEqual($expected, $actual, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertGreaterThanOrEqual', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * @deprecated - * @see \Codeception\Module\Asserts::assertGreaterThenOrEqual() - */ - public function assertGreaterThenOrEqual($expected, $actual, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertGreaterThenOrEqual', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that actual is less than expected - * - * @param $expected - * @param $actual - * @param string $message - * @see \Codeception\Module\Asserts::assertLessThan() - */ - public function assertLessThan($expected, $actual, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertLessThan', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that actual is less or equal than expected - * - * @param $expected - * @param $actual - * @param string $message - * @see \Codeception\Module\Asserts::assertLessThanOrEqual() - */ - public function assertLessThanOrEqual($expected, $actual, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertLessThanOrEqual', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that haystack contains needle - * - * @param $needle - * @param $haystack - * @param string $message - * @see \Codeception\Module\Asserts::assertContains() - */ - public function assertContains($needle, $haystack, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertContains', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that haystack doesn't contain needle. - * - * @param $needle - * @param $haystack - * @param string $message - * @see \Codeception\Module\Asserts::assertNotContains() - */ - public function assertNotContains($needle, $haystack, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotContains', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that string match with pattern - * - * @param string $pattern - * @param string $string - * @param string $message - * @see \Codeception\Module\Asserts::assertRegExp() - */ - public function assertRegExp($pattern, $string, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertRegExp', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that string not match with pattern - * - * @param string $pattern - * @param string $string - * @param string $message - * @see \Codeception\Module\Asserts::assertNotRegExp() - */ - public function assertNotRegExp($pattern, $string, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotRegExp', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that variable is empty. - * - * @param $actual - * @param string $message - * @see \Codeception\Module\Asserts::assertEmpty() - */ - public function assertEmpty($actual, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertEmpty', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that variable is not empty. - * - * @param $actual - * @param string $message - * @see \Codeception\Module\Asserts::assertNotEmpty() - */ - public function assertNotEmpty($actual, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotEmpty', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that variable is NULL - * - * @param $actual - * @param string $message - * @see \Codeception\Module\Asserts::assertNull() - */ - public function assertNull($actual, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNull', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that variable is not NULL - * - * @param $actual - * @param string $message - * @see \Codeception\Module\Asserts::assertNotNull() - */ - public function assertNotNull($actual, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertNotNull', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that condition is positive. - * - * @param $condition - * @param string $message - * @see \Codeception\Module\Asserts::assertTrue() - */ - public function assertTrue($condition, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertTrue', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks that condition is negative. - * - * @param $condition - * @param string $message - * @see \Codeception\Module\Asserts::assertFalse() - */ - public function assertFalse($condition, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertFalse', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks if file exists - * - * @param string $filename - * @param string $message - * @see \Codeception\Module\Asserts::assertFileExists() - */ - public function assertFileExists($filename, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertFileExists', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Checks if file doesn't exists - * - * @param string $filename - * @param string $message - * @see \Codeception\Module\Asserts::assertFileNotExists() - */ - public function assertFileNotExists($filename, $message = null) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('assertFileNotExists', func_get_args())); - } - - - /** - * [!] Method is generated. Documentation taken from corresponding module. - * - * Fails the test with message. - * - * @param $message - * @see \Codeception\Module\Asserts::fail() - */ - public function fail($message) { - return $this->getScenario()->runStep(new \Codeception\Step\Action('fail', func_get_args())); - } -} diff --git a/tests/acceptance.suite.yml b/tests/acceptance.suite.yml deleted file mode 100644 index 2269e0c298..0000000000 --- a/tests/acceptance.suite.yml +++ /dev/null @@ -1,12 +0,0 @@ -# Codeception Test Suite Configuration -# -# Suite for acceptance tests. -# Perform tests in browser using the WebDriver or PhpBrowser. -# If you need both WebDriver and PHPBrowser tests - create a separate suite. - -class_name: AcceptanceTester -modules: - enabled: - - PhpBrowser: - url: http://localhost/myapp - - \Helper\Acceptance \ No newline at end of file diff --git a/tests/acceptance/_bootstrap.php b/tests/acceptance/_bootstrap.php deleted file mode 100644 index 8a88555806..0000000000 --- a/tests/acceptance/_bootstrap.php +++ /dev/null @@ -1,2 +0,0 @@ -createAccount(); + } + + /** + * This method is called before the first test of this test class is run. + * + * @since Method available since Release 3.4.0 + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + + } + + /** + * + */ + public function createAccount() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + if (is_null($this->account)) { + $this->account = FactoryMuffin::create('FireflyIII\Models\Account'); + $this->account->user_id = $user->id; + $this->account->save(); + } + } + + /** + * @covers FireflyIII\Http\Controllers\AccountController::create + */ + public function testCreate() + { + $pref = FactoryMuffin::create('FireflyIII\Models\Preference'); + $pref->data = '1M'; + $this->be($pref->user); + + + Preferences::shouldReceive('get')->withArgs(['viewRange', '1M'])->andReturn($pref); + + + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + Preferences::shouldReceive('get')->withArgs(['language', 'en'])->andReturn($language); + + // CURRENCY: + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + Amount::shouldReceive('getDefaultCurrency')->andReturn($currency); + Amount::shouldReceive('getAllCurrencies')->andReturn([$currency]); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + + $this->call('GET', '/accounts/create/asset'); + $this->assertResponseOk(); + + + $this->assertViewHas('subTitle', 'Create a new asset account'); + $this->assertViewHas('subTitleIcon', 'fa-money'); + $this->assertViewHas('what', 'asset'); + + } + + /** + * @covers FireflyIII\Http\Controllers\AccountController::delete + */ + public function testDelete() + { + + $this->be($this->account->user); + $this->call('GET', '/accounts/delete/' . $this->account->id); + $this->assertResponseOk(); + $this->assertViewHas('subTitle', 'Delete ' . strtolower(e($this->account->accountType->type)) . ' "' . e($this->account->name) . '"'); + + } + + /** + * @covers FireflyIII\Http\Controllers\AccountController::destroy + */ + public function testDestroy() + { + // fake an account. + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + $this->be($account->user); + + // mock: + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('destroy')->andReturn(true); + + // post it! + $this->call('POST', '/accounts/destroy/' . $account->id, ['_token' => 'replaceme']); + $this->assertSessionHas('success'); + $this->assertResponseStatus(302); + } + + /** + * @covers FireflyIII\Http\Controllers\AccountController::edit + */ + public function testEdit() + { + // fake an account. + + $this->be($this->account->user); + $this->assertCount(1, DB::table('accounts')->where('id', $this->account->id)->whereNull('deleted_at')->get()); + + // create a transaction journal that will act as opening balance: + $openingBalance = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('openingBalanceTransaction')->andReturn($openingBalance); + + // create a transaction that will be returned for the opening balance transaction: + $opening = FactoryMuffin::create('FireflyIII\Models\Transaction'); + $repository->shouldReceive('getFirstTransaction')->andReturn($opening); + + // CURRENCY: + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + Amount::shouldReceive('getDefaultCurrency')->andReturn($currency); + Amount::shouldReceive('getAllCurrencies')->andReturn([$currency]); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + // get edit page: + $this->call('GET', '/accounts/edit/' . $this->account->id); + + // assert stuff: + $this->assertResponseOk(); + $this->assertSessionHas('preFilled'); + $this->assertViewHas('subTitle', 'Edit ' . strtolower(e($this->account->accountType->type)) . ' "' . e($this->account->name) . '"'); + + + } + + /** + * @covers FireflyIII\Http\Controllers\AccountController::index + */ + public function testIndex() + { + // an account: + $this->be($this->account->user); + + $collection = new Collection; + $collection->push($this->account); + + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('getAccounts')->andReturn($collection); + $repository->shouldReceive('countAccounts')->andReturn(1); + $repository->shouldReceive('getLastActivity')->andReturn(null); + + Amount::shouldReceive('format')->andReturn(''); + Amount::shouldReceive('getCurrencyCode')->andReturn('A'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + + // put stuff in session: + $this->session(['start' => new Carbon, 'end' => new Carbon]); + + // get edit page: + $this->call('GET', '/accounts/asset'); + $this->assertResponseOk(); + $this->assertViewHas('what', 'asset'); + + } + + /** + * @covers FireflyIII\Http\Controllers\AccountController::show + */ + public function testShow() + { + // an account: + $this->be($this->account->user); + + // mock! + Amount::shouldReceive('getCurrencyCode')->andReturn('A'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('getJournals')->andReturn(new LengthAwarePaginator([], 0, 10)); + + // get edit page: + $this->call('GET', '/accounts/show/' . $this->account->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\AccountController::store + */ + public function testStore() + { + // an account: + $this->be($this->account->user); + + $data = [ + 'name' => 'New test account ' . rand(1, 1000), + 'what' => 'asset', + 'virtualBalance' => 0, + 'accountRole' => 'defaultAsset', + 'openingBalance' => 20, + 'openingBalanceDate' => date('Y-m-d'), + 'openingBalanceCurrency' => 1, + '_token' => 'replaceme' + ]; + + // fake validation routine: + $request = $this->mock('FireflyIII\Http\Requests\AccountFormRequest'); + $request->shouldReceive('input')->andReturn(''); + + // fake store routine: + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('store')->andReturn($this->account); + + $this->call('POST', '/accounts/store', $data); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + + } + + /** + * @covers FireflyIII\Http\Controllers\AccountController::store + */ + public function testStoreAndRedirect() + { + // an account: + $this->be($this->account->user); + + $data = [ + 'name' => 'New test account ' . rand(1, 1000), + 'what' => 'asset', + 'virtualBalance' => 0, + 'accountRole' => 'defaultAsset', + 'openingBalance' => 20, + 'openingBalanceDate' => date('Y-m-d'), + 'openingBalanceCurrency' => 1, + '_token' => 'replaceme', + 'create_another' => 1, + ]; + + // fake validation routine: + $request = $this->mock('FireflyIII\Http\Requests\AccountFormRequest'); + $request->shouldReceive('input')->andReturn(''); + + // fake store routine: + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('store')->andReturn($this->account); + + $this->call('POST', '/accounts/store', $data); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + + } + + /** + * @covers FireflyIII\Http\Controllers\AccountController::update + */ + public function testUpdate() + { + + // an account: + $this->be($this->account->user); + + $data = [ + 'name' => 'Edited test account ' . rand(1, 1000), + 'active' => 1, + 'accountRole' => 'defaultAsset', + 'virtualBalance' => 0, + 'openingBalance' => 25, + 'openingBalanceDate' => date('Y-m-d'), + 'openingBalanceCurrency' => 1, + '_token' => 'replaceme' + ]; + + // fake validation routine: + $request = $this->mock('FireflyIII\Http\Requests\AccountFormRequest'); + $request->shouldReceive('input')->andReturn(''); + + // fake update routine: + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('update')->andReturn($this->account); + + $this->call('POST', '/accounts/update/' . $this->account->id, $data); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + } + + /** + * @covers FireflyIII\Http\Controllers\AccountController::update + */ + public function testUpdateAndRedirect() + { + + // an account: + $this->be($this->account->user); + + $data = [ + 'name' => 'Edited test account ' . rand(1, 1000), + 'active' => 1, + 'accountRole' => 'defaultAsset', + 'virtualBalance' => 0, + 'openingBalance' => 25, + 'openingBalanceDate' => date('Y-m-d'), + 'openingBalanceCurrency' => 1, + '_token' => 'replaceme', + 'return_to_edit' => 1, + ]; + + // fake validation routine: + $request = $this->mock('FireflyIII\Http\Requests\AccountFormRequest'); + $request->shouldReceive('input')->andReturn(''); + + // fake update routine: + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('update')->andReturn($this->account); + + $this->call('POST', '/accounts/update/' . $this->account->id, $data); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + } + +} diff --git a/tests/controllers/AuthControllerTest.php b/tests/controllers/AuthControllerTest.php new file mode 100644 index 0000000000..2517e19b8c --- /dev/null +++ b/tests/controllers/AuthControllerTest.php @@ -0,0 +1,78 @@ + 'test@example.com', + 'password' => 'onetwothree', + 'password_confirmation' => 'onetwothree', + '_token' => 'replaceMe' + ]; + $this->call('POST', '/auth/register', $data); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + } + + /** + * @covers FireflyIII\Http\Controllers\Auth\AuthController::postRegister + * @covers FireflyIII\Http\Controllers\Auth\AuthController::validator + * @covers FireflyIII\Http\Controllers\Auth\AuthController::create + */ + public function testPostRegisterFails() + { + + $data = [ + 'email' => 'test@example.com', + 'password' => 'onetwothree', + 'password_confirmation' => 'onetwofour', + '_token' => 'replaceMe' + ]; + $this->call('POST', '/auth/register', $data); + $this->assertResponseStatus(302); + + + } + +} diff --git a/tests/controllers/BillControllerTest.php b/tests/controllers/BillControllerTest.php new file mode 100644 index 0000000000..3fc9e690b2 --- /dev/null +++ b/tests/controllers/BillControllerTest.php @@ -0,0 +1,273 @@ +be($bill->user); + + // CURRENCY: + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + Amount::shouldReceive('getDefaultCurrency')->andReturn($currency); + Amount::shouldReceive('getAllCurrencies')->andReturn([$currency]); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + $this->call('GET', '/bills/create'); + $this->assertViewHas('subTitle', 'Create new bill'); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\BillController::delete + */ + public function testDelete() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $this->be($bill->user); + $this->call('GET', '/bills/delete/' . $bill->id); + $this->assertViewHas('subTitle', 'Delete bill "' . e($bill->name) . '"'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\BillController::destroy + */ + public function testDestroy() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $this->be($bill->user); + + $repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + $repository->shouldReceive('destroy')->andReturn(true); + + + $this->call('POST', '/bills/destroy/' . $bill->id, ['_token' => 'replaceMe']); + $this->assertSessionHas('success', 'The bill was deleted.'); + $this->assertResponseStatus(302); + } + + /** + * @covers FireflyIII\Http\Controllers\BillController::edit + */ + public function testEdit() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $this->be($bill->user); + + // CURRENCY: + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + Amount::shouldReceive('getDefaultCurrency')->andReturn($currency); + Amount::shouldReceive('getAllCurrencies')->andReturn([$currency]); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + $this->call('GET', '/bills/edit/' . $bill->id); + $this->assertViewHas('subTitle', 'Edit bill "' . e($bill->name) . '"'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\BillController::index + */ + public function testIndex() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $this->be($bill->user); + + $collection = new Collection; + $collection->push($bill); + + Amount::shouldReceive('format')->andReturn('XX'); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + $repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + $repository->shouldReceive('getBills')->once()->andReturn($collection); + $repository->shouldReceive('nextExpectedMatch')->with($bill)->andReturn(new Carbon); + $repository->shouldReceive('lastFoundMatch')->with($bill)->andReturn(new Carbon); + + $this->call('GET', '/bills'); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\BillController::rescan + */ + public function testRescan() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $collection = new Collection; + $this->be($bill->user); + $collection->push($journal); + + $repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + $repository->shouldReceive('getPossiblyRelatedJournals')->once()->andReturn($collection); + $repository->shouldReceive('scan'); + + $this->call('GET', '/bills/rescan/' . $bill->id); + $this->assertResponseStatus(302); + $this->assertSessionHas('success', 'Rescanned everything.'); + + + } + + /** + * @covers FireflyIII\Http\Controllers\BillController::rescan + */ + public function testRescanInactive() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $bill->active = 0; + $bill->save(); + $this->be($bill->user); + + $this->call('GET', '/bills/rescan/' . $bill->id); + $this->assertResponseStatus(302); + $this->assertSessionHas('warning', 'Inactive bills cannot be scanned.'); + + } + + /** + * @covers FireflyIII\Http\Controllers\BillController::show + */ + public function testShow() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $collection = new Collection; + + $bill->save(); + $this->be($bill->user); + $collection->push($journal); + + + $repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + $repository->shouldReceive('getJournals')->once()->andReturn($collection); + $repository->shouldReceive('nextExpectedMatch')->once()->andReturn(new Carbon); + + Amount::shouldReceive('format')->andReturn('XX'); + Amount::shouldReceive('formatJournal')->andReturn('XX'); + Amount::shouldReceive('getCurrencyCode')->andReturn('XX'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + + $this->call('GET', '/bills/show/' . $bill->id); + } + + /** + * @covers FireflyIII\Http\Controllers\BillController::store + */ + public function testStore() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + $request = $this->mock('FireflyIII\Http\Requests\BillFormRequest'); + + $this->be($bill->user); + $request->shouldReceive('getBillData')->once()->andReturn([]); + $repository->shouldReceive('store')->with([])->andReturn($bill); + + $this->call('POST', '/bills/store', ['_token' => 'replaceMe']); + $this->assertResponseStatus(302); + $this->assertSessionHas('success', 'Bill "' . e($bill->name) . '" stored.'); + } + + /** + * @covers FireflyIII\Http\Controllers\BillController::store + */ + public function testStoreAndRedirect() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + $request = $this->mock('FireflyIII\Http\Requests\BillFormRequest'); + + $this->be($bill->user); + $request->shouldReceive('getBillData')->once()->andReturn([]); + $repository->shouldReceive('store')->with([])->andReturn($bill); + + $this->call('POST', '/bills/store', ['_token' => 'replaceMe', 'create_another' => 1]); + $this->assertResponseStatus(302); + $this->assertSessionHas('success', 'Bill "' . e($bill->name) . '" stored.'); + } + + /** + * @covers FireflyIII\Http\Controllers\BillController::update + */ + public function testUpdate() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + $request = $this->mock('FireflyIII\Http\Requests\BillFormRequest'); + + $this->be($bill->user); + $request->shouldReceive('getBillData')->once()->andReturn([]); + $repository->shouldReceive('update')->andReturn($bill); + + $this->call('POST', '/bills/update/' . $bill->id, ['_token' => 'replaceMe']); + $this->assertResponseStatus(302); + $this->assertSessionHas('success', 'Bill "' . e($bill->name) . '" updated.'); + } + + /** + * @covers FireflyIII\Http\Controllers\BillController::update + */ + public function testUpdateAndRedirect() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + $request = $this->mock('FireflyIII\Http\Requests\BillFormRequest'); + + $this->be($bill->user); + $request->shouldReceive('getBillData')->once()->andReturn([]); + $repository->shouldReceive('update')->andReturn($bill); + + $this->call('POST', '/bills/update/' . $bill->id, ['_token' => 'replaceMe', 'return_to_edit' => 1]); + $this->assertResponseStatus(302); + + } +} diff --git a/tests/controllers/BudgetControllerTest.php b/tests/controllers/BudgetControllerTest.php new file mode 100644 index 0000000000..b74bfe6a60 --- /dev/null +++ b/tests/controllers/BudgetControllerTest.php @@ -0,0 +1,435 @@ +mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $limitRepetition = FactoryMuffin::create('FireflyIII\Models\LimitRepetition'); + $budget = $limitRepetition->budgetlimit->budget; + $this->be($budget->user); + $today = new Carbon; + + $this->session(['start' => $today]); + $repository->shouldReceive('updateLimitAmount')->once()->andReturn($limitRepetition); + $this->call('POST', '/budgets/amount/' . $budget->id, ['amount' => 100, '_token' => 'replaceme']); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::amount + */ + public function testAmountZero() + { + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $limitRepetition = FactoryMuffin::create('FireflyIII\Models\LimitRepetition'); + $budget = $limitRepetition->budgetlimit->budget; + $this->be($budget->user); + $today = new Carbon; + + $this->session(['start' => $today]); + $repository->shouldReceive('updateLimitAmount')->once()->andReturn($limitRepetition); + $this->call('POST', '/budgets/amount/' . $budget->id, ['amount' => 0, '_token' => 'replaceme']); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::create + */ + public function testCreate() + { + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $this->be($budget->user); + $this->call('GET', '/budgets/create'); + $this->assertResponseOk(); + $this->assertViewHas('subTitle', 'Create a new budget'); + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::delete + */ + public function testDelete() + { + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $this->be($budget->user); + $this->call('GET', '/budgets/delete/' . $budget->id); + + $this->assertResponseOk(); + $this->assertViewHas('subTitle', 'Delete budget "' . e($budget->name) . '"'); + $this->assertViewHas('budget'); + $this->assertSessionHas('budgets.delete.url'); + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::destroy + */ + public function testDestroy() + { + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $this->be($budget->user); + + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $repository->shouldReceive('destroy')->andReturn(true); + + $this->call('POST', '/budgets/destroy/' . $budget->id, ['_token' => 'replaceme']); + + $this->assertSessionHas('success', 'The budget "' . e($budget->name) . '" was deleted.'); + + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::edit + */ + public function testEdit() + { + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $this->be($budget->user); + + $this->call('GET', '/budgets/edit/' . $budget->id); + + $this->assertResponseOk(); + $this->assertViewHas('subTitle', 'Edit budget "' . e($budget->name) . '"'); + $this->assertViewHas('budget'); + $this->assertSessionHas('budgets.edit.url'); + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::index + */ + public function testIndex() + { + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $this->be($budget->user); + $collection = new Collection; + $collection->push($budget); + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $repetition = FactoryMuffin::create('FireflyIII\Models\LimitRepetition'); + + $repository->shouldReceive('getActiveBudgets')->once()->andReturn($collection); + $repository->shouldReceive('getInactiveBudgets')->once()->andReturn($collection); + $repository->shouldReceive('cleanupBudgets')->once(); + $repository->shouldReceive('spentInPeriodCorrected')->once(); + $repository->shouldReceive('getCurrentRepetition')->once()->andReturn($repetition); + Amount::shouldReceive('getCurrencySymbol')->andReturn('x'); + Amount::shouldReceive('format')->andReturn('x'); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + Amount::shouldReceive('getDefaultCurrency')->andReturn($currency); + $this->call('GET', '/budgets'); + + $this->assertResponseOk(); + $this->assertViewHas('budgets'); + $this->assertViewHas('inactive'); + $this->assertViewHas('inactive'); + + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::noBudget + */ + public function testNoBudget() + { + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $this->be($budget->user); + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $repository->shouldReceive('getWithoutBudget')->andReturn(new Collection); + + $this->call('GET', '/budgets/list/noBudget'); + $this->assertResponseOk(); + $this->assertViewHas('list'); + $this->assertViewHas('subTitle'); + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::postUpdateIncome + */ + public function testPostUpdateIncome() + { + + + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $this->be($budget->user); + $date = Carbon::now()->startOfMonth()->format('FY'); + Preferences::shouldReceive('set')->once()->withArgs(['budgetIncomeTotal' . $date, 1001]); + Preferences::shouldReceive('mark')->once()->andReturn(true); + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + + // language preference: + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + $language->save(); + Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + + $this->call('POST', '/budgets/income', ['_token' => 'replaceme', 'amount' => 1001]); + $this->assertResponseStatus(302); + $this->assertRedirectedToRoute('budgets.index'); + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::show + */ + public function testShow() + { + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $this->be($budget->user); + + $paginator = new LengthAwarePaginator(new Collection, 0, 20, 1); + + Amount::shouldReceive('getCurrencyCode')->andReturn('x'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + Amount::shouldReceive('format')->andReturn('x'); + $repository->shouldReceive('getJournals')->andReturn($paginator); + $repository->shouldReceive('getBudgetLimits')->andReturn(new Collection); + + + $this->call('GET', '/budgets/show/' . $budget->id); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::show + */ + public function testShowInvalidRepetition() + { + + $repetition = FactoryMuffin::create('FireflyIII\Models\LimitRepetition'); + $budget = $repetition->budgetLimit->budget; + $otherBudget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $otherBudget->user_id = $budget->user_id; + $otherBudget->save(); + + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $this->be($otherBudget->user); + + Amount::shouldReceive('getCurrencyCode')->andReturn('x'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + Amount::shouldReceive('format')->andReturn('x'); + $repository->shouldReceive('getJournals')->andReturn(new Collection); + $repository->shouldReceive('getBudgetLimits')->andReturn(new Collection); + + + $this->call('GET', '/budgets/show/' . $otherBudget->id . '/' . $repetition->id); + $this->assertResponseOk(); + $this->assertViewHas('message', 'Invalid selection.'); + + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::show + */ + public function testShowRepetition() + { + $repetition = FactoryMuffin::create('FireflyIII\Models\LimitRepetition'); + $budget = $repetition->budgetLimit->budget; + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $this->be($budget->user); + + $paginator = new LengthAwarePaginator(new Collection, 0, 20, 1); + + Amount::shouldReceive('getCurrencyCode')->andReturn('x'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + Amount::shouldReceive('format')->andReturn('x'); + $repository->shouldReceive('getJournals')->andReturn($paginator); + $repository->shouldReceive('getBudgetLimits')->andReturn(new Collection); + + + $this->call('GET', '/budgets/show/' . $budget->id . '/' . $repetition->id); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::store + */ + public function testStore() + { + // a budget: + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $this->be($budget->user); + + $data = [ + 'name' => 'New test budget ' . rand(1, 1000), + '_token' => 'replaceme' + ]; + + // fake validation routine: + $request = $this->mock('FireflyIII\Http\Requests\BudgetFormRequest'); + $request->shouldReceive('input')->andReturn(''); + + // fake store routine: + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $repository->shouldReceive('store')->andReturn($budget); + + $this->call('POST', '/budgets/store', $data); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::store + */ + public function testStoreAndRedirect() + { + // a budget: + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $this->be($budget->user); + + $data = [ + 'name' => 'New test budget ' . rand(1, 1000), + '_token' => 'replaceme', + 'create_another' => 1, + ]; + + // fake validation routine: + $request = $this->mock('FireflyIII\Http\Requests\BudgetFormRequest'); + $request->shouldReceive('input')->andReturn(''); + + // fake store routine: + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $repository->shouldReceive('store')->andReturn($budget); + + $this->call('POST', '/budgets/store', $data); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::update + */ + public function testUpdate() + { + + // a budget: + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $this->be($budget->user); + + $data = [ + 'name' => 'Edited test account ' . rand(1, 1000), + 'active' => 1, + '_token' => 'replaceme' + ]; + + // fake validation routine: + $request = $this->mock('FireflyIII\Http\Requests\BudgetFormRequest'); + $request->shouldReceive('input')->andReturn(''); + + // fake update routine: + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $repository->shouldReceive('update')->andReturn($budget); + + $this->call('POST', '/budgets/update/' . $budget->id, $data); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::update + */ + public function testUpdateAndRedirect() + { + + // a budget: + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $this->be($budget->user); + + $data = [ + 'name' => 'Edited test account ' . rand(1, 1000), + 'active' => 1, + '_token' => 'replaceme', + 'return_to_edit' => 1, + ]; + + // fake validation routine: + $request = $this->mock('FireflyIII\Http\Requests\BudgetFormRequest'); + $request->shouldReceive('input')->andReturn(''); + + // fake update routine: + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $repository->shouldReceive('update')->andReturn($budget); + + $this->call('POST', '/budgets/update/' . $budget->id, $data); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + } + + /** + * @covers FireflyIII\Http\Controllers\BudgetController::updateIncome + */ + public function testUpdateIncome() + { + + // a budget: + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $this->be($budget->user); + $date = Carbon::now()->format('FY'); + $pref = FactoryMuffin::create('FireflyIII\Models\Preference'); + Preferences::shouldReceive('get')->withArgs(['budgetIncomeTotal' . $date, 1000])->andReturn($pref); + Amount::shouldReceive('format')->andReturn('xx'); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + + // language preference: + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + $language->save(); + Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + + + $this->call('GET', '/budgets/income'); + $this->assertResponseOk(); + $this->assertViewHas('amount'); + } +} diff --git a/tests/controllers/CategoryControllerTest.php b/tests/controllers/CategoryControllerTest.php new file mode 100644 index 0000000000..6fa7f76b00 --- /dev/null +++ b/tests/controllers/CategoryControllerTest.php @@ -0,0 +1,263 @@ +be($category->user); + + $this->call('GET', '/categories/create'); + $this->assertResponseOk(); + $this->assertViewHas('subTitle', 'Create a new category'); + } + + /** + * @covers FireflyIII\Http\Controllers\CategoryController::delete + */ + public function testDelete() + { + + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->be($category->user); + + $this->call('GET', '/categories/delete/' . $category->id); + $this->assertResponseOk(); + $this->assertViewHas('subTitle', 'Delete category "' . e($category->name) . '"'); + } + + /** + * @covers FireflyIII\Http\Controllers\CategoryController::destroy + */ + public function testDestroy() + { + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->be($category->user); + + $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + $repository->shouldReceive('destroy'); + + $this->call('POST', '/categories/destroy/' . $category->id, ['_token' => 'replaceMe']); + $this->assertResponseStatus(302); + $this->assertSessionHas('success', 'The category "' . e($category->name) . '" was deleted.'); + } + + /** + * @covers FireflyIII\Http\Controllers\CategoryController::edit + */ + public function testEdit() + { + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->be($category->user); + + $this->call('GET', '/categories/edit/' . $category->id); + $this->assertResponseOk(); + $this->assertViewHas('subTitle', 'Edit category "' . e($category->name) . '"'); + } + + /** + * @covers FireflyIII\Http\Controllers\CategoryController::index + */ + public function testIndex() + { + $collection = new Collection; + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->be($category->user); + $collection->push($category); + + Amount::shouldReceive('getCurrencyCode')->andReturn('xx'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + $repository->shouldReceive('getCategories')->andReturn($collection); + $repository->shouldReceive('getLatestActivity')->andReturn(new Carbon); + + $this->call('GET', '/categories'); + $this->assertResponseOk(); + $this->assertViewHas('categories'); + } + + /** + * @covers FireflyIII\Http\Controllers\CategoryController::noCategory + */ + public function testNoCategory() + { + $collection = new Collection; + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $this->be($journal->user); + $collection->push($journal); + + Amount::shouldReceive('format')->andReturn('xx'); + Amount::shouldReceive('getCurrencyCode')->andReturn('xx'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + + $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + $repository->shouldReceive('getWithoutCategory')->andReturn($repository); + + $this->call('GET', '/categories/list/noCategory'); + $this->assertResponseOk(); + $this->assertViewHas('subTitle'); + + + } + + /** + * @covers FireflyIII\Http\Controllers\CategoryController::show + */ + public function testShow() + { + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $collection = new Collection; + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $this->be($category->user); + $collection->push($journal); + + $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + + $repository->shouldReceive('getJournals')->andReturn($collection); + $repository->shouldReceive('countJournals')->andReturn(1); + + Amount::shouldReceive('format')->andReturn('xx'); + Amount::shouldReceive('getCurrencyCode')->andReturn('xx'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + Amount::shouldReceive('formatJournal')->andReturn('xx'); + + $this->call('GET', '/categories/show/' . $category->id); + $this->assertResponseOk(); + $this->assertViewHas('hideCategory', true); + + } + + + /** + * @covers FireflyIII\Http\Controllers\CategoryController::store + */ + public function testStore() + { + // create + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->be($category->user); + + // mock + $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + $request = $this->mock('FireflyIII\Http\Requests\CategoryFormRequest'); + + // expect + $repository->shouldReceive('store')->andReturn($category); + $request->shouldReceive('input')->andReturn(''); + + $this->call('POST', '/categories/store', ['_token' => 'replaceMe', 'name' => 'Bla bla #' . rand(1, 1000)]); + $this->assertResponseStatus(302); + $this->assertSessionHas('success', 'New category "' . $category->name . '" stored!'); + } + + /** + * @covers FireflyIII\Http\Controllers\CategoryController::store + */ + public function testStoreAndRedirect() + { + // create + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->be($category->user); + + // mock: + $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + $request = $this->mock('FireflyIII\Http\Requests\CategoryFormRequest'); + + // fake: + $repository->shouldReceive('store')->andReturn($category); + $request->shouldReceive('input')->andReturn(''); + + + $this->call('POST', '/categories/store', ['_token' => 'replaceMe', 'create_another' => 1, 'name' => 'Bla bla #' . rand(1, 1000)]); + $this->assertResponseStatus(302); + $this->assertSessionHas('success', 'New category "' . $category->name . '" stored!'); + } + + /** + * @covers FireflyIII\Http\Controllers\CategoryController::update + */ + public function testUpdate() + { + // create + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->be($category->user); + + // mock + $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + $request = $this->mock('FireflyIII\Http\Requests\CategoryFormRequest'); + + // expect + $repository->shouldReceive('update')->andReturn($category); + $request->shouldReceive('input')->andReturn(''); + + $this->call('POST', '/categories/update/' . $category->id, ['_token' => 'replaceMe', 'name' => 'Bla bla #' . rand(1, 1000)]); + $this->assertResponseStatus(302); + $this->assertSessionHas('success', 'Category "' . $category->name . '" updated.'); + } + + /** + * @covers FireflyIII\Http\Controllers\CategoryController::update + */ + public function testUpdateAndRedirect() + { + // create + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->be($category->user); + + // mock + $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + $request = $this->mock('FireflyIII\Http\Requests\CategoryFormRequest'); + + // expect + $request->shouldReceive('input')->andReturn(''); + $repository->shouldReceive('update')->andReturn($category); + + + $this->call('POST', '/categories/update/' . $category->id, ['_token' => 'replaceMe', 'return_to_edit' => 1, 'name' => 'Bla bla #' . rand(1, 1000)]); + $this->assertResponseStatus(302); + $this->assertSessionHas('success', 'Category "' . $category->name . '" updated.'); + } +} diff --git a/tests/controllers/CurrencyControllerTest.php b/tests/controllers/CurrencyControllerTest.php new file mode 100644 index 0000000000..50153b50b3 --- /dev/null +++ b/tests/controllers/CurrencyControllerTest.php @@ -0,0 +1,279 @@ +be($user); + + $this->call('GET', '/currency/create'); + $this->assertResponseOk(); + $this->assertViewHas('subTitle', 'Create a new currency'); + $this->assertViewHas('subTitleIcon', 'fa-plus'); + + } + + /** + * @covers FireflyIII\Http\Controllers\CurrencyController::defaultCurrency + */ + public function testDefaultCurrency() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + + $this->call('GET', '/currency/default/' . $currency->id); + $this->assertResponseStatus(302); + $this->assertSessionHas('success', $currency->name . ' is now the default currency.'); + } + + /** + * @covers FireflyIII\Http\Controllers\CurrencyController::delete + */ + public function testDelete() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + + $repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface'); + $repository->shouldReceive('countJournals')->andReturn(0); + + $this->call('GET', '/currency/delete/' . $currency->id); + $this->assertResponseOk(); + $this->assertViewHas('currency'); + + } + + /** + * @covers FireflyIII\Http\Controllers\CurrencyController::delete + */ + public function testDeleteUnable() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + + $repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface'); + $repository->shouldReceive('countJournals')->andReturn(1); + + $this->call('GET', '/currency/delete/' . $currency->id); + $this->assertResponseStatus(302); + $this->assertSessionHas('error'); + + } + + /** + * @covers FireflyIII\Http\Controllers\CurrencyController::destroy + */ + public function testDestroy() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $role = FactoryMuffin::create('FireflyIII\Models\Role'); + $role->name = 'owner'; + $role->save(); + $user->attachRole($role); + + $this->be($user); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + + $repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface'); + $repository->shouldReceive('countJournals')->andReturn(0); + + $this->call('POST', '/currency/destroy/' . $currency->id, ['_token' => 'replaceMe']); + $this->assertResponseStatus(302); + $this->assertSessionHas('success', 'Currency "' . e($currency->name) . '" deleted'); + + } + + /** + * @covers FireflyIII\Http\Controllers\CurrencyController::destroy + */ + public function testDestroyUnable() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $role = FactoryMuffin::create('FireflyIII\Models\Role'); + $role->name = 'owner'; + $role->save(); + $user->attachRole($role); + $this->be($user); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + + $repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface'); + $repository->shouldReceive('countJournals')->andReturn(1); + + $this->call('POST', '/currency/destroy/' . $currency->id, ['_token' => 'replaceMe']); + $this->assertResponseStatus(302); + $this->assertSessionHas('error'); + + } + + /** + * @covers FireflyIII\Http\Controllers\CurrencyController::edit + */ + public function testEdit() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + + $repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface'); + $repository->shouldReceive('countJournals')->andReturn(0); + + $this->call('GET', '/currency/edit/' . $currency->id); + $this->assertResponseOk(); + $this->assertViewHas('currency'); + } + + /** + * @covers FireflyIII\Http\Controllers\CurrencyController::index + */ + public function testIndex() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($user); + + $repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface'); + $repository->shouldReceive('get')->andReturn(new Collection); + $repository->shouldReceive('getCurrencyByPreference')->andReturn($currency); + + $this->call('GET', '/currency'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\CurrencyController::store + */ + public function testStore() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $role = FactoryMuffin::create('FireflyIII\Models\Role'); + $role->name = 'owner'; + $role->save(); + $user->attachRole($role); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($user); + + $repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface'); + $request = $this->mock('FireflyIII\Http\Requests\CurrencyFormRequest'); + $request->shouldReceive('getCurrencyData')->andReturn([]); + $repository->shouldReceive('store')->andReturn($currency); + + $this->call('POST', '/currency/store', ['_token' => 'replaceMe']); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + + + } + + /** + * @covers FireflyIII\Http\Controllers\CurrencyController::store + */ + public function testStoreAndReturn() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $role = FactoryMuffin::create('FireflyIII\Models\Role'); + $role->name = 'owner'; + $role->save(); + $user->attachRole($role); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($user); + + $repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface'); + $request = $this->mock('FireflyIII\Http\Requests\CurrencyFormRequest'); + $request->shouldReceive('getCurrencyData')->andReturn([]); + $repository->shouldReceive('store')->andReturn($currency); + + $this->call('POST', '/currency/store', ['_token' => 'replaceMe', 'create_another' => 1]); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + + + } + + /** + * @covers FireflyIII\Http\Controllers\CurrencyController::update + */ + public function testUpdate() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $role = FactoryMuffin::create('FireflyIII\Models\Role'); + $role->name = 'owner'; + $role->save(); + $user->attachRole($role); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($user); + + $repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface'); + $request = $this->mock('FireflyIII\Http\Requests\CurrencyFormRequest'); + $request->shouldReceive('getCurrencyData')->andReturn([]); + $repository->shouldReceive('update')->andReturn($currency); + + $this->call('POST', '/currency/update/' . $currency->id, ['_token' => 'replaceMe']); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + } + + /** + * @covers FireflyIII\Http\Controllers\CurrencyController::update + */ + public function testUpdateAndReturn() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $role = FactoryMuffin::create('FireflyIII\Models\Role'); + $role->name = 'owner'; + $role->save(); + $user->attachRole($role); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($user); + + $repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface'); + $request = $this->mock('FireflyIII\Http\Requests\CurrencyFormRequest'); + $request->shouldReceive('getCurrencyData')->andReturn([]); + $repository->shouldReceive('update')->andReturn($currency); + + $this->call('POST', '/currency/update/' . $currency->id, ['_token' => 'replaceMe', 'return_to_edit' => 1]); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + } +} diff --git a/tests/controllers/HelpControllerTest.php b/tests/controllers/HelpControllerTest.php new file mode 100644 index 0000000000..b7f40c5f5a --- /dev/null +++ b/tests/controllers/HelpControllerTest.php @@ -0,0 +1,104 @@ +be($user); + // mock some stuff. + $interface = $this->mock('FireflyIII\Helpers\Help\HelpInterface'); + $interface->shouldReceive('hasRoute')->once()->with('accounts.index')->andReturn(true); + $interface->shouldReceive('getFromCache')->once()->with('help.accounts.index.title')->andReturn('Title.'); + $interface->shouldReceive('getFromCache')->once()->with('help.accounts.index.text')->andReturn('Text'); + $interface->shouldReceive('inCache')->andReturn(true); + + + $this->call('GET', '/help/accounts.index'); + $this->assertResponseOk(); + } + + /** + * Everything present and accounted for, but not cached + * + * @covers FireflyIII\Http\Controllers\HelpController::show + */ + public function testGetHelpTextNoCache() + { + // login + $user = FactoryMuffin::create('FireflyIII\User'); + $content = ['title' => 'Bla', 'text' => 'Bla']; + + $this->be($user); + // mock some stuff. + $interface = $this->mock('FireflyIII\Helpers\Help\HelpInterface'); + $interface->shouldReceive('hasRoute')->once()->with('accounts.index')->andReturn(true); + $interface->shouldReceive('getFromGithub')->once()->with('accounts.index')->andReturn($content); + $interface->shouldReceive('putInCache')->once()->withArgs(['accounts.index', $content]); + $interface->shouldReceive('inCache')->once()->andReturn(false); + + + $this->call('GET', '/help/accounts.index'); + $this->assertResponseOk(); + } + + /** + * No such route. + * + * @covers FireflyIII\Http\Controllers\HelpController::show + */ + public function testGetHelpTextNoRoute() + { + // login + $user = FactoryMuffin::create('FireflyIII\User'); + + $this->be($user); + // mock some stuff. + $interface = $this->mock('FireflyIII\Helpers\Help\HelpInterface'); + $interface->shouldReceive('hasRoute')->once()->with('accounts.index')->andReturn(false); + + + $this->call('GET', '/help/accounts.index'); + $this->assertResponseOk(); + } +} diff --git a/tests/controllers/HomeControllerTest.php b/tests/controllers/HomeControllerTest.php new file mode 100644 index 0000000000..f4595b017e --- /dev/null +++ b/tests/controllers/HomeControllerTest.php @@ -0,0 +1,159 @@ +be($user); + + + $this->call('POST', '/daterange', ['end' => $end, 'start' => $start, '_token' => 'replaceme']); + $this->assertResponseOk(); + + $this->assertSessionHas('start'); + $this->assertSessionHas('end'); + + } + + + /** + * @covers FireflyIII\Http\Controllers\HomeController::dateRange + */ + public function testDateRangeWarning() + { + $start = '2014-03-01'; + $end = '2015-03-31'; + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $this->call('POST', '/daterange', ['end' => $end, 'start' => $start, '_token' => 'replaceme']); + $this->assertResponseOk(); + + $this->assertSessionHas('start'); + $this->assertSessionHas('end'); + $this->assertSessionHas('warning'); + } + + /** + * @covers FireflyIII\Http\Controllers\HomeController::flush + */ + public function testFlush() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // create at least one tag: + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->tags()->save($tag); + + $this->call('GET', '/flush'); + $this->assertResponseStatus(302); + + } + + /** + * @covers FireflyIII\Http\Controllers\HomeController::index + */ + public function testIndex() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journals = new Collection([$journal]); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $accounts = new Collection([$account]); + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + $this->be($user); + + // mock ALL THE THINGS! + $repository->shouldReceive('countAccounts')->once()->andReturn(3); + Preferences::shouldReceive('get')->once()->withArgs(['frontPageAccounts', []])->andReturn($preference); + $repository->shouldReceive('getFrontpageAccounts')->once()->with($preference)->andReturn($accounts); + $repository->shouldReceive('getSavingsAccounts')->once()->andReturn($accounts); + $repository->shouldReceive('getPiggyBankAccounts')->once()->andReturn($accounts); + $repository->shouldReceive('sumOfEverything')->once()->andReturn(1); + $repository->shouldReceive('getFrontpageTransactions')->once()->andReturn($journals); + + // language preference: + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + $language->save(); + Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + + + Amount::shouldReceive('getCurrencyCode')->andReturn('EUR'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + Amount::shouldReceive('format')->andReturn('xxx'); + Amount::shouldReceive('formatJournal')->with($journal)->andReturn('xxx'); + + $this->call('GET', '/'); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\HomeController::index + */ + public function testIndexEmpty() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + $this->be($user); + + // mock ALL THE THINGS! + $repository->shouldReceive('countAccounts')->once()->andReturn(0); + + // language preference: + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + $language->save(); + Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + + $this->call('GET', '/'); + $this->assertResponseStatus(302); + $this->assertRedirectedToRoute('new-user.index'); + } + +} diff --git a/tests/controllers/JsonControllerTest.php b/tests/controllers/JsonControllerTest.php new file mode 100644 index 0000000000..51ab83c92c --- /dev/null +++ b/tests/controllers/JsonControllerTest.php @@ -0,0 +1,228 @@ + new Carbon, 'end' => new Carbon]]; + $this->be($bill->user); + + $bills = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + $accounts = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + // mock! + $bills->shouldReceive('getActiveBills')->andReturn($collection); + $bills->shouldReceive('getRanges')->andReturn($ranges); + $bills->shouldReceive('getJournalsInRange')->andReturn(new Collection); + $bills->shouldReceive('billPaymentsInRange')->andReturn(12); + $accounts->shouldReceive('getCreditCards')->andReturn($ccs); + $accounts->shouldReceive('getTransfersInRange')->andReturn(new Collection); + Amount::shouldReceive('format')->andReturn('xx'); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + Steam::shouldReceive('balance')->andReturn(0); + + + $this->call('GET', '/json/box/bills-paid'); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\JsonController::boxBillsUnpaid + */ + public function testBoxBillsUnpaid() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $creditCard = FactoryMuffin::create('FireflyIII\Models\Account'); + $ccs = new Collection([$creditCard]); + $collection = new Collection([$bill]); + $ranges = [['start' => new Carbon, 'end' => new Carbon]]; + $this->be($bill->user); + + $bills = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + $accounts = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + // mock! + $bills->shouldReceive('getActiveBills')->andReturn($collection); + $bills->shouldReceive('getRanges')->andReturn($ranges); + $bills->shouldReceive('getJournalsInRange')->andReturn(new Collection); + $bills->shouldReceive('createFakeBill')->andReturn($bill); + $accounts->shouldReceive('getCreditCards')->andReturn($ccs); + Amount::shouldReceive('format')->andReturn('xx'); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + Steam::shouldReceive('balance')->andReturn(-1); + + $this->call('GET', '/json/box/bills-unpaid'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\JsonController::boxIn + */ + public function testBoxIn() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $repository = $this->mock('FireflyIII\Helpers\Report\ReportQueryInterface'); + $repository->shouldReceive('incomeInPeriodCorrected')->andReturn(new Collection); + Amount::shouldReceive('format')->andReturn('xx'); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + $this->call('GET', '/json/box/in'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\JsonController::boxOut + */ + public function testBoxOut() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $repository = $this->mock('FireflyIII\Helpers\Report\ReportQueryInterface'); + $repository->shouldReceive('expenseInPeriodCorrected')->andReturn(new Collection); + Amount::shouldReceive('format')->andReturn('xx'); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + $this->call('GET', '/json/box/out'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\JsonController::categories + */ + public function testCategories() + { + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->be($category->user); + $categories = new Collection([$category]); + + $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + $repository->shouldReceive('getCategories')->andReturn($categories); + + $this->call('GET', '/json/categories'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\JsonController::expenseAccounts + */ + public function testExpenseAccounts() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $this->be($account->user); + $accounts = new Collection([$account]); + + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('getAccounts')->with(['Expense account', 'Beneficiary account'])->andReturn($accounts); + + $this->call('GET', '/json/expense-accounts'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\JsonController::revenueAccounts + */ + public function testRevenueAccounts() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $this->be($account->user); + $accounts = new Collection([$account]); + + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('getAccounts')->with(['Revenue account'])->andReturn($accounts); + + $this->call('GET', '/json/revenue-accounts'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\JsonController::tags + */ + public function testTags() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->user()->associate($user); + + $tag->save(); + $this->be($tag->user); + $tags = new Collection([$tag]); + + $repository = $this->mock('FireflyIII\Repositories\Tag\TagRepositoryInterface'); + $repository->shouldReceive('get')->andReturn($tags); + + $this->call('GET', '/json/tags'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\JsonController::transactionJournals + */ + public function testTransactionJournals() + { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $type = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $collection = new Collection([$journal]); + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); + $repository->shouldReceive('getTransactionType')->with('withdrawal')->andReturn($type); + $repository->shouldReceive('getJournalsOfType')->with($type)->andReturn($collection); + + + $this->call('GET', '/json/transaction-journals/withdrawal'); + $this->assertResponseOk(); + } + +} diff --git a/tests/controllers/NewUserControllerTest.php b/tests/controllers/NewUserControllerTest.php new file mode 100644 index 0000000000..3016148bd8 --- /dev/null +++ b/tests/controllers/NewUserControllerTest.php @@ -0,0 +1,110 @@ +mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + $this->be($user); + + // mock ALL THE THINGS! + $repository->shouldReceive('countAccounts')->once()->andReturn(0); + + // language preference: + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + $language->save(); + Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + $this->call('GET', '/new-user'); + $this->assertResponseStatus(200); + } + + public function testIndexNoAccounts() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + + $this->be($user); + + // mock ALL THE THINGS! + $repository->shouldReceive('countAccounts')->once()->andReturn(3); + + // language preference: + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + $language->save(); + Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + + $this->call('GET', '/new-user'); + $this->assertResponseStatus(302); + $this->assertRedirectedToRoute('index'); + + } + + public function testPostIndex() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $this->be($user); + + $data = [ + '_token' => 'replaceMe', + 'bank_name' => 'Some Bank', + 'bank_balance' => '100', + 'balance_currency_id' => $currency->id, + 'savings_balance' => '100', + 'credit_card_limit' => '100', + + ]; + + $repository->shouldReceive('store')->andReturn($account); + + $this->call('POST', '/new-user/submit', $data); + $this->assertResponseStatus(302); + $this->assertRedirectedToRoute('index'); + + } + +} diff --git a/tests/controllers/PiggyBankControllerTest.php b/tests/controllers/PiggyBankControllerTest.php new file mode 100644 index 0000000000..884943a5aa --- /dev/null +++ b/tests/controllers/PiggyBankControllerTest.php @@ -0,0 +1,454 @@ +be($piggyBank->account->user); + + + // mock + /** @var Mockery\MockInterface $repository */ + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('leftOnAccount')->withAnyArgs()->andReturn(12); + Amount::shouldReceive('format')->andReturn('XXxx'); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + $this->call('GET', '/piggy-banks/add/' . $piggyBank->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::create + */ + public function testCreate() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $collection = new Collection([$account]); + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // mock + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('getAccounts')->once()->with(['Default account', 'Asset account'])->andReturn($collection); + ExpandedForm::shouldReceive('makeSelectList')->with($collection)->andReturn([]); + + // also cover the view now that we've touched ExpandedForm: + ExpandedForm::shouldReceive('text')->andReturn(''); + ExpandedForm::shouldReceive('select')->andReturn(''); + ExpandedForm::shouldReceive('amount')->andReturn(''); + ExpandedForm::shouldReceive('date')->andReturn(''); + ExpandedForm::shouldReceive('checkbox')->andReturn(''); + ExpandedForm::shouldReceive('optionsList')->andReturn(''); + + $this->call('GET', '/piggy-banks/create'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::delete + */ + public function testDelete() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + + + $this->call('GET', '/piggy-banks/delete/' . $piggyBank->id); + $this->assertResponseOk(); + $this->assertViewHas('subTitle', 'Delete piggy bank "' . e($piggyBank->name) . '"'); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::destroy + */ + public function testDestroy() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + + $repository = $this->mock('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); + $repository->shouldReceive('destroy')->once()->withAnyArgs()->andReturn(true); + + + $this->call('POST', '/piggy-banks/destroy/' . $piggyBank->id, ['_token' => 'replaceMe']); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::edit + */ + public function testEdit() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $piggyBank->targetdate = Carbon::now()->addYear(); + $piggyBank->save(); + $this->be($piggyBank->account->user); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $collection = new Collection([$account]); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('getAccounts')->once()->with(['Default account', 'Asset account'])->andReturn($collection); + ExpandedForm::shouldReceive('makeSelectList')->with($collection)->andReturn([]); + + // also cover the view now that we've touched ExpandedForm: + ExpandedForm::shouldReceive('text')->andReturn(''); + ExpandedForm::shouldReceive('select')->andReturn(''); + ExpandedForm::shouldReceive('amount')->andReturn(''); + ExpandedForm::shouldReceive('date')->andReturn(''); + ExpandedForm::shouldReceive('checkbox')->andReturn(''); + ExpandedForm::shouldReceive('optionsList')->andReturn(''); + + + $this->call('GET', '/piggy-banks/edit/' . $piggyBank->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::edit + */ + public function testEditNullDate() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + $piggyBank->targetdate = null; + $piggyBank->save(); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $collection = new Collection([$account]); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $repository->shouldReceive('getAccounts')->once()->with(['Default account', 'Asset account'])->andReturn($collection); + ExpandedForm::shouldReceive('makeSelectList')->with($collection)->andReturn([]); + + // also cover the view now that we've touched ExpandedForm: + ExpandedForm::shouldReceive('text')->andReturn(''); + ExpandedForm::shouldReceive('select')->andReturn(''); + ExpandedForm::shouldReceive('amount')->andReturn(''); + ExpandedForm::shouldReceive('date')->andReturn(''); + ExpandedForm::shouldReceive('checkbox')->andReturn(''); + ExpandedForm::shouldReceive('optionsList')->andReturn(''); + + + $this->call('GET', '/piggy-banks/edit/' . $piggyBank->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::index + */ + public function testIndex() + { + $piggyBank1 = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $piggyBank2 = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $piggyBank3 = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $piggyBank2->account_id = $piggyBank1->account_id; + $user = FactoryMuffin::create('FireflyIII\User'); + + $piggyBank2->save(); + + $collection = new Collection([$piggyBank1, $piggyBank2, $piggyBank3]); + $this->be($user); + + // mock! + $accounts = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $piggyBanks = $this->mock('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); + + // act! + $piggyBanks->shouldReceive('getPiggyBanks')->once()->andReturn($collection); + Steam::shouldReceive('balance')->andReturn(20); + $accounts->shouldReceive('leftOnAccount')->andReturn(12); + Amount::shouldReceive('format')->andReturn('123'); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + + $this->call('GET', '/piggy-banks'); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::order + */ + public function testOrder() + { + $piggyBank1 = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $piggyBank2 = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // mock! + $piggyBanks = $this->mock('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); + $piggyBanks->shouldReceive('reset')->once(); + $piggyBanks->shouldReceive('setOrder'); + $array = [ + $piggyBank1->id => 0, + $piggyBank2->id => 1, + ]; + + $this->call('POST', '/piggy-banks/sort', ['_token' => 'replaceMe', 'order' => $array]); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::postAdd + */ + public function testPostAdd() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + + // mock! + $accounts = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $piggyBanks = $this->mock('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); + $accounts->shouldReceive('leftOnAccount')->andReturn(20); + $piggyBanks->shouldReceive('createEvent')->once(); + + Amount::shouldReceive('format')->andReturn('something'); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + $this->call('POST', '/piggy-banks/add/' . $piggyBank->id, ['_token' => 'replaceMe']); + $this->assertResponseStatus(302); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::postAdd + */ + public function testPostAddOverdraw() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + + // mock! + $accounts = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $accounts->shouldReceive('leftOnAccount')->andReturn(20); + + Amount::shouldReceive('format')->andReturn('something'); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + $this->call('POST', '/piggy-banks/add/' . $piggyBank->id, ['_token' => 'replaceMe', 'amount' => '10000']); + $this->assertResponseStatus(302); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::postRemove + */ + public function testPostRemove() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + + // mock! + $accounts = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $piggyBanks = $this->mock('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); + $accounts->shouldReceive('leftOnAccount')->andReturn(20); + $piggyBanks->shouldReceive('createEvent')->once(); + + Amount::shouldReceive('format')->andReturn('something'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('something'); + + $this->call('POST', '/piggy-banks/remove/' . $piggyBank->id, ['_token' => 'replaceMe']); + $this->assertResponseStatus(302); + } + + public function testPostRemoveOverdraw() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + + // mock! + $accounts = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $accounts->shouldReceive('leftOnAccount')->andReturn(20); + + Amount::shouldReceive('format')->andReturn('something'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('something'); + + $this->call('POST', '/piggy-banks/remove/' . $piggyBank->id, ['_token' => 'replaceMe', 'amount' => '10000']); + $this->assertResponseStatus(302); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::remove + */ + public function testRemove() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + + Amount::shouldReceive('format')->andReturn('something'); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + + $this->call('GET', '/piggy-banks/remove/' . $piggyBank->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::show + */ + public function testShow() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + + $piggyBanks = $this->mock('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); + $piggyBanks->shouldReceive('getEvents')->andReturn(new Collection); + Amount::shouldReceive('format')->andReturn('something'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('something'); + Amount::shouldReceive('getCurrencyCode')->andReturn('something'); + + + $this->call('GET', '/piggy-banks/show/' . $piggyBank->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::store + */ + public function testStore() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + + $piggyBankData = [ + 'name' => 'Some name' . rand(1, 100), + 'account_id' => $piggyBank->account_id, + 'targetamount' => 100, + 'targetdate' => '', + '_token' => 'replaceMe' + ]; + + // mock! + $piggyBanks = $this->mock('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); + $piggyBanks->shouldReceive('store')->once()->andReturn($piggyBank); + + $this->call('POST', '/piggy-banks/store', $piggyBankData); + $this->assertResponseStatus(302); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::store + */ + public function testStoreCreateAnother() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + + $piggyBankData = [ + 'name' => 'Some name' . rand(1, 100), + 'account_id' => $piggyBank->account_id, + 'targetamount' => 100, + 'targetdate' => '', + 'create_another' => 1, + '_token' => 'replaceMe' + ]; + + // mock! + $piggyBanks = $this->mock('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); + $piggyBanks->shouldReceive('store')->once()->andReturn($piggyBank); + + $this->call('POST', '/piggy-banks/store', $piggyBankData); + $this->assertResponseStatus(302); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::update + */ + public function testUpdate() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + + $piggyBankData = [ + 'name' => 'Some name' . rand(1, 100), + 'account_id' => $piggyBank->account_id, + 'targetamount' => 200, + 'targetdate' => '', + '_token' => 'replaceMe' + ]; + + // mock! + $piggyBanks = $this->mock('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); + $piggyBanks->shouldReceive('update')->once()->andReturn($piggyBank); + + $this->call('POST', '/piggy-banks/update/' . $piggyBank->id, $piggyBankData); + $this->assertResponseStatus(302); + } + + /** + * @covers FireflyIII\Http\Controllers\PiggyBankController::update + */ + public function testUpdateReturnToEdit() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + + $piggyBankData = [ + 'name' => 'Some name' . rand(1, 100), + 'account_id' => $piggyBank->account_id, + 'targetamount' => 200, + 'targetdate' => '', + 'return_to_edit' => 1, + '_token' => 'replaceMe' + ]; + + // mock! + $piggyBanks = $this->mock('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); + $piggyBanks->shouldReceive('update')->once()->andReturn($piggyBank); + + $this->call('POST', '/piggy-banks/update/' . $piggyBank->id, $piggyBankData); + $this->assertResponseStatus(302); + } +} diff --git a/tests/controllers/PreferencesControllerTest.php b/tests/controllers/PreferencesControllerTest.php new file mode 100644 index 0000000000..7631aa2ea8 --- /dev/null +++ b/tests/controllers/PreferencesControllerTest.php @@ -0,0 +1,121 @@ +be($user); + + + // mock: + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + // fake! + $repository->shouldReceive('getAccounts')->with(['Default account', 'Asset account'])->andReturn(new Collection); + Preferences::shouldReceive('get')->once()->withArgs(['viewRange', '1M'])->andReturn($pref); + Preferences::shouldReceive('get')->once()->withArgs(['frontPageAccounts', []])->andReturn($pref); + Preferences::shouldReceive('get')->once()->withArgs(['budgetMaximum', 1000])->andReturn($pref); + Preferences::shouldReceive('get')->withArgs(['currencyPreference', 'EUR'])->andReturn($pref); + Amount::shouldReceive('format')->andReturn('xx'); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + Amount::shouldReceive('getAllCurrencies')->andReturn(new Collection); + Amount::shouldReceive('getDefaultCurrency')->andReturn($currency); + + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + + // language preference: + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + $language->save(); + Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + + $this->call('GET', '/preferences'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\PreferencesController::postIndex + */ + public function testPostIndex() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $data = [ + 'frontPageAccounts' => [1, 2, 3], + '_token' => 'replaceMe', + 'viewRange' => '1M', + 'language' => 'en', + ]; + + // language preference: + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + $language->save(); + Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + + Preferences::shouldReceive('set')->once()->withArgs(['frontPageAccounts', [1, 2, 3]]); + Preferences::shouldReceive('set')->once()->withArgs(['viewRange', '1M']); + Preferences::shouldReceive('set')->once()->withArgs(['budgetMaximum', 0]); + Preferences::shouldReceive('set')->once()->withArgs(['language', 'en']); + Preferences::shouldReceive('mark')->once()->andReturn(true); + + // language preference: + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + $language->save(); + Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + + + $this->call('POST', '/preferences', $data); + $this->assertResponseStatus(302); + } +} diff --git a/tests/controllers/ProfileControllerTest.php b/tests/controllers/ProfileControllerTest.php new file mode 100644 index 0000000000..ff08eef3a2 --- /dev/null +++ b/tests/controllers/ProfileControllerTest.php @@ -0,0 +1,195 @@ +be($user); + + $this->call('GET', '/profile/change-password'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\ProfileController::deleteAccount + */ + public function testDeleteAccount() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $this->call('GET', '/profile/delete-account'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\ProfileController::index + */ + public function testIndex() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $this->call('GET', '/profile'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\ProfileController::postChangePassword + * @covers FireflyIII\Http\Controllers\ProfileController::validatePassword + */ + public function testPostChangePassword() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $user->password = bcrypt('current'); + $user->save(); + $this->be($user); + + $post = [ + 'current_password' => 'current', + 'new_password' => 'something', + 'new_password_confirmation' => 'something', + '_token' => 'replaceMe' + ]; + + $this->call('POST', '/profile/change-password', $post); + + $this->assertRedirectedToRoute('profile'); + $this->assertSessionHas('success', 'Password changed!'); + $this->assertResponseStatus(302); + + } + + /** + * @covers FireflyIII\Http\Controllers\ProfileController::postChangePassword + * @covers FireflyIII\Http\Controllers\ProfileController::validatePassword + */ + public function testPostChangePasswordInvalidCurrent() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $post = [ + 'current_password' => 'currentWrong', + 'new_password' => 'something', + 'new_password_confirmation' => 'something', + '_token' => 'replaceMe' + ]; + + $this->call('POST', '/profile/change-password', $post); + + $this->assertRedirectedToRoute('profile.change-password'); + $this->assertSessionHas('error', 'Invalid current password!'); + $this->assertResponseStatus(302); + + } + + /** + * @covers FireflyIII\Http\Controllers\ProfileController::postChangePassword + * @covers FireflyIII\Http\Controllers\ProfileController::validatePassword + */ + public function testPostChangePasswordNoNewPassword() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $user->password = bcrypt('current'); + $user->save(); + $this->be($user); + + $post = [ + 'current_password' => 'current', + 'new_password' => 'current', + 'new_password_confirmation' => 'current', + '_token' => 'replaceMe' + ]; + + $this->call('POST', '/profile/change-password', $post); + + $this->assertSessionHas('error', 'The idea is to change your password.'); + $this->assertResponseStatus(302); + $this->assertRedirectedToRoute('profile.change-password'); + + + } + + /** + * @covers FireflyIII\Http\Controllers\ProfileController::postDeleteAccount + */ + public function testPostDeleteAccount() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $user->password = bcrypt('current'); + $user->save(); + $this->be($user); + + $post = [ + 'password' => 'current', + '_token' => 'replaceMe' + ]; + + $this->call('POST', '/profile/delete-account', $post); + + $this->assertRedirectedToRoute('index'); + $this->assertResponseStatus(302); + + } + + /** + * @covers FireflyIII\Http\Controllers\ProfileController::postDeleteAccount + */ + public function testPostDeleteAccountInvalidPassword() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $user->password = bcrypt('current'); + $user->save(); + $this->be($user); + + $post = [ + 'password' => 'currentXX', + '_token' => 'replaceMe' + ]; + + $this->call('POST', '/profile/delete-account', $post); + + $this->assertRedirectedToRoute('profile.delete-account'); + $this->assertSessionHas('error', 'Invalid password!'); + $this->assertResponseStatus(302); + + } + +} diff --git a/tests/controllers/ReportControllerTest.php b/tests/controllers/ReportControllerTest.php new file mode 100644 index 0000000000..51791cf341 --- /dev/null +++ b/tests/controllers/ReportControllerTest.php @@ -0,0 +1,172 @@ +be($user); + + // mock stuff + $helper = $this->mock('FireflyIII\Helpers\Report\ReportHelperInterface'); + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + // make shared: + AccountMeta::create( + [ + 'account_id' => $account->id, + 'name' => 'accountRole', + 'data' => 'sharedAsset' + ] + ); + + $helper->shouldReceive('listOfMonths')->andReturn([]); + $helper->shouldReceive('listOfYears')->andReturn([]); + $repository->shouldReceive('getAccounts')->andReturn(new Collection([$account])); + + + $this->call('GET', '/reports'); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\ReportController::month + */ + public function testMonth() + { + $user = FactoryMuffin::create('FireflyIII\User'); + FactoryMuffin::create('FireflyIII\Models\Account'); + $budget1 = FactoryMuffin::create('FireflyIII\Models\Budget'); + $budget1->queryAmount = 12; + $budget2 = FactoryMuffin::create('FireflyIII\Models\Budget'); + $budget2->queryAmount = 0; + $this->be($user); + + // mock! + $helper = $this->mock('FireflyIII\Helpers\Report\ReportHelperInterface'); + + // fake! + $helper->shouldReceive('getAccountReport')->andReturn(new Collection); + $helper->shouldReceive('getIncomeReport')->andReturn(new Collection); + $helper->shouldReceive('getExpenseReport')->andReturn(new Collection); + $helper->shouldReceive('getBudgetReport')->andReturn(new Collection); + $helper->shouldReceive('getCategoryReport')->andReturn(new Collection); + $helper->shouldReceive('getBalanceReport')->andReturn(new Collection); + $helper->shouldReceive('getBillReport')->andReturn(new Collection); + + + $this->call('GET', '/reports/2015/1'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\ReportController::month + */ + public function testMonthShared() + { + $user = FactoryMuffin::create('FireflyIII\User'); + FactoryMuffin::create('FireflyIII\Models\Account'); + $budget1 = FactoryMuffin::create('FireflyIII\Models\Budget'); + $budget1->queryAmount = 12; + $budget2 = FactoryMuffin::create('FireflyIII\Models\Budget'); + $budget2->queryAmount = 0; + $this->be($user); + + // mock! + $helper = $this->mock('FireflyIII\Helpers\Report\ReportHelperInterface'); + + // fake! + $helper->shouldReceive('getAccountReport')->andReturn(new Collection); + $helper->shouldReceive('getIncomeReport')->andReturn(new Collection); + $helper->shouldReceive('getExpenseReport')->andReturn(new Collection); + $helper->shouldReceive('getBudgetReport')->andReturn(new Collection); + $helper->shouldReceive('getCategoryReport')->andReturn(new Collection); + $helper->shouldReceive('getBalanceReport')->andReturn(new Collection); + $helper->shouldReceive('getBillReport')->andReturn(new Collection); + + $this->call('GET', '/reports/2015/1/shared'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\ReportController::year + */ + public function testYear() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + // make shared: + AccountMeta::create( + [ + 'account_id' => $account->id, + 'name' => 'accountRole', + 'data' => 'sharedAsset' + ] + ); + new Collection([$journal]); + + $this->be($user); + + $helper = $this->mock('FireflyIII\Helpers\Report\ReportHelperInterface'); + + + $helper->shouldReceive('getAccountReport')->once()->withAnyArgs()->andReturn([]); + $helper->shouldReceive('getIncomeReport')->once()->withAnyArgs()->andReturn([]); + $helper->shouldReceive('getExpenseReport')->once()->withAnyArgs()->andReturn([]); + + // mock stuff! + Amount::shouldReceive('getDefaultCurrency')->andReturn($currency); + Amount::shouldReceive('getAllCurrencies')->andReturn([$currency]); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + Amount::shouldReceive('format')->andReturn('X'); + + $this->call('GET', '/reports/2015/shared'); + $this->assertResponseOk(); + } + + +} diff --git a/tests/controllers/SearchControllerTest.php b/tests/controllers/SearchControllerTest.php new file mode 100644 index 0000000000..33ac7f9f36 --- /dev/null +++ b/tests/controllers/SearchControllerTest.php @@ -0,0 +1,58 @@ +be($user); + $words = ['Something']; + // mock! + $repository = $this->mock('FireflyIII\Support\Search\SearchInterface'); + $repository->shouldReceive('searchTransactions')->with($words)->once()->andReturn([]); + $repository->shouldReceive('searchAccounts')->with($words)->once()->andReturn([]); + $repository->shouldReceive('searchCategories')->with($words)->once()->andReturn([]); + $repository->shouldReceive('searchBudgets')->with($words)->once()->andReturn([]); + $repository->shouldReceive('searchTags')->with($words)->once()->andReturn([]); + + $this->call('GET', '/search?q=Something'); + $this->assertResponseOk(); + } +} diff --git a/tests/controllers/TagControllerTest.php b/tests/controllers/TagControllerTest.php new file mode 100644 index 0000000000..9b6a29df69 --- /dev/null +++ b/tests/controllers/TagControllerTest.php @@ -0,0 +1,296 @@ +be($user); + + $this->call('GET', '/tags/create'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TagController::delete + */ + public function testDelete() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $this->be($tag->user); + + $this->call('GET', '/tags/delete/' . $tag->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TagController::destroy + */ + public function testDestroy() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $this->be($tag->user); + + $this->call('POST', '/tags/destroy/' . $tag->id, ['_token' => 'replaceMe']); + $this->assertSessionHas('success'); + $this->assertResponseStatus(302); + + } + + /** + * @covers FireflyIII\Http\Controllers\TagController::edit + */ + public function testEdit() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $this->be($tag->user); + + $this->call('GET', '/tags/edit/' . $tag->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TagController::edit + */ + public function testEditBalancingAct() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $type = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $type->type = 'Transfer'; + $type->save(); + $journal->transactionType()->associate($type); + $journal->save(); + $tag->transactionJournals()->save($journal); + $tag->tagMode = 'balancingAct'; + $tag->save(); + $this->be($tag->user); + + $this->call('GET', '/tags/edit/' . $tag->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TagController::edit + */ + public function testEditThreeExpenses() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $type = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $type->type = 'Withdrawal'; + $type->save(); + + for ($i = 0; $i < 3; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->transactionType()->associate($type); + $journal->save(); + $tag->transactionJournals()->save($journal); + } + + + $tag->tagMode = 'nothing'; + $tag->save(); + $this->be($tag->user); + + $this->call('GET', '/tags/edit/' . $tag->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TagController::hideTagHelp + */ + public function testHideTagHelp() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $this->be($tag->user); + + $this->call('POST', '/tags/hideTagHelp/true', ['_token' => 'replaceMe']); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TagController::index + */ + public function testIndex() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $this->be($tag->user); + + $this->call('GET', '/tags'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TagController::edit + */ + public function testMultipleDeposits() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $type = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + for ($i = 0; $i < 3; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->transaction_type_id = $type->id; + $journal->save(); + $tag->transactionJournals()->save($journal); + } + + + $tag->tagMode = 'nothing'; + $tag->save(); + $this->be($tag->user); + + $this->call('GET', '/tags/edit/' . $tag->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TagController::show + */ + public function testShow() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $this->be($tag->user); + + $this->call('GET', '/tags/show/' . $tag->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TagController::store + */ + public function testStore() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $data = [ + '_token' => 'replaceMe', + 'tag' => 'BlaBla' . rand(1, 1000), + 'tagMode' => 'nothing' + ]; + + $this->call('POST', '/tags/store/', $data); + $this->assertResponseStatus(302); + } + + /** + * @covers FireflyIII\Http\Controllers\TagController::store + */ + public function testStoreWithLocation() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $data = [ + '_token' => 'replaceMe', + 'tag' => 'BlaBla' . rand(1, 1000), + 'tagMode' => 'nothing', + 'latitude' => 12, + 'longitude' => 13, + 'zoomLevel' => 3, + 'setTag' => 'true', + 'create_another' => 1, + ]; + + $this->call('POST', '/tags/store/', $data); + $this->assertResponseStatus(302); + } + + public function testUpdate() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $this->be($tag->user); + + $data = [ + '_token' => 'replaceMe', + 'tag' => 'BlaBla' . rand(1, 1000), + 'tagMode' => 'nothing', + 'id' => $tag->id, + ]; + + $this->call('POST', '/tags/update/' . $tag->id, $data); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + } + + public function testUpdateNoNameChange() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $this->be($tag->user); + + $data = [ + '_token' => 'replaceMe', + 'tag' => $tag->tag, + 'tagMode' => 'nothing', + 'id' => $tag->id, + ]; + + $this->call('POST', '/tags/update/' . $tag->id, $data); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + } + + /** + * @covers FireflyIII\Http\Controllers\TagController::update + */ + public function testUpdateWithLocation() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $this->be($tag->user); + + $data = [ + '_token' => 'replaceMe', + 'tag' => 'BlaBla' . rand(1, 1000), + 'tagMode' => 'nothing', + 'id' => $tag->id, + 'latitude' => 12, + 'setTag' => 'true', + 'longitude' => 13, + 'zoomLevel' => 3, + 'return_to_edit' => 1, + ]; + + $this->call('POST', '/tags/update/' . $tag->id, $data); + $this->assertResponseStatus(302); + } + + +} diff --git a/tests/controllers/TransactionControllerTest.php b/tests/controllers/TransactionControllerTest.php new file mode 100644 index 0000000000..814457fe6b --- /dev/null +++ b/tests/controllers/TransactionControllerTest.php @@ -0,0 +1,598 @@ +be($user); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + + // fake! + $repository->shouldReceive('getAccounts')->andReturn(new Collection); + + + $this->call('GET', '/transactions/create/withdrawal?account_id=12'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TransactionController::delete + */ + public function testDelete() + { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $this->be($journal->user); + + $this->call('GET', '/transaction/delete/' . $journal->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TransactionController::destroy + */ + public function testDestroy() + { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $this->be($journal->user); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); + + // fake! + $repository->shouldReceive('delete')->andReturn(true); + + $this->call('POST', '/transaction/destroy/' . $journal->id, ['_token' => 'replaceMe']); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + + } + + /** + * @covers FireflyIII\Http\Controllers\TransactionController::edit + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testEdit() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + // make complete journal: + $accountType = FactoryMuffin::create('FireflyIII\Models\AccountType'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $transaction1 = FactoryMuffin::create('FireflyIII\Models\Transaction'); + $transaction2 = FactoryMuffin::create('FireflyIII\Models\Transaction'); + + $accountType->type = 'Asset account'; + $account->account_type_id = $accountType->id; + + $account->save(); + $transaction1->account_id = $account->id; + $transaction1->transaction_journal_id = $journal->id; + $transaction1->save(); + + $transaction2->account_id = $account->id; + $transaction2->transaction_journal_id = $journal->id; + $transaction2->save(); + + // also add some tags: + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->transactionJournals()->save($journal); + + // and a category and a budget: + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $category->transactionJournals()->save($journal); + $budget->transactionJournals()->save($journal); + + // and a piggy bank event: + $pbEvent = FactoryMuffin::create('FireflyIII\Models\PiggyBankEvent'); + $pbEvent->transaction_journal_id = $journal->id; + $pbEvent->save(); + + $this->be($journal->user); + + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + + // fake! + $repository->shouldReceive('getAccounts')->andReturn(new Collection); + + $this->call('GET', '/transaction/edit/' . $journal->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TransactionController::edit + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testEditCashDestination() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + // make complete journal: + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + //$expense = FactoryMuffin::create('FireflyIII\Models\Account'); // expense account + //$revenue = FactoryMuffin::create('FireflyIII\Models\Account'); // revenue account + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); // asset account + $cash = FactoryMuffin::create('FireflyIII\Models\Account'); // cash account + + $journal->transactions[0]->account_id = $asset->id; + $journal->transactions[0]->amount = -300; + $journal->transactions[0]->save(); + + $journal->transactions[0]->account_id = $cash->id; + $journal->transactions[0]->amount = 300; + $journal->transactions[0]->save(); + + $this->be($journal->user); + // mock! + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + // fake! + $repository->shouldReceive('getAccounts')->andReturn(new Collection); + + $this->call('GET', '/transaction/edit/' . $journal->id); + + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TransactionController::edit + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testEditDeposit() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + // make complete journal: + $accountType = FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $transaction1 = FactoryMuffin::create('FireflyIII\Models\Transaction'); + $transaction2 = FactoryMuffin::create('FireflyIII\Models\Transaction'); + + $accountType->type = 'Asset account'; + $account->account_type_id = $accountType->id; + + $account->save(); + $transaction1->account_id = $account->id; + $transaction1->transaction_journal_id = $journal->id; + $transaction1->save(); + + $transaction2->account_id = $account->id; + $transaction2->transaction_journal_id = $journal->id; + $transaction2->save(); + + // also add some tags: + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->transactionJournals()->save($journal); + + // and a category and a budget: + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $category->transactionJournals()->save($journal); + $budget->transactionJournals()->save($journal); + + // and a piggy bank event: + $pbEvent = FactoryMuffin::create('FireflyIII\Models\PiggyBankEvent'); + $pbEvent->transaction_journal_id = $journal->id; + $pbEvent->save(); + + $this->be($journal->user); + + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + + // fake! + $repository->shouldReceive('getAccounts')->andReturn(new Collection); + + $this->call('GET', '/transaction/edit/' . $journal->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TransactionController::index + */ + public function testIndexRevenue() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); + + // fake! + $repository->shouldReceive('getJournalsOfTypes')->withArgs([['Deposit'], 0, 0])->andReturn(new LengthAwarePaginator(new Collection, 0, 50)); + + $this->call('GET', '/transactions/deposit'); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\TransactionController::index + */ + public function testIndexTransfer() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); + + // fake! + $repository->shouldReceive('getJournalsOfTypes')->withArgs([['Transfer'], 0, 0])->andReturn(new LengthAwarePaginator(new Collection, 0, 50)); + + $this->call('GET', '/transactions/transfers'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TransactionController::index + */ + public function testIndexWithdrawal() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); + + // fake! + $repository->shouldReceive('getJournalsOfTypes')->withArgs([['Withdrawal'], 0, 0])->andReturn(new LengthAwarePaginator(new Collection, 0, 50)); + + $this->call('GET', '/transactions/withdrawal'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TransactionController::reorder + */ + public function testReorder() + { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $this->be($journal->user); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); + + // fake! + $repository->shouldReceive('getWithDate')->withAnyArgs()->andReturn($journal); + + $data = [ + 'items' => [$journal->id], + 'date' => $journal->date->format('Y-m-d'), + '_token' => 'replaceMe' + ]; + + $this->call('POST', '/transaction/reorder', $data); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\TransactionController::show + */ + public function testShow() + { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $transaction1 = FactoryMuffin::create('FireflyIII\Models\Transaction'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $transaction1->transaction_journal_id = $journal->id; + $transaction1->save(); + $this->be($journal->user); + + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); + + // fake! + $repository->shouldReceive('getAmountBefore')->withAnyArgs()->andReturn(5); + Amount::shouldReceive('getDefaultCurrency')->andReturn($currency); + Amount::shouldReceive('getAllCurrencies')->andReturn([$currency]); + Amount::shouldReceive('getCurrencyCode')->andReturn('X'); + Amount::shouldReceive('getCurrencySymbol')->andReturn('X'); + Amount::shouldReceive('formatTransaction')->andReturn('X'); + Amount::shouldReceive('format')->andReturn('X'); + + + $this->call('GET', '/transaction/show/' . $journal->id); + $this->assertResponseOk(); + } + + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @covers FireflyIII\Http\Controllers\TransactionController::store + */ + public function testStore() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $this->be($account->user); + + $data = [ + 'what' => 'withdrawal', + 'description' => 'Bla bla bla', + 'account_id' => $account->id, + 'expense_account' => 'Bla bla', + 'amount' => '100', + 'amount_currency_id' => $currency->id, + 'date' => '2015-05-05', + 'budget_id' => '0', + 'category' => '', + 'tags' => 'fat-test', + 'piggy_bank_id' => '0', + '_token' => 'replaceMe', + ]; + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); + + // fake! + $repository->shouldReceive('store')->andReturn($journal); + + + $this->call('POST', '/transactions/store/withdrawal', $data); + + //$this->assertSessionHas('errors','bla'); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + + } + + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @covers FireflyIII\Http\Controllers\TransactionController::store + */ + public function testStoreCreateAnother() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $this->be($account->user); + + $data = [ + 'what' => 'withdrawal', + 'description' => 'Bla bla bla', + 'account_id' => $account->id, + 'expense_account' => 'Bla bla', + 'amount' => '100', + 'amount_currency_id' => $currency->id, + 'date' => '2015-05-05', + 'budget_id' => '0', + 'create_another' => '1', + 'category' => '', + 'tags' => 'fat-test', + 'piggy_bank_id' => '0', + '_token' => 'replaceMe', + ]; + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); + + // fake! + $repository->shouldReceive('store')->andReturn($journal); + + + $this->call('POST', '/transactions/store/withdrawal', $data); + + //$this->assertSessionHas('errors','bla'); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + + } + + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @covers FireflyIII\Http\Controllers\TransactionController::store + */ + public function testStoreTransfer() + { + // account types: + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + + $piggy = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $this->be($account->user); + + $account2->user_id = $account->user_id; + $account->account_type_id = $asset->id; + $account2->account_type_id = $asset->id; + $piggy->account_id = $account->id; + $account->save(); + $account2->save(); + $piggy->save(); + + $data = [ + 'what' => 'transfer', + 'description' => 'Bla bla bla', + 'account_from_id' => $account->id, + 'account_to_id' => $account2->id, + 'amount' => '100', + 'amount_currency_id' => $currency->id, + 'date' => '2015-05-05', + 'budget_id' => '0', + 'create_another' => '1', + 'category' => '', + 'tags' => 'fat-test', + 'piggy_bank_id' => $piggy->id, + '_token' => 'replaceMe', + ]; + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); + + // fake! + $repository->shouldReceive('store')->andReturn($journal); + + + $this->call('POST', '/transactions/store/withdrawal', $data); + + //$this->assertSessionHas('errors','bla'); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + + } + + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @covers FireflyIII\Http\Controllers\TransactionController::update + */ + public function testUpdate() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $this->be($journal->user); + $account->user_id = $journal->user_id; + $account->save(); + + $data = [ + '_token' => 'replaceMe', + 'id' => $journal->id, + 'what' => 'withdrawal', + 'description' => 'LunchX', + 'account_id' => $account->id, + 'expense_account' => 'Lunch House', + 'amount' => '4.72', + 'amount_currency_id' => $currency->id, + 'date' => '2015-05-31', + 'budget_id' => '0', + 'category' => 'Lunch', + 'tags' => 'fat-test', + 'piggy_bank_id' => '0', + ]; + + $this->call('POST', '/transactions/store/withdrawal', $data); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); + + // fake! + $repository->shouldReceive('update')->andReturn($journal); + + + $this->call('POST', '/transaction/update/' . $journal->id, $data); + //$this->assertSessionHas('errors','bla'); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + + + } + + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @covers FireflyIII\Http\Controllers\TransactionController::update + */ + public function testUpdateWithRedirect() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $this->be($journal->user); + $account->user_id = $journal->user_id; + $account->save(); + + $data = [ + '_token' => 'replaceMe', + 'id' => $journal->id, + 'what' => 'withdrawal', + 'description' => 'LunchX', + 'account_id' => $account->id, + 'expense_account' => 'Lunch House', + 'amount' => '4.72', + 'amount_currency_id' => $currency->id, + 'date' => '2015-05-31', + 'budget_id' => '0', + 'category' => 'Lunch', + 'return_to_edit' => 1, + 'tags' => 'fat-test', + 'piggy_bank_id' => '0', + ]; + + $this->call('POST', '/transactions/store/withdrawal', $data); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface'); + + // fake! + $repository->shouldReceive('update')->andReturn($journal); + + + $this->call('POST', '/transaction/update/' . $journal->id, $data); + //$this->assertSessionHas('errors','bla'); + $this->assertResponseStatus(302); + $this->assertSessionHas('success'); + + + } + +} diff --git a/tests/controllers/charts/ChartAccountControllerTest.php b/tests/controllers/charts/ChartAccountControllerTest.php new file mode 100644 index 0000000000..8f41214040 --- /dev/null +++ b/tests/controllers/charts/ChartAccountControllerTest.php @@ -0,0 +1,134 @@ +account_type_id = $asset->id; + $two->account_type_id = $asset->id; + $one->save(); + $two->save(); + $accounts = new Collection([$one, $two]); + $date = new Carbon; + $this->be($user); + + // make one shared: + AccountMeta::create( + [ + 'account_id' => $one->id, + 'name' => 'accountRole', + 'data' => 'sharedAsset' + ] + ); + + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + // fake! + $repository->shouldReceive('getAccounts')->once()->andReturn($accounts); + + $this->call('GET', '/chart/account/month/' . $date->format('Y/m')); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\AccountController::all + */ + public function testAllShared() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $accounts = new Collection([$account]); + $date = new Carbon; + $this->be($user); + + // make it shared: + AccountMeta::create( + [ + 'account_id' => $account->id, + 'name' => 'accountRole', + 'data' => 'sharedAsset' + ] + ); + + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + // fake! + $repository->shouldReceive('getAccounts')->once()->andReturn($accounts); + + + $this->call('GET', '/chart/account/month/' . $date->format('Y/m') . '/shared'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\AccountController::frontpage + */ + public function testFrontpage() + { + $accounts = new Collection([FactoryMuffin::create('FireflyIII\Models\Account')]); + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + // fake! + $repository->shouldReceive('getFrontpageAccounts')->andReturn($accounts); + + $this->call('GET', '/chart/account/frontpage'); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\AccountController::single + */ + public function testSingle() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $this->be($account->user); + + $this->call('GET', '/chart/account/' . $account->id); + $this->assertResponseOk(); + + } +} diff --git a/tests/controllers/charts/ChartBillControllerTest.php b/tests/controllers/charts/ChartBillControllerTest.php new file mode 100644 index 0000000000..b7f3ec3f9d --- /dev/null +++ b/tests/controllers/charts/ChartBillControllerTest.php @@ -0,0 +1,100 @@ +be($user); + + + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\BillController::frontpage + */ + public function testFrontpage() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // set! + $bills = new Collection([FactoryMuffin::create('FireflyIII\Models\Bill'), FactoryMuffin::create('FireflyIII\Models\Bill')]); + $journals = new Collection( + [FactoryMuffin::create('FireflyIII\Models\TransactionJournal'), FactoryMuffin::create('FireflyIII\Models\TransactionJournal')] + ); + $creditCards = new Collection([FactoryMuffin::create('FireflyIII\Models\Account'), FactoryMuffin::create('FireflyIII\Models\Account')]); + $ranges = [['start' => new Carbon, 'end' => new Carbon]]; + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + $accounts = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + + // fake! + $repository->shouldReceive('getActiveBills')->andReturn($bills); + $repository->shouldReceive('getRanges')->andReturn($ranges); + $repository->shouldReceive('getJournalsInRange')->andReturn(new Collection, $journals); + $accounts->shouldReceive('getCreditCards')->andReturn($creditCards); + $accounts->shouldReceive('getTransfersInRange')->andReturn(new Collection); + $repository->shouldReceive('createFakeBill')->andReturn($bills->first()); + Steam::shouldReceive('balance')->andReturn(-10, 0); + + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + Preferences::shouldReceive('get')->withArgs(['language', 'en'])->andReturn($language); + + + $this->call('GET', '/chart/bill/frontpage'); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\BillController::single + */ + public function testSingle() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $this->be($bill->user); + + // set + $journals = new Collection([FactoryMuffin::create('FireflyIII\Models\TransactionJournal')]); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + $repository->shouldReceive('getJournals')->andReturn($journals); + + // fake! + + $this->call('GET', '/chart/bill/' . $bill->id); + $this->assertResponseOk(); + + } + + +} diff --git a/tests/controllers/charts/ChartBudgetControllerTest.php b/tests/controllers/charts/ChartBudgetControllerTest.php new file mode 100644 index 0000000000..4670313328 --- /dev/null +++ b/tests/controllers/charts/ChartBudgetControllerTest.php @@ -0,0 +1,164 @@ +be($budget->user); + + $this->call('GET', '/chart/budget/' . $budget->id); + $this->assertResponseOk(); + + + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\BudgetController::budgetLimit + */ + public function testBudgetLimit() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + /** @var \FireflyIII\Models\BudgetLimit $limit */ + $limit = FactoryMuffin::create('FireflyIII\Models\BudgetLimit'); + /** @var \FireflyIII\Models\LimitRepetition $repetition */ + $repetition = FactoryMuffin::create('FireflyIII\Models\LimitRepetition'); + + $start = Carbon::now()->startOfMonth(); + $end = Carbon::now()->endOfMonth(); + + $budget->user_id = $user->id; + $limit->budget_id = $budget->id; + $limit->startdate = $start; + $repetition->budget_limit_id = $limit->id; + $repetition->startdate = $start; + $repetition->enddate = $end; + + $budget->save(); + $limit->save(); + $repetition->save(); + + + $this->be($user); + + $this->call('GET', '/chart/budget/' . $budget->id . '/' . $repetition->id); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\BudgetController::frontpage + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testFrontpage() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $start = Carbon::now()->startOfMonth(); + $end = Carbon::now()->endOfMonth(); + $budgets = new Collection; + $limits = []; + $repetitions = []; + + for ($i = 0; $i < 5; $i++) { + /** @var \FireflyIII\Models\Budget $budget */ + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $budgets->push($budget); + + /** @var \FireflyIII\Models\BudgetLimit $limit */ + $limit = FactoryMuffin::create('FireflyIII\Models\BudgetLimit'); + $limit->budget_id = $budget->id; + $limit->startdate = $start; + $limit->save(); + + $set = new Collection([$limit]); + $limits[] = $set; + + /** @var \FireflyIII\Models\LimitRepetition $repetition */ + $repetition = FactoryMuffin::create('FireflyIII\Models\LimitRepetition'); + $repetition->budget_limit_id = $limit->id; + $repetition->startdate = $start; + $repetition->enddate = $end; + $repetition->save(); + $set = new Collection([$repetition]); + $repetitions[] = $set; + } + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + + // fake! + $repository->shouldReceive('getBudgets')->andReturn($budgets); + $repository->shouldReceive('getBudgetLimitRepetitions')->andReturn($repetitions[0], $repetitions[1], new Collection); + $repository->shouldReceive('spentInPeriodCorrected')->andReturn(10); + $repository->shouldReceive('getWithoutBudgetSum')->andReturn(10); + + $this->call('GET', '/chart/budget/frontpage'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\BudgetController::year + */ + public function testYear() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $collection = new Collection([$budget]); + $this->be($user); + + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + + // fake! + $repository->shouldReceive('getBudgets')->andReturn($collection); + $repository->shouldReceive('spentInPeriodCorrected')->andReturn(0); + + + $this->call('GET', '/chart/budget/year/2015'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\BudgetController::year + */ + public function testYearShared() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $this->call('GET', '/chart/budget/year/2015/shared'); + $this->assertResponseOk(); + } + +} diff --git a/tests/controllers/charts/ChartCategoryControllerTest.php b/tests/controllers/charts/ChartCategoryControllerTest.php new file mode 100644 index 0000000000..fe544b7d1f --- /dev/null +++ b/tests/controllers/charts/ChartCategoryControllerTest.php @@ -0,0 +1,125 @@ +be($category->user); + + $this->call('GET', '/chart/category/' . $category->id . '/all'); + $this->assertResponseOk(); + + + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\CategoryController::frontpage + */ + public function testFrontpage() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // make data: + $set = [ + ['name' => 'Something', 'sum' => 100], + ['name' => 'Something Else', 'sum' => 200], + ['name' => 'Something Else Entirely', 'sum' => 200] + ]; + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + + // fake! + $repository->shouldReceive('getCategoriesAndExpensesCorrected')->andReturn($set); + + //getCategoriesAndExpensesCorrected + + $this->call('GET', '/chart/category/frontpage'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\CategoryController::month + */ + public function testMonth() + { + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->be($category->user); + + $this->call('GET', '/chart/category/' . $category->id . '/month'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\CategoryController::year + */ + public function testYear() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $categories = new Collection([FactoryMuffin::create('FireflyIII\Models\Category')]); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + + // fake! + $repository->shouldReceive('getCategories')->andReturn($categories); + $repository->shouldReceive('spentInPeriodCorrected')->andReturn(0); + + $this->call('GET', '/chart/category/year/2015'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\CategoryController::year + */ + public function testYearShared() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $categories = new Collection([FactoryMuffin::create('FireflyIII\Models\Category')]); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + + // fake! + $repository->shouldReceive('getCategories')->andReturn($categories); + $repository->shouldReceive('spentInPeriodCorrected')->andReturn(0); + + $this->call('GET', '/chart/category/year/2015/shared'); + $this->assertResponseOk(); + } + +} diff --git a/tests/controllers/charts/ChartPiggyBankControllerTest.php b/tests/controllers/charts/ChartPiggyBankControllerTest.php new file mode 100644 index 0000000000..b5b27fc0c5 --- /dev/null +++ b/tests/controllers/charts/ChartPiggyBankControllerTest.php @@ -0,0 +1,53 @@ +be($piggy->account->user); + + // data: + $obj = new stdClass; + $obj->sum = 100; + $obj->date = '2015-01-01'; + $set = [ + $obj + ]; + + // mock! + $repository = $this->mock('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); + + // fake! + $repository->shouldReceive('getEventSummarySet')->andReturn($set); + + $this->call('GET', '/chart/piggyBank/' . $piggy->id); + $this->assertResponseOk(); + } +} diff --git a/tests/controllers/charts/ChartReportControllerTest.php b/tests/controllers/charts/ChartReportControllerTest.php new file mode 100644 index 0000000000..950bcb25a5 --- /dev/null +++ b/tests/controllers/charts/ChartReportControllerTest.php @@ -0,0 +1,77 @@ +be($user); + + $this->call('GET', '/chart/report/in-out/2015'); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\ReportController::yearInOut + */ + public function testYearInOutShared() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $this->call('GET', '/chart/report/in-out/2015/shared'); + $this->assertResponseOk(); + + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\ReportController::yearInOutSummarized + */ + public function testYearInOutSummarized() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $this->call('GET', '/chart/report/in-out-sum/2015'); + $this->assertResponseOk(); + } + + /** + * @covers FireflyIII\Http\Controllers\Chart\ReportController::yearInOutSummarized + */ + public function testYearInOutSummarizedShared() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $this->call('GET', '/chart/report/in-out-sum/2015/shared'); + $this->assertResponseOk(); + } +} diff --git a/tests/factories/all.php b/tests/factories/all.php new file mode 100644 index 0000000000..96a6bb4044 --- /dev/null +++ b/tests/factories/all.php @@ -0,0 +1,321 @@ + 'word', + ] +); + +FactoryMuffin::define( + 'FireflyIII\Models\Bill', + [ + 'user_id' => 'factory|FireflyIII\User', + 'name' => 'sentence', + 'match' => function () { + $words = []; + for ($i = 0; $i < 3; $i++) { + $words[] = RandomString::generateRandomString(5); + } + + return join(',', $words); + }, + 'amount_min' => 10, + 'amount_max' => 20, + 'date' => 'date', + 'active' => 1, + 'automatch' => 1, + 'repeat_freq' => 'monthly', + 'skip' => 0, + 'name_encrypted' => 1, + 'match_encrypted' => 1, + + ] +); + +FactoryMuffin::define( + 'FireflyIII\Models\Account', + [ + 'user_id' => 'factory|FireflyIII\User', + 'account_type_id' => 'factory|FireflyIII\Models\AccountType', + 'name' => 'word', + 'active' => 'boolean', + 'encrypted' => function () { + return true; + }, + 'virtual_balance' => 0 + ] +); + +FactoryMuffin::define( + 'FireflyIII\Models\Tag', + [ + 'description' => 'sentence', + 'user_id' => 'factory|FireflyIII\User', + 'tag' => function () { + return RandomString::generateRandomString(20); + }, + 'tagMode' => 'nothing', + 'date' => 'date', + 'latitude' => 12, + 'longitude' => 13, + 'zoomLevel' => 3, + ] +); + +FactoryMuffin::define( + 'FireflyIII\Models\Budget', + [ + 'user_id' => 'factory|FireflyIII\User', + 'name' => 'sentence', + 'active' => 'boolean', + 'encrypted' => 1, + ] +); + +FactoryMuffin::define( + 'FireflyIII\Models\TransactionGroup', + [ + 'user_id' => 'factory|FireflyIII\User', + 'relation' => 'balance', + ] +); + + +FactoryMuffin::define( + 'FireflyIII\Models\Category', + [ + 'user_id' => 'factory|FireflyIII\User', + 'name' => 'sentence', + 'encrypted' => 1, + ] +); + +FactoryMuffin::define( + 'FireflyIII\Models\LimitRepetition', + [ + 'budget_limit_id' => 'factory|FireflyIII\Models\BudgetLimit', + 'startdate' => 'date', + 'enddate' => 'date', + 'amount' => function () { + return rand(1, 100); + }, + ] +); + +FactoryMuffin::define( + 'FireflyIII\Models\BudgetLimit', + [ + 'budget_id' => 'factory|FireflyIII\Models\Budget', + 'startdate' => 'date', + 'amount' => function () { + return rand(1, 100); + }, + 'repeats' => 'false', + 'repeat_freq' => 'monthly', + + ] +); + + +FactoryMuffin::define( + 'FireflyIII\Models\Preference', + [ + 'name' => 'word', + 'data' => 'sentence', + 'user_id' => 'factory|FireflyIII\User', + ] +); + +FactoryMuffin::define( + 'FireflyIII\Models\AccountType', + [ + 'type' => function () { + $types = ['Expense account', 'Revenue account', 'Asset account', 'Cash account']; + $count = DB::table('account_types')->count(); + if ($count < 4) { + return $types[$count]; + } else { + return RandomString::generateRandomString(10); + } + }, + 'editable' => 1, + ] +); + +FactoryMuffin::define( + 'FireflyIII\Models\TransactionCurrency', + [ + 'code' => function () { + return RandomString::generateRandomString(3); + }, + 'symbol' => function () { + return RandomString::generateRandomString(8); + }, + 'name' => 'word' + ] +); + + +FactoryMuffin::define( + 'FireflyIII\User', + [ + 'email' => function () { + $first = RandomString::generateRandomString(20); + $second = RandomString::generateRandomString(20); + $domain = RandomString::generateRandomString(30); + $email = $first . '.' . $second . '@' . $domain . '.com'; + + return $email; + }, + 'password' => bcrypt('james'), + ] +); + +FactoryMuffin::define( + 'FireflyIII\Models\Transaction', + [ + 'transaction_journal_id' => 'factory|FireflyIII\Models\TransactionJournal', + 'amount' => function () { + return rand(1, 100); + }, + 'description' => 'sentence', + 'account_id' => 'factory|FireflyIII\Models\Account' + ] +); + +FactoryMuffin::define( + 'FireflyIII\Models\PiggyBank', + [ + 'account_id' => 'factory|FireflyIII\Models\Account', + 'name' => 'sentence', + 'targetamount' => function () { + return rand(1, 100); + }, + 'startdate' => 'date', + 'targetdate' => 'date', + 'remind_me' => false, + 'reminder_skip' => 0, + 'order' => 0, + ] +); + +FactoryMuffin::define( + 'FireflyIII\Models\PiggyBankRepetition', + [ + 'piggy_bank_id' => 'factory|FireflyIII\Models\PiggyBank', + 'startdate' => 'date', + 'targetdate' => 'date', + 'currentamount' => function () { + return rand(1, 100); + }, + ] +); + + +FactoryMuffin::define( + 'FireflyIII\Models\PiggyBankEvent', + [ + 'piggy_bank_id' => 'factory|FireflyIII\Models\PiggyBank', + 'transaction_journal_id' => 'factory|FireflyIII\Models\TransactionJournal', + 'date' => 'date', + 'amount' => function () { + return rand(1, 100); + }, + ] +); + + +FactoryMuffin::define( + 'FireflyIII\Models\TransactionType', + [ + 'type' => function () { + $types = ['Withdrawal', 'Deposit', 'Transfer']; + $count = DB::table('transaction_types')->count(); + if ($count < 3) { + return $types[$count]; + } else { + return RandomString::generateRandomString(10); + } + } + ] +); + +FactoryMuffin::define( + 'FireflyIII\Models\TransactionJournal', + [ + 'user_id' => 'factory|FireflyIII\User', + 'transaction_type_id' => 'factory|FireflyIII\Models\TransactionType', + 'transaction_currency_id' => 'factory|FireflyIII\Models\TransactionCurrency', + 'description' => 'sentence', + 'completed' => '1', + 'date' => 'date', + 'encrypted' => '1', + 'order' => '0', + ], function (TransactionJournal $object, $saved) { + if ($saved) { + $one = FactoryMuffin::create('FireflyIII\Models\Account'); + $two = FactoryMuffin::create('FireflyIII\Models\Account'); + + Transaction::create( + [ + 'account_id' => $one->id, + 'transaction_journal_id' => $object->id, + 'amount' => 100 + ] + ); + Transaction::create( + [ + 'account_id' => $two->id, + 'transaction_journal_id' => $object->id, + 'amount' => -100 + ] + ); + + } + +} +); diff --git a/tests/functional.suite.yml b/tests/functional.suite.yml deleted file mode 100644 index 075765bd90..0000000000 --- a/tests/functional.suite.yml +++ /dev/null @@ -1,11 +0,0 @@ -# Codeception Test Suite Configuration -# -# Suite for functional (integration) tests -# Emulate web requests and make application process them -# Include one of framework modules (Symfony2, Yii2, Laravel5) to use it - -class_name: FunctionalTester -modules: - enabled: - # add framework module here - - \Helper\Functional \ No newline at end of file diff --git a/tests/functional/_bootstrap.php b/tests/functional/_bootstrap.php deleted file mode 100644 index 8a88555806..0000000000 --- a/tests/functional/_bootstrap.php +++ /dev/null @@ -1,2 +0,0 @@ -object = new ChartJsAccountChartGenerator; + + parent::setUp(); + + + } + + /** + * This method is called before the first test of this test class is run. + * + * @since Method available since Release 3.4.0 + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + } + + /** + * @covers FireflyIII\Generator\Chart\Account\ChartJsAccountChartGenerator::frontpage + */ + public function testFrontpage() + { + // be somebody + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // create some accounts: + $accounts = new Collection; + for ($i = 0; $i < 5; $i++) { + $accounts->push(FactoryMuffin::create('FireflyIII\Models\Account')); + } + + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->data = 'en'; + $preference->save(); + + // data for call: + $start = Carbon::createFromDate(2015, 1, 1); + $end = Carbon::createFromDate(2015, 1, 15); + + // mock language preference: + Preferences::shouldReceive('get')->withArgs(['language', 'en'])->andReturn($preference); + + // mock Steam::balance + Steam::shouldReceive('balance')->withAnyArgs()->andReturn(0); + + // call + $result = $this->object->frontpage($accounts, $start, $end); + + $this->assertEquals($accounts->count(), $result['count']); + $this->assertCount(15, $result['labels']); + $this->assertCount($accounts->count(), $result['datasets']); + } + + /** + * @covers FireflyIII\Generator\Chart\Account\ChartJsAccountChartGenerator::single + */ + public function testSingle() + { + // be somebody + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->data = 'en'; + $preference->save(); + + // mock language preference: + Preferences::shouldReceive('get')->withArgs(['language', 'en'])->andReturn($preference); + + // mock Steam::balance + Steam::shouldReceive('balance')->withAnyArgs()->andReturn(0); + + // data for call: + $start = Carbon::createFromDate(2015, 1, 1); + $end = Carbon::createFromDate(2015, 1, 15); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + // call + $result = $this->object->single($account, $start, $end); + + + // test + $this->assertCount(15, $result['labels']); + $this->assertEquals($account->name, $result['datasets'][0]['label']); + $this->assertCount(15, $result['datasets'][0]['data']); + + + } +} \ No newline at end of file diff --git a/tests/generators/ChartJsBillChartGeneratorTest.php b/tests/generators/ChartJsBillChartGeneratorTest.php new file mode 100644 index 0000000000..62485e9c84 --- /dev/null +++ b/tests/generators/ChartJsBillChartGeneratorTest.php @@ -0,0 +1,100 @@ +object = new ChartJsBillChartGenerator(); + + + } + + /** + * This method is called before the first test of this test class is run. + * + * @since Method available since Release 3.4.0 + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + } + + /** + * @covers FireflyIII\Generator\Chart\Bill\ChartJsBillChartGenerator::frontpage + */ + public function testFrontpage() + { + // to test frontpage, we generate the exact fake entries + // needed: + $paid = new Collection; + for ($i = 0; $i < 5; $i++) { + $obj = new stdClass(); + $obj->description = 'Something'; + $obj->amount = 100; + $paid->push($obj); + } + + $unpaid = new Collection; + $sum = 0; + for ($i = 0; $i < 5; $i++) { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $date = new Carbon; + $sum += (($bill->amount_max + $bill->amount_min) / 2); + $unpaid->push([$bill, $date]); + } + + + $data = $this->object->frontpage($paid, $unpaid); + + $this->assertCount(2, $data); + $this->assertEquals($sum, $data[0]['value']); + $this->assertEquals(500, $data[1]['value']); + } + + /** + * @covers FireflyIII\Generator\Chart\Bill\ChartJsBillChartGenerator::single + */ + public function testSingle() + { + + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->data = 'en'; + $preference->save(); + + // mock language preference: + Preferences::shouldReceive('get')->withArgs(['language', 'en'])->andReturn($preference); + + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $entries = new Collection; + for ($i = 0; $i < 5; $i++) { + $obj = new stdClass; + $obj->amount = 100; + $obj->date = new Carbon; + $entries->push($obj); + } + $data = $this->object->single($bill, $entries); + + $this->assertCount(5, $data['labels']); + $this->assertCount(5, $data['datasets'][1]['data']); + $this->assertEquals(100, $data['datasets'][1]['data'][0]); // see if first is equal. + + + } +} \ No newline at end of file diff --git a/tests/generators/ChartJsBudgetChartGeneratorTest.php b/tests/generators/ChartJsBudgetChartGeneratorTest.php new file mode 100644 index 0000000000..4fe5e2290e --- /dev/null +++ b/tests/generators/ChartJsBudgetChartGeneratorTest.php @@ -0,0 +1,113 @@ +object = new ChartJsBudgetChartGenerator(); + + } + + /** + * This method is called before the first test of this test class is run. + * + * @since Method available since Release 3.4.0 + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + } + + /** + * @covers FireflyIII\Generator\Chart\Budget\ChartJsBudgetChartGenerator::budget + */ + public function testBudget() + { + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->data = 'en'; + $preference->save(); + + // mock language preference: + Preferences::shouldReceive('get')->withArgs(['language', 'en'])->andReturn($preference); + + // make a collection with some amounts in them. + $collection = new Collection; + for ($i = 0; $i < 5; $i++) { + $collection->push([new Carbon, 100]); + } + + $data = $this->object->budget($collection); + + $this->assertCount(5, $data['labels']); + $this->assertCount(5, $data['datasets'][0]['data']); + $this->assertEquals(100, $data['datasets'][0]['data'][0]); + } + + /** + * @covers FireflyIII\Generator\Chart\Budget\ChartJsBudgetChartGenerator::frontpage + */ + public function testFrontpage() + { + // make a collection with some amounts in them. + $collection = new Collection; + for ($i = 0; $i < 5; $i++) { + $collection->push(['Some label', 100, 200, 300]); + } + + $data = $this->object->frontpage($collection); + + $this->assertCount(5, $data['labels']); + $this->assertCount(5, $data['datasets'][0]['data']); + $this->assertEquals(100, $data['datasets'][0]['data'][0]); + $this->assertEquals(200, $data['datasets'][1]['data'][0]); + $this->assertEquals(300, $data['datasets'][2]['data'][0]); + + } + + /** + * @covers FireflyIII\Generator\Chart\Budget\ChartJsBudgetChartGenerator::year + */ + public function testYear() + { + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->data = 'en'; + $preference->save(); + + $budgets = new Collection; + $entries = new Collection; + + // make some budgets: + for ($i = 0; $i < 5; $i++) { + $budgets->push(FactoryMuffin::create('FireflyIII\Models\Budget')); + $entries->push([new Carbon, 100, 100, 100]); + } + + // mock language preference: + Preferences::shouldReceive('get')->withArgs(['language', 'en'])->andReturn($preference); + + $data = $this->object->year($budgets, $entries); + + $this->assertCount(5, $data['labels']); + $this->assertCount(5, $data['datasets']); + $this->assertCount(3, $data['datasets'][0]['data']); + + } +} \ No newline at end of file diff --git a/tests/generators/ChartJsCategoryChartGeneratorTest.php b/tests/generators/ChartJsCategoryChartGeneratorTest.php new file mode 100644 index 0000000000..3b75f4361a --- /dev/null +++ b/tests/generators/ChartJsCategoryChartGeneratorTest.php @@ -0,0 +1,114 @@ +object = new ChartJsCategoryChartGenerator; + + } + + /** + * This method is called before the first test of this test class is run. + * + * @since Method available since Release 3.4.0 + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + } + + /** + * @covers FireflyIII\Generator\Chart\Category\ChartJsCategoryChartGenerator::all + */ + public function testAll() + { + + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->data = 'en'; + $preference->save(); + + // mock language preference: + Preferences::shouldReceive('get')->withArgs(['language', 'en'])->andReturn($preference); + + + // make a collection of stuff: + $collection = new Collection; + for ($i = 0; $i < 5; $i++) { + $collection->push([new Carbon, 100]); + } + + $data = $this->object->all($collection); + + $this->assertCount(5, $data['labels']); + $this->assertCount(5, $data['datasets'][0]['data']); + $this->assertEquals(100, $data['datasets'][0]['data'][0]); + } + + /** + * @covers FireflyIII\Generator\Chart\Category\ChartJsCategoryChartGenerator::frontpage + */ + public function testFrontpage() + { + // make a collection of stuff: + $collection = new Collection; + for ($i = 0; $i < 5; $i++) { + $collection->push(['name' => 'Something', 'sum' => 100]); + } + + $data = $this->object->frontpage($collection); + + $this->assertCount(5, $data['labels']); + $this->assertCount(5, $data['datasets'][0]['data']); + $this->assertEquals('Something', $data['labels'][0]); + $this->assertEquals(100, $data['datasets'][0]['data'][0]); + } + + /** + * @covers FireflyIII\Generator\Chart\Category\ChartJsCategoryChartGenerator::year + */ + public function testYear() + { + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->data = 'en'; + $preference->save(); + + // mock language preference: + Preferences::shouldReceive('get')->withArgs(['language', 'en'])->andReturn($preference); + + // make a collection of stuff: + $collection = new Collection; + $categories = new Collection; + for ($i = 0; $i < 5; $i++) { + $categories->push(FactoryMuffin::create('FireflyIII\Models\Category')); + $collection->push([new Carbon, 100, 100, 100]); + } + + $data = $this->object->year($categories, $collection); + + $this->assertCount(5, $data['labels']); + $this->assertEquals($categories->first()->name, $data['labels'][0]); + $this->assertCount(3, $data['datasets'][0]['data']); + + + } +} \ No newline at end of file diff --git a/tests/generators/ChartJsPiggyBankChartGeneratorTest.php b/tests/generators/ChartJsPiggyBankChartGeneratorTest.php new file mode 100644 index 0000000000..e63b9d73fd --- /dev/null +++ b/tests/generators/ChartJsPiggyBankChartGeneratorTest.php @@ -0,0 +1,66 @@ +object = new ChartJsPiggyBankChartGenerator; + + } + + /** + * This method is called before the first test of this test class is run. + * + * @since Method available since Release 3.4.0 + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + } + + /** + * @covers FireflyIII\Generator\Chart\PiggyBank\ChartJsPiggyBankChartGenerator::history + */ + public function testHistory() + { + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->data = 'en'; + $preference->save(); + + // mock language preference: + Preferences::shouldReceive('get')->withArgs(['language', 'en'])->andReturn($preference); + + // create a set + $set = new Collection; + for ($i = 0; $i < 5; $i++) { + $obj = new stdClass; + $obj->date = new Carbon; + $obj->sum = 100; + $set->push($obj); + } + + $data = $this->object->history($set); + $this->assertCount(5, $data['labels']); + $this->assertCount(5, $data['datasets'][0]['data']); + $this->assertEquals(100, $data['datasets'][0]['data'][0]); + $this->assertEquals(500, $data['datasets'][0]['data'][4]); + + + } +} \ No newline at end of file diff --git a/tests/generators/ChartJsReportChartGeneratorTest.php b/tests/generators/ChartJsReportChartGeneratorTest.php new file mode 100644 index 0000000000..4c41c23123 --- /dev/null +++ b/tests/generators/ChartJsReportChartGeneratorTest.php @@ -0,0 +1,80 @@ +object = new ChartJsReportChartGenerator; + + } + + /** + * This method is called before the first test of this test class is run. + * + * @since Method available since Release 3.4.0 + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + } + + /** + * FireflyIII\Generator\Chart\Report\ChartJsReportChartGenerator::yearInOut + */ + public function testYearInOut() + { + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->data = 'en'; + $preference->save(); + + // mock language preference: + Preferences::shouldReceive('get')->withArgs(['language', 'en'])->andReturn($preference); + + // make set: + $collection = new Collection; + for ($i = 0; $i < 5; $i++) { + $collection->push([new Carbon, 200, 100]); + } + + $data = $this->object->yearInOut($collection); + + $this->assertEquals(200, $data['datasets'][0]['data'][0]); + $this->assertEquals(100, $data['datasets'][1]['data'][0]); + $this->assertCount(5, $data['labels']); + + } + + /** + * FireflyIII\Generator\Chart\Report\ChartJsReportChartGenerator::yearInOutSummarized + */ + public function testYearInOutSummarized() + { + // make set: + $income = 2400; + $expense = 1200; + + $data = $this->object->yearInOutSummarized($income, $expense, 12); + + $this->assertEquals(200, $data['datasets'][0]['data'][1]); + $this->assertEquals(100, $data['datasets'][1]['data'][1]); + + } +} \ No newline at end of file diff --git a/tests/generators/GoogleAccountChartGeneratorTest.php b/tests/generators/GoogleAccountChartGeneratorTest.php new file mode 100644 index 0000000000..864034180b --- /dev/null +++ b/tests/generators/GoogleAccountChartGeneratorTest.php @@ -0,0 +1,120 @@ +object = new GoogleAccountChartGenerator; + + } + + /** + * This method is called before the first test of this test class is run. + * + * @since Method available since Release 3.4.0 + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + } + + /** + * @covers FireflyIII\Generator\Chart\Account\GoogleAccountChartGenerator::all + */ + public function testAll() + { + // be somebody + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // create some accounts: + $accounts = new Collection; + for ($i = 0; $i < 5; $i++) { + $accounts->push(FactoryMuffin::create('FireflyIII\Models\Account')); + } + + // data for call: + $start = Carbon::createFromDate(2015, 1, 1); + $end = Carbon::createFromDate(2015, 1, 15); + + // mock Steam::balance + Steam::shouldReceive('balance')->withAnyArgs()->andReturn(0); + + $data = $this->object->all($accounts, $start, $end); + $this->assertCount(11, $data['cols']); // accounts * 2 + date. + // fifteen days, + $this->assertCount(16, $data['rows']); // 15 + 1? + } + + /** + * @covers FireflyIII\Generator\Chart\Account\GoogleAccountChartGenerator::frontpage + */ + public function testFrontpage() + { + // be somebody + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // create some accounts: + $accounts = new Collection; + for ($i = 0; $i < 5; $i++) { + $accounts->push(FactoryMuffin::create('FireflyIII\Models\Account')); + } + + // data for call: + $start = Carbon::createFromDate(2015, 1, 1); + $end = Carbon::createFromDate(2015, 1, 15); + + // mock Steam::balance + Steam::shouldReceive('balance')->withAnyArgs()->andReturn(0); + + $data = $this->object->frontpage($accounts, $start, $end); + $this->assertCount(11, $data['cols']); // accounts * 2 + date. + // fifteen days, + $this->assertCount(16, $data['rows']); // 15 + 1? + } + + /** + * @covers FireflyIII\Generator\Chart\Account\GoogleAccountChartGenerator::single + */ + public function testSingle() + { + // be somebody + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + /** @var Account $account */ + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + // data for call: + $start = Carbon::createFromDate(2015, 1, 1); + $end = Carbon::createFromDate(2015, 1, 15); + + // mock Steam::balance + Steam::shouldReceive('balance')->withAnyArgs()->andReturn(0); + + $data = $this->object->single($account, $start, $end); + $this->assertCount(3, $data['cols']); // account, date, certainty + // fifteen days, + $this->assertCount(15, $data['rows']); // 15 days + } + +} \ No newline at end of file diff --git a/tests/generators/GoogleBillChartGeneratorTest.php b/tests/generators/GoogleBillChartGeneratorTest.php new file mode 100644 index 0000000000..5576098595 --- /dev/null +++ b/tests/generators/GoogleBillChartGeneratorTest.php @@ -0,0 +1,90 @@ +object = new GoogleBillChartGenerator; + + } + + /** + * This method is called before the first test of this test class is run. + * + * @since Method available since Release 3.4.0 + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + } + + /** + * @covers FireflyIII\Generator\Chart\Bill\GoogleBillChartGenerator::frontpage + */ + public function testFrontpage() + { + // to test frontpage, we generate the exact fake entries + // needed: + $paid = new Collection; + for ($i = 0; $i < 5; $i++) { + $obj = new stdClass(); + $obj->description = 'Something'; + $obj->amount = 100; + $paid->push($obj); + } + + $unpaid = new Collection; + $sum = 0; + for ($i = 0; $i < 5; $i++) { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $date = new Carbon; + $sum += (($bill->amount_max + $bill->amount_min) / 2); + $unpaid->push([$bill, $date]); + } + + + $data = $this->object->frontpage($paid, $unpaid); + + $this->assertCount(2, $data['cols']); + $this->assertCount(2, $data['rows']); // two rows, two columns. + } + + /** + * @covers FireflyIII\Generator\Chart\Bill\GoogleBillChartGenerator::single + */ + public function testSingle() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $entries = new Collection; + for ($i = 0; $i < 5; $i++) { + $obj = new stdClass; + $obj->amount = 100; + $obj->date = new Carbon; + $entries->push($obj); + } + $data = $this->object->single($bill, $entries); + + $this->assertCount(5, $data['rows']); + $this->assertCount(4, $data['cols']); + $this->assertEquals(100, $data['rows'][0]['c'][3]['v']); + } +} \ No newline at end of file diff --git a/tests/generators/GoogleBudgetChartGeneratorTest.php b/tests/generators/GoogleBudgetChartGeneratorTest.php new file mode 100644 index 0000000000..13b3cb54bc --- /dev/null +++ b/tests/generators/GoogleBudgetChartGeneratorTest.php @@ -0,0 +1,99 @@ +object = new GoogleBudgetChartGenerator(); + + } + + + /** + * This method is called before the first test of this test class is run. + * + * @since Method available since Release 3.4.0 + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + } + + + /** + * @covers FireflyIII\Generator\Chart\Budget\GoogleBudgetChartGenerator::budget + */ + public function testBudget() + { + // make a collection with some amounts in them. + $collection = new Collection; + for ($i = 0; $i < 5; $i++) { + $collection->push([new Carbon, 100]); + } + + $data = $this->object->budget($collection); + + $this->assertCount(5, $data['rows']); + $this->assertCount(2, $data['cols']); + $this->assertEquals(100, $data['rows'][0]['c'][1]['v']); + } + + /** + * @covers FireflyIII\Generator\Chart\Budget\GoogleBudgetChartGenerator::frontpage + */ + public function testFrontpage() + { + // make a collection with some amounts in them. + $collection = new Collection; + for ($i = 0; $i < 5; $i++) { + $collection->push(['Some label', 100, 200, 300]); + } + + $data = $this->object->frontpage($collection); + + $this->assertCount(5, $data['rows']); + $this->assertEquals(100, $data['rows'][0]['c'][1]['v']); + $this->assertEquals(200, $data['rows'][0]['c'][2]['v']); + $this->assertEquals(300, $data['rows'][0]['c'][3]['v']); + } + + /** + * @covers FireflyIII\Generator\Chart\Budget\GoogleBudgetChartGenerator::year + */ + public function testYear() + { + $budgets = new Collection; + $entries = new Collection; + + // make some budgets: + for ($i = 0; $i < 5; $i++) { + $budgets->push(FactoryMuffin::create('FireflyIII\Models\Budget')); + $entries->push([new Carbon, 100, 100, 100]); + } + + $data = $this->object->year($budgets, $entries); + + $this->assertCount(5, $data['rows']); + $this->assertCount(6, $data['cols']); + } +} \ No newline at end of file diff --git a/tests/generators/GoogleCategoryChartGeneratorTest.php b/tests/generators/GoogleCategoryChartGeneratorTest.php new file mode 100644 index 0000000000..78f0ffde39 --- /dev/null +++ b/tests/generators/GoogleCategoryChartGeneratorTest.php @@ -0,0 +1,116 @@ +object = new GoogleCategoryChartGenerator(); + + } + + /** + * This method is called before the first test of this test class is run. + * + * @since Method available since Release 3.4.0 + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + } + + + /** + * @covers FireflyIII\Generator\Chart\Category\GoogleCategoryChartGenerator::all + */ + public function testAll() + { + // make a collection of stuff: + $collection = new Collection; + for ($i = 0; $i < 5; $i++) { + $collection->push([new Carbon, 100]); + } + + $data = $this->object->all($collection); + + $this->assertCount(5, $data['rows']); + $this->assertCount(2, $data['cols']); + $this->assertEquals(100, $data['rows'][0]['c'][1]['v']); + } + + /** + * @covers FireflyIII\Generator\Chart\Category\GoogleCategoryChartGenerator::frontpage + */ + public function testFrontpage() + { + // make a collection of stuff: + $collection = new Collection; + for ($i = 0; $i < 5; $i++) { + $collection->push(['name' => 'Something', 'sum' => 100]); + } + + $data = $this->object->frontpage($collection); + + $this->assertCount(5, $data['rows']); + $this->assertCount(2, $data['cols']); + $this->assertEquals('Something', $data['rows'][0]['c'][0]['v']); + $this->assertEquals(100, $data['rows'][0]['c'][1]['v']); + + } + + /** + * @covers FireflyIII\Generator\Chart\Category\GoogleCategoryChartGenerator::month + */ + public function testMonth() + { + // make a collection of stuff: + $collection = new Collection; + for ($i = 0; $i < 5; $i++) { + $collection->push([new Carbon, 100]); + } + + $data = $this->object->month($collection); + + $this->assertCount(5, $data['rows']); + $this->assertCount(2, $data['cols']); + $this->assertEquals(100, $data['rows'][0]['c'][1]['v']); + } + + /** + * @covers FireflyIII\Generator\Chart\Category\GoogleCategoryChartGenerator::year + */ + public function testYear() + { + // make a collection of stuff: + $collection = new Collection; + $categories = new Collection; + for ($i = 0; $i < 5; $i++) { + $categories->push(FactoryMuffin::create('FireflyIII\Models\Category')); + $collection->push([new Carbon, 100, 100, 100]); + } + + $data = $this->object->year($categories, $collection); + + $this->assertCount(5, $data['rows']); + $this->assertEquals($categories->first()->name, $data['cols'][1]['label']); + $this->assertEquals(100, $data['rows'][0]['c'][1]['v']); + + } +} \ No newline at end of file diff --git a/tests/generators/GooglePiggyBankChartGeneratorTest.php b/tests/generators/GooglePiggyBankChartGeneratorTest.php new file mode 100644 index 0000000000..7c1daf7a9f --- /dev/null +++ b/tests/generators/GooglePiggyBankChartGeneratorTest.php @@ -0,0 +1,58 @@ +object = new GooglePiggyBankChartGenerator(); + + } + + /** + * This method is called before the first test of this test class is run. + * + * @since Method available since Release 3.4.0 + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + } + + /** + * @covers FireflyIII\Generator\Chart\PiggyBank\GooglePiggyBankChartGenerator::history + */ + public function testHistory() + { + // create a set + $set = new Collection; + for ($i = 0; $i < 5; $i++) { + $obj = new stdClass; + $obj->date = new Carbon; + $obj->sum = 100; + $set->push($obj); + } + + $data = $this->object->history($set); + $this->assertCount(5, $data['rows']); + $this->assertCount(2, $data['cols']); + + $this->assertEquals(100, $data['rows'][0]['c'][1]['v']); + $this->assertEquals(500, $data['rows'][4]['c'][1]['v']); + } +} \ No newline at end of file diff --git a/tests/generators/GoogleReportChartGeneratorTest.php b/tests/generators/GoogleReportChartGeneratorTest.php new file mode 100644 index 0000000000..57c715ad02 --- /dev/null +++ b/tests/generators/GoogleReportChartGeneratorTest.php @@ -0,0 +1,69 @@ +object = new GoogleReportChartGenerator; + + } + + /** + * This method is called before the first test of this test class is run. + * + * @since Method available since Release 3.4.0 + */ + public static function setUpBeforeClass() + { + parent::setUpBeforeClass(); + } + + /** + * FireflyIII\Generator\Chart\Report\GoogleReportChartGenerator::yearInOut + */ + public function testYearInOut() + { + // make set: + $collection = new Collection; + for ($i = 0; $i < 5; $i++) { + $collection->push([new Carbon, 200, 100]); + } + + $data = $this->object->yearInOut($collection); + + $this->assertCount(5, $data['rows']); + + $this->assertEquals(200, $data['rows'][0]['c'][1]['v']); + $this->assertEquals(100, $data['rows'][0]['c'][2]['v']); + } + + /** + * FireflyIII\Generator\Chart\Report\GoogleReportChartGenerator::yearInOutSummarized + */ + public function testYearInOutSummarized() + { + // make set: + $income = 2400; + $expense = 1200; + + $data = $this->object->yearInOutSummarized($income, $expense, 12); + + $this->assertEquals(200, $data['rows'][1]['c'][1]['v']); + $this->assertEquals(100, $data['rows'][1]['c'][2]['v']); + } +} \ No newline at end of file diff --git a/tests/helpers/ConnectJournalToPiggyBankTest.php b/tests/helpers/ConnectJournalToPiggyBankTest.php new file mode 100644 index 0000000000..09967aa788 --- /dev/null +++ b/tests/helpers/ConnectJournalToPiggyBankTest.php @@ -0,0 +1,139 @@ +get('piggy_bank_id')))); + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + */ + public function setUp() + { + parent::setUp(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + } + + /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @covers FireflyIII\Handlers\Events\ConnectJournalToPiggyBank::handle + */ + public function testNoRepetition() + { + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + /** @var \FireflyIII\Models\PiggyBank $piggyBank */ + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account1->user_id = $user->id; + $account2->user_id = $user->id; + $piggyBank->account_id = $account1->id; + $account1->save(); + $account2->save(); + $piggyBank->save(); + + // because the event handler responds to this piggy bank, we must remove + // the piggy bank repetition: + /** @var \FireflyIII\Models\PiggyBankRepetition $rep */ + foreach ($piggyBank->piggyBankRepetitions()->get() as $rep) { + $rep->forceDelete(); + } + + + $event = new JournalCreated($journal, $piggyBank->id); + $class = new ConnectJournalToPiggyBank(); + $result = $class->handle($event); + + + $this->assertFalse($result); + } + + /** + * @covers FireflyIII\Handlers\Events\ConnectJournalToPiggyBank::handle + */ + public function testNoSuchPiggy() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $event = new JournalCreated($journal, 1); + $class = new ConnectJournalToPiggyBank(); + $result = $class->handle($event); + + + $this->assertFalse($result); + } + + /** + * @covers FireflyIII\Handlers\Events\ConnectJournalToPiggyBank::handle + */ + public function testWithRepetition() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // deposit + + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + + $journal->user_id = $user->id; + $journal->save(); + + // create piggy bank event to continue handler: + $start = clone $journal->date; + $end = clone $journal->date; + $start->subDay(); + $end->addDay(); + + PiggyBankRepetition::create( + [ + 'piggy_bank_id' => $piggyBank->id, + 'startdate' => $start->format('Y-m-d'), + 'targetdate' => $end->format('Y-m-d'), + 'currentamount' => 0, + ] + ); + + + /** @var Transaction $transaction */ + foreach ($journal->transactions()->get() as $transaction) { + if ($transaction->amount < 0) { + $piggyBank->account_id = $transaction->account_id; + $account = $transaction->account; + $account->user_id = $user->id; + $account->save(); + $piggyBank->account_id = $account->id; + $piggyBank->save(); + } + } + $event = new JournalCreated($journal, $piggyBank->id); + $class = new ConnectJournalToPiggyBank(); + $result = $class->handle($event); + + $this->assertTrue($result); + } +} diff --git a/tests/helpers/ReportHelperTest.php b/tests/helpers/ReportHelperTest.php new file mode 100644 index 0000000000..91a3576d47 --- /dev/null +++ b/tests/helpers/ReportHelperTest.php @@ -0,0 +1,329 @@ +object = new ReportHelper($query); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + } + + /** + * @covers FireflyIII\Helpers\Report\ReportHelper::getAccountReport + */ + public function testGetAccountReport() + { + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); + $cash = FactoryMuffin::create('FireflyIII\Models\AccountType'); + $user = FactoryMuffin::create('FireflyIII\User'); + for ($i = 0; $i < 5; $i++) { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $account->user_id = $user->id; + $account->account_type_id = $asset->id; + $account->save(); + + } + + $cashAccount = FactoryMuffin::create('FireflyIII\Models\Account'); + $cashAccount->user_id = $user->id; + $cashAccount->account_type_id = $cash->id; + $cashAccount->save(); + + $this->be($user); + /** @var AccountCollection $object */ + $object = $this->object->getAccountReport(Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth(), false); + + $this->assertCount(5, $object->getAccounts()); + $this->assertEquals(0, $object->getDifference()); + $this->assertEquals(0, $object->getEnd()); + $this->assertEquals(0, $object->getStart()); + } + + /** + * @covers FireflyIII\Helpers\Report\ReportHelper::getBalanceReport + */ + public function testGetBalanceReport() + { + // factory! + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); + $user = FactoryMuffin::create('FireflyIII\User'); + $rep = FactoryMuffin::create('FireflyIII\Models\LimitRepetition'); + for ($i = 0; $i < 5; $i++) { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $account->user_id = $user->id; + $account->account_type_id = $asset->id; + $account->save(); + } + + $set = new Collection; + for ($i = 0; $i < 5; $i++) { + $set->push(FactoryMuffin::create('FireflyIII\Models\Budget')); + } + + $this->be($user); + + // mock! + $budgetRepos = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + $tagRepos = $this->mock('FireflyIII\Repositories\Tag\TagRepositoryInterface'); + + // fake! + $budgetRepos->shouldReceive('getBudgets')->andReturn($set); + $budgetRepos->shouldReceive('getCurrentRepetition')->andReturn($rep); + $tagRepos->shouldReceive('coveredByBalancingActs')->andReturn(0); + + // test! + $object = $this->object->getBalanceReport(Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth(), false); + $this->assertCount(8, $object->getBalanceLines()); + $this->assertCount(5, $object->getBalanceHeader()->getAccounts()); + } + + /** + * @covers FireflyIII\Helpers\Report\ReportHelper::getBillReport + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testGetBillReport() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // factory! + $set = new Collection; + $journals = new Collection; + FactoryMuffin::create('FireflyIII\Models\Account'); + FactoryMuffin::create('FireflyIII\Models\Account'); + for ($i = 0; $i < 5; $i++) { + $set->push(FactoryMuffin::create('FireflyIII\Models\Bill')); + } + + for ($i = 0; $i < 5; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journals->push($journal); + } + + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface'); + + // fake! + $repository->shouldReceive('getBills')->andReturn($set); + $repository->shouldReceive('getJournalsInRange')->withAnyArgs()->andReturn(new Collection, $journals); + + // test! + $object = $this->object->getBillReport(Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth(), false); + $this->assertCount(5, $object->getBills()); + } + + /** + * @covers FireflyIII\Helpers\Report\ReportHelper::getBudgetReport + */ + public function testGetBudgetReport() + { + // factory! + $user = FactoryMuffin::create('FireflyIII\User'); + $set = new Collection; + $rep1 = new Collection; + $rep2 = new Collection; + for ($i = 0; $i < 5; $i++) { + $set->push(FactoryMuffin::create('FireflyIII\Models\Budget')); + } + for ($i = 0; $i < 5; $i++) { + $rep1->push(FactoryMuffin::create('FireflyIII\Models\LimitRepetition')); + } + + $this->be($user); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + + // fake! + $repository->shouldReceive('getBudgets')->andReturn($set); + $repository->shouldReceive('getBudgetLimitRepetitions')->andReturn($rep1, $rep2); + $repository->shouldReceive('spentInPeriodCorrected')->andReturn(rand(0, 100)); + $repository->shouldReceive('getWithoutBudgetSum')->andReturn(rand(0, 100)); + + // test! + $object = $this->object->getBudgetReport(Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth(), false); + + $this->assertCount(10, $object->getBudgetLines()); + } + + /** + * @covers FireflyIII\Helpers\Report\ReportHelper::getCategoryReport + */ + public function testGetCategoryReport() + { + // factory! + $user = FactoryMuffin::create('FireflyIII\User'); + $set = new Collection; + for ($i = 0; $i < 5; $i++) { + $set->push(FactoryMuffin::create('FireflyIII\Models\Category')); + } + + $this->be($user); + + // mock! + $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface'); + + // fake! + $repository->shouldReceive('getCategories')->andReturn($set); + $repository->shouldReceive('spentInPeriodCorrected')->andReturn(rand(1, 100)); + + // test! + $object = $this->object->getCategoryReport(Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth(), false); + $this->assertCount(5, $object->getCategories()); + } + + + /** + * @covers FireflyIII\Helpers\Report\ReportHelper::getExpenseReport + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testGetExpenseReport() + { + // factory! + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $type = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + // create five journals in this month for the report: + $date = Carbon::now()->startOfMonth()->addDay(); + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); + $left = FactoryMuffin::create('FireflyIII\Models\Account'); + $right = FactoryMuffin::create('FireflyIII\Models\Account'); + $left->account_type_id = $asset->id; + $right->account_type_id = $asset->id; + $right->save(); + $left->save(); + + // save meta for account: + AccountMeta::create( + [ + 'account_id' => $left->id, + 'name' => 'accountRole', + 'data' => 'defaultAsset' + ] + ); + AccountMeta::create( + [ + 'account_id' => $right->id, + 'name' => 'accountRole', + 'data' => 'defaultAsset' + ] + ); + + + for ($i = 0; $i < 5; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->date = $date; + $journal->transaction_type_id = $type->id; + $journal->user_id = $user->id; + $journal->save(); + } + + // test! + $object = $this->object->getExpenseReport(Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth(), true); + $this->assertCount(5, $object->getExpenses()); + } + + /** + * @covers FireflyIII\Helpers\Report\ReportHelper::getIncomeReport + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testGetIncomeReport() + { + // factory! + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $type = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + // create five journals in this month for the report: + $date = Carbon::now()->startOfMonth()->addDay(); + $left = FactoryMuffin::create('FireflyIII\Models\Account'); + $right = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); + $left->account_type_id = $asset->id; + $right->account_type_id = $asset->id; + + // save meta for account: + AccountMeta::create( + [ + 'account_id' => $left->id, + 'name' => 'accountRole', + 'data' => 'defaultAsset' + ] + ); + AccountMeta::create( + [ + 'account_id' => $right->id, + 'name' => 'accountRole', + 'data' => 'defaultAsset' + ] + ); + + $right->save(); + $left->save(); + for ($i = 0; $i < 5; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->date = $date; + $journal->transaction_type_id = $type->id; + $journal->user_id = $user->id; + $journal->save(); + } + + // test! + $object = $this->object->getIncomeReport(Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth(), true); + $this->assertCount(5, $object->getIncomes()); + + } + + /** + * @covers FireflyIII\Helpers\Report\ReportHelper::listOfMonths + */ + public function testListOfMonths() + { + // start of year up until now + $date = new Carbon('2015-01-01'); + $now = new Carbon; + $diff = $now->diffInMonths($date) + 1; // the month itself. + $result = $this->object->listOfMonths($date); + + $this->assertCount($diff, $result[2015]); + + } + +} diff --git a/tests/helpers/ReportQueryTest.php b/tests/helpers/ReportQueryTest.php new file mode 100644 index 0000000000..a7451c5319 --- /dev/null +++ b/tests/helpers/ReportQueryTest.php @@ -0,0 +1,397 @@ +object = new ReportQuery; + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + } + + + /** + * @covers FireflyIII\Helpers\Report\ReportQuery::expenseInPeriodCorrected + * @covers FireflyIII\Helpers\Report\ReportQuery::queryJournalsWithTransactions + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testExpenseInPeriodCorrected() + { + $start = new Carbon('2015-01-01'); + $end = new Carbon('2015-02-01'); + + $user = FactoryMuffin::create('FireflyIII\User'); + $type = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + $expense = FactoryMuffin::create('FireflyIII\Models\AccountType'); + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); + + $date = new Carbon('2015-01-12'); + + for ($i = 0; $i < 10; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->date = $date; + $journal->user_id = $user->id; + $journal->transaction_type_id = $type->id; + $journal->save(); + + // two transactions: + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account1->account_type_id = $asset->id; + $account1->user_id = $user->id; + $account2->account_type_id = $expense->id; + $account2->user_id = $user->id; + $account1->save(); + $account2->save(); + + AccountMeta::create( + [ + 'account_id' => $account1->id, + 'name' => 'accountRole', + 'data' => 'defaultAsset' + ] + ); + + $amount = 100; + if ($i == 8) { + $amount = 0; // at least one "empty" journal. + } + + // update both transactions + $journal->transactions[0]->account_id = $account1->id; + $journal->transactions[0]->amount = $amount * -1; + $journal->transactions[0]->save(); + + $journal->transactions[1]->account_id = $account2->id; + $journal->transactions[1]->amount = $amount; + $journal->transactions[1]->save(); + + + } + $this->be($user); + + + $set = $this->object->expenseInPeriodCorrected($start, $end, false); + + + $this->assertCount(9, $set); + } + + /** + * @covers FireflyIII\Helpers\Report\ReportQuery::expenseInPeriodCorrected + * @covers FireflyIII\Helpers\Report\ReportQuery::queryJournalsWithTransactions + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testExpenseInPeriodCorrectedShared() + { + $start = new Carbon('2015-01-01'); + $end = new Carbon('2015-02-01'); + + $user = FactoryMuffin::create('FireflyIII\User'); + $type = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + $expense = FactoryMuffin::create('FireflyIII\Models\AccountType'); + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); + + $date = new Carbon('2015-01-12'); + + for ($i = 0; $i < 10; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->date = $date; + $journal->user_id = $user->id; + $journal->transaction_type_id = $type->id; + $journal->save(); + + // two transactions: + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account1->account_type_id = $asset->id; + $account1->user_id = $user->id; + $account2->account_type_id = $expense->id; + $account2->user_id = $user->id; + $account1->save(); + $account2->save(); + + AccountMeta::create( + [ + 'account_id' => $account1->id, + 'name' => 'accountRole', + 'data' => 'defaultAsset' + ] + ); + + $amount = 100; + if ($i == 8) { + $amount = 0; // at least one "empty" journal. + } + + // update both transactions + $journal->transactions[0]->account_id = $account1->id; + $journal->transactions[0]->amount = $amount * -1; + $journal->transactions[0]->save(); + + $journal->transactions[1]->account_id = $account2->id; + $journal->transactions[1]->amount = $amount; + $journal->transactions[1]->save(); + + } + $this->be($user); + + $set = $this->object->expenseInPeriodCorrected($start, $end, true); + + $this->assertCount(9, $set); + } + + /** + * @covers FireflyIII\Helpers\Report\ReportQuery::getAllAccounts + */ + public function testGetAllAccounts() + { + $start = new Carbon('2015-01-01'); + $end = new Carbon('2015-02-01'); + $user = FactoryMuffin::create('FireflyIII\User'); + FactoryMuffin::create('FireflyIII\Models\Account'); + FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + for ($i = 0; $i < 10; $i++) { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $account->account_type_id = $asset->id; + $account->user_id = $user->id; + $account->save(); + } + + Steam::shouldReceive('balance')->andReturn(0); + + $this->be($user); + + $set = $this->object->getAllAccounts($start, $end, false); + $this->assertCount(10, $set); + } + + /** + * @covers FireflyIII\Helpers\Report\ReportQuery::getAllAccounts + */ + public function testGetAllAccountsShared() + { + $start = new Carbon('2015-01-01'); + $end = new Carbon('2015-02-01'); + $user = FactoryMuffin::create('FireflyIII\User'); + FactoryMuffin::create('FireflyIII\Models\Account'); + FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + for ($i = 0; $i < 10; $i++) { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $account->account_type_id = $asset->id; + $account->user_id = $user->id; + $account->save(); + } + + Steam::shouldReceive('balance')->andReturn(0); + + $this->be($user); + + $set = $this->object->getAllAccounts($start, $end, true); + $this->assertCount(10, $set); + } + + /** + * @covers FireflyIII\Helpers\Report\ReportQuery::incomeInPeriodCorrected + * @covers FireflyIII\Helpers\Report\ReportQuery::queryJournalsWithTransactions + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testIncomeInPeriodCorrected() + { + $start = new Carbon('2015-01-01'); + $end = new Carbon('2015-02-01'); + + $user = FactoryMuffin::create('FireflyIII\User'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $type = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + $expense = FactoryMuffin::create('FireflyIII\Models\AccountType'); + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); + + $date = new Carbon('2015-01-12'); + + for ($i = 0; $i < 10; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->date = $date; + $journal->user_id = $user->id; + $journal->transaction_type_id = $type->id; + $journal->save(); + + // two transactions: + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account1->account_type_id = $asset->id; + $account1->user_id = $user->id; + $account2->account_type_id = $expense->id; + $account2->user_id = $user->id; + $account1->save(); + $account2->save(); + + AccountMeta::create( + [ + 'account_id' => $account1->id, + 'name' => 'accountRole', + 'data' => 'defaultAsset' + ] + ); + + $amount = 100; + if ($i == 8) { + $amount = 0; // at least one "empty" journal. + } + + // update both transactions + $journal->transactions[0]->account_id = $account1->id; + $journal->transactions[0]->amount = $amount; + $journal->transactions[0]->save(); + + $journal->transactions[1]->account_id = $account2->id; + $journal->transactions[1]->amount = $amount * -1; + $journal->transactions[1]->save(); + + } + $this->be($user); + + $set = $this->object->incomeInPeriodCorrected($start, $end, false); + + $this->assertCount(9, $set); + } + + /** + * @covers FireflyIII\Helpers\Report\ReportQuery::incomeInPeriodCorrected + * @covers FireflyIII\Helpers\Report\ReportQuery::queryJournalsWithTransactions + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testIncomeInPeriodCorrectedShared() + { + $start = new Carbon('2015-01-01'); + $end = new Carbon('2015-02-01'); + + $user = FactoryMuffin::create('FireflyIII\User'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $type = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + $expense = FactoryMuffin::create('FireflyIII\Models\AccountType'); + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); + + $date = new Carbon('2015-01-12'); + + for ($i = 0; $i < 10; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->date = $date; + $journal->user_id = $user->id; + $journal->transaction_type_id = $type->id; + $journal->save(); + + // two transactions: + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account1->account_type_id = $asset->id; + $account1->user_id = $user->id; + $account2->account_type_id = $expense->id; + $account2->user_id = $user->id; + $account1->save(); + $account2->save(); + + AccountMeta::create( + [ + 'account_id' => $account1->id, + 'name' => 'accountRole', + 'data' => 'defaultAsset' + ] + ); + + $amount = 100; + if ($i == 8) { + $amount = 0; // at least one "empty" journal. + } + + // update both transactions + $journal->transactions[0]->account_id = $account1->id; + $journal->transactions[0]->amount = $amount * -1; + $journal->transactions[0]->save(); + + $journal->transactions[1]->account_id = $account2->id; + $journal->transactions[1]->amount = $amount; + $journal->transactions[1]->save(); + + } + $this->be($user); + + $set = $this->object->incomeInPeriodCorrected($start, $end, true); + + $this->assertCount(9, $set); + } + + /** + * @covers FireflyIII\Helpers\Report\ReportQuery::spentInBudgetCorrected + */ + public function testSpentInBudgetCorrected() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $account->user_id = $user->id; + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $budget->user_id = $user->id; + + $account->save(); + $budget->save(); + + $this->be($user); + + $result = $this->object->spentInBudgetCorrected($account, $budget, new Carbon, new Carbon); + $this->assertEquals(0, $result); + + } + + /** + * @covers FireflyIII\Helpers\Report\ReportQuery::spentNoBudget + */ + public function testSpentNoBudget() + { + + $user = FactoryMuffin::create('FireflyIII\User'); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $account->user_id = $user->id; + + $account->save(); + + $this->be($user); + + $result = $this->object->spentNoBudget($account, new Carbon, new Carbon); + $this->assertEquals(0, $result); + } + +} diff --git a/tests/models/AccountModelTest.php b/tests/models/AccountModelTest.php new file mode 100644 index 0000000000..662a2a73a2 --- /dev/null +++ b/tests/models/AccountModelTest.php @@ -0,0 +1,162 @@ + $account->name, + 'account_type_id' => $account->account_type_id, + 'user_id' => $account->user_id + ]; + + $result = Account::firstOrCreateEncrypted($search); + + // should be the same account: + + $this->assertEquals($account->id, $result->id); + + } + + /** + * @covers FireflyIII\Models\Account::firstOrCreateEncrypted + */ + public function testFirstOrCreateEncryptedNew() + { + // create account: + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + FactoryMuffin::create('FireflyIII\User'); + + // search for account with the same properties: + $search = [ + 'name' => 'Some new account', + 'account_type_id' => $account->account_type_id, + 'user_id' => $account->user_id, + 'active' => 1, + ]; + + $result = Account::firstOrCreateEncrypted($search); + + // should not be the same account: + + $this->assertNotEquals($account->id, $result->id); + + + } + + /** + * @covers FireflyIII\Models\Account::firstOrNullEncrypted + */ + public function testFirstOrNullEncrypted() + { + // create account: + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + + // search for account with the same properties: + $search = [ + 'name' => $account->name, + 'account_type_id' => $account->account_type_id, + 'user_id' => $account->user_id + ]; + + $result = Account::firstOrNullEncrypted($search); + + // should be the same account: + + $this->assertEquals($account->id, $result->id); + } + + /** + * @covers FireflyIII\Models\Account::firstOrNullEncrypted + */ + public function testFirstOrNullEncryptedNew() + { + // create account: + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + FactoryMuffin::create('FireflyIII\User'); + + // search for account with the same properties: + $search = [ + 'name' => 'Some new account', + 'account_type_id' => $account->account_type_id, + 'user_id' => $account->user_id, + 'active' => 1, + ]; + + $result = Account::firstOrNullEncrypted($search); + + // should not be the same account: + + $this->assertNull($result); + + + } + + /** + * @covers FireflyIII\Models\Account::getNameForEditformAttribute + */ + public function testGetNameForEditformAttribute() + { + // normal name is normal + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $this->assertEquals($account->name, $account->getNameForEditformAttribute()); + } + /** + * @covers FireflyIII\Models\Account::getNameForEditformAttribute + */ + public function testGetNameForEditformAttributeCash() + { + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + // cash name is empty + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $this->assertEquals('', $account->getNameForEditformAttribute()); + } + +} diff --git a/tests/models/CategoryModelTest.php b/tests/models/CategoryModelTest.php new file mode 100644 index 0000000000..c4abe4cfb5 --- /dev/null +++ b/tests/models/CategoryModelTest.php @@ -0,0 +1,76 @@ + $category->name, + 'user_id' => $category->user_id + ]; + + $result = Category::firstOrCreateEncrypted($search); + + $this->assertEquals($result->id, $category->id); + } + + /** + * @covers FireflyIII\Models\Category::firstOrCreateEncrypted + */ + public function testFirstOrCreateEncryptedNew() + { + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + + $search = [ + 'name' => 'Some category name', + 'user_id' => $category->user_id + ]; + + $result = Category::firstOrCreateEncrypted($search); + + $this->assertNotEquals($result->id, $category->id); + } + +} diff --git a/tests/models/TagModelTest.php b/tests/models/TagModelTest.php new file mode 100644 index 0000000000..b4411d1eb1 --- /dev/null +++ b/tests/models/TagModelTest.php @@ -0,0 +1,95 @@ + 'something', + 'tag' => $tag->tag, + 'user_id' => $tag->user_id, + ]; + + $result = Tag::firstOrCreateEncrypted($search); + + $this->assertEquals($tag->id, $result->id); + } + + /** + * @covers FireflyIII\Models\Tag::firstOrCreateEncrypted + */ + public function testFirstOrCreateEncryptedNew() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + + $search = [ + 'tagMode' => 'something', + 'tag' => 'Something else', + 'user_id' => $tag->user_id, + ]; + + $result = Tag::firstOrCreateEncrypted($search); + + $this->assertNotEquals($tag->id, $result->id); + } + + /** + * @covers FireflyIII\Models\Tag::save + */ + public function testSave() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + // connect some transaction journals to the tag: + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->tags()->save($tag); + $tag->save(); + $journal = TransactionJournal::find($journal->id); + + $this->assertEquals(1, $journal->tag_count); + + + } + +} diff --git a/tests/models/TransactionJournalModelTest.php b/tests/models/TransactionJournalModelTest.php new file mode 100644 index 0000000000..17d8646557 --- /dev/null +++ b/tests/models/TransactionJournalModelTest.php @@ -0,0 +1,636 @@ +transactions[0]->amount = '123.45'; + $journal->transactions[0]->save(); + $journal->transactions[1]->amount = '-123.45'; + $journal->transactions[1]->save(); + + $amount = $journal->actual_amount; + $this->assertEquals('123.45', $amount); + } + + /** + * Journal has one tag. + * + * @covers FireflyIII\Models\TransactionJournal::getAmountAttribute + * @covers FireflyIII\Models\TransactionJournal::amountByTag + * @covers FireflyIII\Models\TransactionJournal::save + * @covers FireflyIII\Models\TransactionJournal::amountByTags + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testGetAmountAttributeAdvancePayment() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + // make types: + $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $depositType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + // make tag + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'advancePayment'; + $tag->save(); + + // make withdrawal + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $withdrawal->transaction_type_id = $withdrawalType->id; + $withdrawal->save(); + + // make deposit + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $deposit->transaction_type_id = $depositType->id; + $deposit->save(); + + // make accounts + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + // transactions are already in place, update them: + $withdrawal->transactions[0]->account_id = $asset->id; + $withdrawal->transactions[0]->amount = -300; + $withdrawal->transactions[0]->save(); + + $withdrawal->transactions[1]->account_id = $expense->id; + $withdrawal->transactions[1]->amount = 300; + $withdrawal->transactions[1]->save(); + + $deposit->transactions[0]->account_id = $revenue->id; + $deposit->transactions[0]->amount = -89.88; + $deposit->transactions[0]->save(); + + $deposit->transactions[1]->account_id = $asset->id; + $deposit->transactions[1]->amount = 89.88; + $deposit->transactions[1]->save(); + + // connect to tag: + $tag->transactionJournals()->save($withdrawal); + $tag->transactionJournals()->save($deposit); + + $withdrawal->save(); + $deposit->save(); + $withdrawal = TransactionJournal::find($withdrawal->id); + $deposit = TransactionJournal::find($deposit->id); + + // amount should be 210.12: + $this->assertEquals('210.12', $withdrawal->amount); + $this->assertEquals('0', $deposit->amount); + + + } + + + /** + * Journal has one tag. + * + * @covers FireflyIII\Models\TransactionJournal::getAmountAttribute + * @covers FireflyIII\Models\TransactionJournal::amountByTag + * @covers FireflyIII\Models\TransactionJournal::amountByTags + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testGetAmountAttributeBalancingAct() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // make types: + $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $transferType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + // make a tag + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'balancingAct'; + $tag->save(); + + // make a withdrawal and a transfer + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $withdrawal->transaction_type_id = $withdrawalType->id; + $withdrawal->save(); + + $transfer = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $transfer->transaction_type_id = $transferType->id; + $transfer->save(); + + // connect to tag: + $tag->transactionJournals()->save($withdrawal); + $tag->transactionJournals()->save($transfer); + + // make accounts: + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue->account_type_id = $asset->account_type_id; + $revenue->save(); + + // transactions are already in place, update them: + $withdrawal->transactions[0]->account_id = $asset->id; + $withdrawal->transactions[0]->amount = -123.45; + $withdrawal->transactions[0]->save(); + + $withdrawal->transactions[1]->account_id = $expense->id; + $withdrawal->transactions[1]->amount = 123.45; + $withdrawal->transactions[1]->save(); + + $transfer->transactions[0]->account_id = $revenue->id; + $transfer->transactions[0]->amount = -123.45; + $transfer->transactions[0]->save(); + + $transfer->transactions[1]->account_id = $asset->id; + $transfer->transactions[1]->amount = 123.45; + $transfer->transactions[1]->save(); + + $withdrawal->save(); + + $withdrawal = TransactionJournal::find($withdrawal->id); + + $amount = $withdrawal->amount; + + + $this->assertEquals('0', $amount); + } + + /** + * Journal has no tags. + * + * @covers FireflyIII\Models\TransactionJournal::getAmountAttribute + * @covers FireflyIII\Models\TransactionJournal::amountByTag + * @covers FireflyIII\Models\TransactionJournal::amountByTags + */ + public function testGetAmountAttributeNoTags() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + + $journal->transactions[0]->amount = 123.45; + $journal->transactions[0]->save(); + + $journal->transactions[1]->amount = -123.45; + $journal->transactions[1]->save(); + + $amount = $journal->amount; + $this->assertEquals('123.45', $amount); + } + + /** + * + * Journal has one tag. + * + * @covers FireflyIII\Models\TransactionJournal::getAmountAttribute + * @covers FireflyIII\Models\TransactionJournal::amountByTag + * @covers FireflyIII\Models\TransactionJournal::amountByTags + */ + public function testGetAmountAttributeTag() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // has a normal tag, but nothing special. + // make tag + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'nothing'; + $tag->save(); + + // make withdrawal + $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $withdrawal->transaction_type_id = $withdrawalType->id; + $withdrawal->save(); + + // make accounts + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + $withdrawal->transactions[0]->amount = -300; + $withdrawal->transactions[0]->account_id = $asset->id; + $withdrawal->transactions[0]->save(); + + $withdrawal->transactions[1]->amount = 300; + $withdrawal->transactions[1]->account_id = $expense->id; + $withdrawal->transactions[1]->save(); + + // connect to tag: + $tag->transactionJournals()->save($withdrawal); + + $withdrawal->save(); + $withdrawal = TransactionJournal::find($withdrawal->id); + + $this->assertEquals('300', $withdrawal->amount); + + + } + + /** + * Journal has multiple tags, withdrawal. All default tag. + * + * @covers FireflyIII\Models\TransactionJournal::getAmountAttribute + * @covers FireflyIII\Models\TransactionJournal::amountByTag + * @covers FireflyIII\Models\TransactionJournal::amountByTags + */ + public function testGetAmountAttributeTags() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // has two normal tags: + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'nothing'; + $tag->save(); + $tag2 = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag2->tagMode = 'nothing'; + $tag2->save(); + + // make withdrawal + $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $withdrawal->transaction_type_id = $withdrawalType->id; + $withdrawal->save(); + + // make accounts + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + $withdrawal->transactions[0]->amount = -300; + $withdrawal->transactions[0]->account_id = $asset->id; + $withdrawal->transactions[0]->save(); + + $withdrawal->transactions[1]->amount = 300; + $withdrawal->transactions[1]->account_id = $expense->id; + $withdrawal->transactions[1]->save(); + + // connect to tag: + $tag->transactionJournals()->save($withdrawal); + $tag2->transactionJournals()->save($withdrawal); + + // grab withdrawal again to update tag count: + $withdrawal->save(); + $withdrawal = TransactionJournal::find($withdrawal->id); + + $this->assertEquals('300', $withdrawal->amount); + + + } + + /** + * Multiple tags, transfer, and one is a balancing act + * + * @covers FireflyIII\Models\TransactionJournal::getAmountAttribute + * @covers FireflyIII\Models\TransactionJournal::amountByTag + * @covers FireflyIII\Models\TransactionJournal::amountByTags + */ + public function testGetAmountAttributeTagsTransfer() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // has two normal tags: + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'balancingAct'; + $tag->save(); + $tag2 = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag2->tagMode = 'nothing'; + $tag2->save(); + + // make withdrawal + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $transferType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $transfer = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $transfer->transaction_type_id = $transferType->id; + $transfer->save(); + + // make accounts + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + $transfer->transactions[0]->amount = -300; + $transfer->transactions[0]->account_id = $asset->id; + $transfer->transactions[0]->save(); + + $transfer->transactions[1]->amount = 300; + $transfer->transactions[1]->account_id = $expense->id; + $transfer->transactions[1]->save(); + + // connect to tag: + $tag->transactionJournals()->save($transfer); + $tag2->transactionJournals()->save($transfer); + + $transfer->save(); + $transfer = TransactionJournal::find($transfer->id); + + $this->assertEquals('300', $transfer->amount); + + + } + + /** + * Multiple tags, transfer, and one is a advance payment. + * + * @covers FireflyIII\Models\TransactionJournal::getAmountAttribute + * @covers FireflyIII\Models\TransactionJournal::amountByTag + * @covers FireflyIII\Models\TransactionJournal::amountByTags + */ + public function testGetAmountAttributeTagsTransferAdvance() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // has two normal tags: + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'advancePayment'; + $tag->save(); + $tag2 = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag2->tagMode = 'nothing'; + $tag2->save(); + + // make withdrawal + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $transferType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $transfer = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $transfer->transaction_type_id = $transferType->id; + $transfer->save(); + + // make accounts + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + $transfer->transactions[0]->amount = -300; + $transfer->transactions[0]->account_id = $asset->id; + $transfer->transactions[0]->save(); + + $transfer->transactions[1]->amount = 300; + $transfer->transactions[1]->account_id = $expense->id; + $transfer->transactions[1]->save(); + + // connect to tag: + $tag->transactionJournals()->save($transfer); + $tag2->transactionJournals()->save($transfer); + + $transfer->save(); + $transfer = TransactionJournal::find($transfer->id); + + $this->assertEquals('300', $transfer->amount); + + + } + + /** + * Multiple tags, withdrawal, and one is a balancingAct. + * + * @covers FireflyIII\Models\TransactionJournal::getAmountAttribute + * @covers FireflyIII\Models\TransactionJournal::amountByTag + * @covers FireflyIII\Models\TransactionJournal::amountByTags + */ + public function testGetAmountAttributeTagsWithdrawalAdvance() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // has two normal tags: + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'balancingAct'; + $tag->save(); + $tag2 = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag2->tagMode = 'nothing'; + $tag2->save(); + + // make withdrawal + $withdrawalType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $withdrawal->transaction_type_id = $withdrawalType->id; + $withdrawal->save(); + + // make accounts + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + $withdrawal->transactions[0]->amount = -300; + $withdrawal->transactions[0]->account_id = $asset->id; + $withdrawal->transactions[0]->save(); + + $withdrawal->transactions[1]->amount = 300; + $withdrawal->transactions[1]->account_id = $expense->id; + $withdrawal->transactions[1]->save(); + + // connect to tag: + $tag->transactionJournals()->save($withdrawal); + $tag2->transactionJournals()->save($withdrawal); + + $withdrawal->save(); + $withdrawal = TransactionJournal::find($withdrawal->id); + + $this->assertEquals('300', $withdrawal->amount); + + + } + + + /** + * @covers FireflyIII\Models\TransactionJournal::getCorrectAmountAttribute + */ + public function testGetCorrectAmountAttribute() + { + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + + // make accounts + FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + // make withdrawal + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->transaction_type_id = $withdrawal->id; + $journal->save(); + + $journal->transactions[0]->account_id = $asset->id; + $journal->transactions[0]->amount = 300; + $journal->transactions[0]->save(); + + $journal->transactions[1]->account_id = $revenue->id; + $journal->transactions[1]->amount = -300; + $journal->transactions[1]->save(); + + // get asset account: + $result = $journal->correct_amount; + + $this->assertEquals(-300, $result); + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getCorrectAmountAttribute + */ + public function testGetCorrectAmountAttributeDeposit() + { + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionType'); // deposit + + // make accounts + FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + // make withdrawal + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->transaction_type_id = $deposit->id; + $journal->save(); + + $journal->transactions[0]->account_id = $asset->id; + $journal->transactions[0]->amount = 300; + $journal->transactions[0]->save(); + + $journal->transactions[1]->account_id = $revenue->id; + $journal->transactions[1]->amount = -300; + $journal->transactions[1]->save(); + + // get asset account: + $result = $journal->correct_amount; + + $this->assertEquals(300, $result); + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getCorrectAmountAttribute + */ + public function testGetCorrectAmountAttributeTransfer() + { + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // deposit + $transfer = FactoryMuffin::create('FireflyIII\Models\TransactionType'); // transfer + + // make accounts + FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + // make withdrawal + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->transaction_type_id = $transfer->id; + $journal->save(); + + $journal->transactions[0]->account_id = $asset->id; + $journal->transactions[0]->amount = 300; + $journal->transactions[0]->save(); + + $journal->transactions[1]->account_id = $revenue->id; + $journal->transactions[1]->amount = -300; + $journal->transactions[1]->save(); + + // get asset account: + $result = $journal->correct_amount; + + $this->assertEquals('300', $result); + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getDestinationAccountAttribute + */ + public function testGetDestinationAccountAttribute() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $depositType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $deposit->transaction_type_id = $depositType->id; + $deposit->save(); + + // make accounts + FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + $deposit->transactions[0]->account_id = $asset->id; + $deposit->transactions[0]->amount = 300; + $deposit->transactions[0]->save(); + + $deposit->transactions[1]->account_id = $revenue->id; + $deposit->transactions[1]->amount = -300; + $deposit->transactions[1]->save(); + + // get asset account: + $result = $deposit->destination_account; + + $this->assertEquals($asset->id, $result->id); + } + + /** + * @covers FireflyIII\Models\TransactionJournal::getSourceAccountAttribute + */ + public function testGetSourceAccountAttribute() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $depositType = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $deposit->transaction_type_id = $depositType->id; + $deposit->save(); + + // make accounts + FactoryMuffin::create('FireflyIII\Models\Account'); + $revenue = FactoryMuffin::create('FireflyIII\Models\Account'); + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); + + $deposit->transactions[0]->account_id = $asset->id; + $deposit->transactions[0]->amount = 300; + $deposit->transactions[0]->save(); + + $deposit->transactions[1]->account_id = $revenue->id; + $deposit->transactions[1]->amount = -300; + $deposit->transactions[1]->save(); + + // get asset account: + $result = $deposit->source_account; + + $this->assertEquals($revenue->id, $result->id); + } + +} diff --git a/tests/repositories/AccountRepositoryTest.php b/tests/repositories/AccountRepositoryTest.php new file mode 100644 index 0000000000..6a17b5d564 --- /dev/null +++ b/tests/repositories/AccountRepositoryTest.php @@ -0,0 +1,891 @@ +object = new AccountRepository; + parent::setUp(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::countAccounts + */ + public function testCountAccounts() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $type = $account->accountType->type; + $this->be($account->user); + + $this->assertEquals(1, $this->object->countAccounts([$type])); + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::destroy + * @covers FireflyIII\Providers\EventServiceProvider::boot + * @covers FireflyIII\Providers\EventServiceProvider::registerDeleteEvents + */ + public function testDestroy() + { + // create account: + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + // create some transactions and attach them to the account: + for ($i = 0; $i < 5; $i++) { + $transaction = FactoryMuffin::create('FireflyIII\Models\Transaction'); + $transaction->account_id = $account->id; + $transaction->save(); + } + + $accountId = $account->id; + $this->be($account->user); + + + $this->object->destroy($account); + + // cannot find account: + $this->assertCount(0, Account::whereId($accountId)->whereNotNull('deleted_at')->get()); + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::getAccounts + */ + public function testGetAccounts() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $type = $account->accountType->type; + $this->be($account->user); + + $this->assertCount(1, $this->object->getAccounts([$type])); + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::getCreditCards + */ + public function testGetCreditCards() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + // create account meta object: + $meta = new AccountMeta; + $meta->name = 'accountRole'; + $meta->data = 'ccAsset'; + $meta->account_id = $account->id; + $meta->save(); + + // meta account type + $meta = new AccountMeta; + $meta->name = 'ccType'; + $meta->data = 'monthlyFull'; + $meta->account_id = $account->id; + $meta->save(); + + // login + $this->be($account->user); + + // test! + $this->assertCount(1, $this->object->getCreditCards()); + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::getFirstTransaction + */ + public function testGetFirstTransaction() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $first = $journal->transactions()->orderBy('date', 'DESC')->first(); + $first->account_id = $account->id; + $first->save(); + + + // login + $this->be($account->user); + + $oldest = $this->object->getFirstTransaction($journal, $account); + + $this->assertEquals($first->amount, $oldest->amount); + $this->assertEquals($first->id, $oldest->id); + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::getFrontpageAccounts + */ + public function testGetFrontpageAccounts() + { + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + + + // making two account types is kind of cheating but it works. + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + /** @var Preference $preference */ + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->data = []; + $preference->save(); + $this->be($account->user); + + $set = $this->object->getFrontpageAccounts($preference); + + $this->assertEquals($account->id, $set->first()->id); + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::getFrontpageAccounts + */ + public function testGetFrontpageAccountsWithPreference() + { + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + + + // making two account types is kind of cheating but it works. + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + /** @var Preference $preference */ + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->data = [$account->id]; + $preference->save(); + $this->be($account->user); + + $set = $this->object->getFrontpageAccounts($preference); + + $this->assertEquals($account->id, $set->first()->id); + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::getFrontpageTransactions + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testGetFrontpageTransactions() + { + // three journals + /** @var Account $account */ + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $journal1 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal2 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal3 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + + // three dates (one is out of bounds) + $journal1->date = new Carbon('2012-01-02'); + $journal1->user_id = $account->user_id; + $journal2->date = new Carbon('2012-01-09'); + $journal2->user_id = $account->user_id; + $journal3->date = new Carbon('2012-02-02'); + $journal3->user_id = $account->user_id; + + // save all + $journal1->save(); + $journal2->save(); + $journal3->save(); + + $journal1->transactions[0]->account_id = $account->id; + $journal1->transactions[0]->save(); + $journal2->transactions[0]->account_id = $account->id; + $journal2->transactions[0]->save(); + $journal3->transactions[0]->account_id = $account->id; + $journal3->transactions[0]->save(); + + // be user + $this->be($journal1->user); + + // get set: + + $set = $this->object->getFrontpageTransactions($account, new Carbon('2012-01-01'), new Carbon('2012-01-31')); + + // should have two left. + $this->assertCount(2, $set); + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::getJournals + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testGetJournals() + { + $date = new Carbon; + // three journals + /** @var Account $account */ + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $journal1 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal2 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal3 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + + // three dates (one is out of bounds) + $journal1->date = $date; + $journal1->user_id = $account->user_id; + $journal2->date = $date; + $journal2->user_id = $account->user_id; + $journal3->date = $date; + $journal3->user_id = $account->user_id; + + // save all + $journal1->save(); + $journal2->save(); + $journal3->save(); + + $journal1->transactions[0]->account_id = $account->id; + $journal1->transactions[0]->save(); + $journal2->transactions[0]->account_id = $account->id; + $journal2->transactions[0]->save(); + $journal3->transactions[0]->account_id = $account->id; + $journal3->transactions[0]->save(); + + // be user + $this->be($journal1->user); + + // get paginator: + /** @var LengthAwarePaginator $paginator */ + $paginator = $this->object->getJournals($account, 1); + + // should have three entries: + $this->assertEquals(3, $paginator->count()); + $this->assertEquals(1, $paginator->currentPage()); + $this->assertFalse($paginator->isEmpty()); + + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::getLastActivity + */ + public function testGetLastActivity() + { + $date = new Carbon('2012-02-02'); + // one journal + /** @var Account $account */ + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->date = $date; + $journal->user_id = $account->user_id; + $journal->save(); + + $journal->transactions[0]->account_id = $account->id; + $journal->transactions[0]->save(); + + // be user + $this->be($journal->user); + + $latest = $this->object->getLastActivity($account); + $this->assertEquals($date->format('Y-m-d'), $latest->format('Y-m-d')); + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::getLastActivity + */ + public function testGetLastActivityNoActivity() + { + /** @var Account $account */ + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $this->be($account->user); + + $latest = $this->object->getLastActivity($account); + $this->assertnull($latest); + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::getPiggyBankAccounts + */ + public function testGetPiggyBankAccounts() + { + $date = Carbon::now()->startOfMonth()->addDays(3); + /* + * Quite the collection of objects for this one. + */ + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $piggyBankRepetition = $piggyBank->piggybankRepetitions()->first(); + /* + * Update id's to match each other: + */ + $piggyBankRepetition->currentamount = rand(1, 100); + $piggyBankRepetition->startdate = $date; + $piggyBankRepetition->targetdate = $date; + $piggyBank->account_id = $account->id; + $piggyBankRepetition->save(); + $piggyBank->save(); + + /* + * Put dates in session: + */ + $this->session(['start' => Carbon::now()->startOfMonth(), 'end' => Carbon::now()->endOfMonth()]); + + /* + * Run method: + */ + $this->be($account->user); + $collection = $this->object->getPiggyBankAccounts(); + + $this->assertCount(1, $collection); + $this->assertEquals($collection->first()->id, $account->id); + $this->assertEquals($collection->first()->piggyBalance, $piggyBankRepetition->currentamount); + $this->assertEquals(0, $collection->first()->startBalance); + $this->assertEquals(0, $collection->first()->endBalance); + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::getSavingsAccounts + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testGetSavingsAccounts() + { + // create three accounts: + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + $type = FactoryMuffin::create('FireflyIII\Models\AccountType'); + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account1->account_type_id = $type->id; + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2->account_type_id = $type->id; + $account3 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account3->account_type_id = $type->id; + + // make them savings accounts: + $meta = new AccountMeta; + $meta->name = 'accountRole'; + $meta->data = 'savingAsset'; + $meta->account_id = $account1->id; + $meta->save(); + + $meta = new AccountMeta; + $meta->name = 'accountRole'; + $meta->data = 'savingAsset'; + $meta->account_id = $account2->id; + $meta->save(); + + $meta = new AccountMeta; + $meta->name = 'accountRole'; + $meta->data = 'savingAsset'; + $meta->account_id = $account3->id; + $meta->save(); + + // assign to the same user: + $account2->user_id = $account1->user_id; + $account3->user_id = $account1->user_id; + $account1->save(); + $account2->save(); + $account3->save(); + $this->be($account1->user); + + // mock steam balance: + Steam::shouldReceive('balance')->andReturn(0, 0, 1, 2, 4, 3); + + // get the result from the method: + $result = $this->object->getSavingsAccounts(); + + $this->assertEquals(0, $result->get(0)->difference); + $this->assertEquals(1, $result->get(1)->difference); + $this->assertEquals(-1, $result->get(2)->difference); + + $this->assertEquals(100, $result->get(0)->percentage); + $this->assertEquals(100, $result->get(1)->percentage); + $this->assertEquals(25, $result->get(2)->percentage); + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::getTransfersInRange + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testGetTransfersInRange() + { + FactoryMuffin::create('FireflyIII\Models\Account'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // deposit + FactoryMuffin::create('FireflyIII\Models\AccountType'); // expense + FactoryMuffin::create('FireflyIII\Models\AccountType'); // revenue + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); // asset + $transfer = FactoryMuffin::create('FireflyIII\Models\TransactionType'); // transfer + $user = FactoryMuffin::create('FireflyIII\User'); // user! + $accounts = []; + $opposings = []; // opposing accounts. + $journals = []; + // dates + $start = new Carbon('2014-01-01'); + $end = new Carbon('2014-01-31'); + $inRange = new Carbon('2014-01-15'); + $before = new Carbon('2013-01-15'); + + // create two accounts: + for ($i = 0; $i < 2; $i++) { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $account->account_type_id = $asset->id; + $account->user_id = $user->id; + $account->save(); + $accounts[] = $account; + + $opposing = FactoryMuffin::create('FireflyIII\Models\Account'); + $opposing->account_type_id = $asset->id; + $opposing->user_id = $user->id; + $opposing->save(); + $opposings[] = $opposing; + } + + // for each account, create ten journals + foreach ($accounts as $index => $account) { + for ($i = 0; $i < 10; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->user_id = $user->id; + $journal->transaction_type_id = $transfer->id; + $journal->save(); + + // if $i < 6, transfer is in range: + if ($i < 6) { + $journal->date = $inRange; + } else { + $journal->date = $before; + } + + /* + * Transfers can go either way (see the amount) + */ + if ($i < 4) { + $amount = 100; + } else { + $amount = -100; + } + + + $journal->transactions[0]->account_id = $account->id; + $journal->transactions[0]->amount = $amount; + $journal->transactions[1]->account_id = $opposings[$index]->id; + $journal->transactions[1]->amount = $amount * -1; + $journal->transactions[0]->save(); + $journal->transactions[1]->save(); + // save journal: + $journal->save(); + $journals[] = $journal; + } + } + $this->be($user); + + $set = $this->object->getTransfersInRange($accounts[0], $start, $end); + + $this->assertEquals(4, $set->count()); + $this->assertEquals(100, $set->first()->amount); + $this->assertEquals($journals[0]->description, $set->first()->description); + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::leftOnAccount + */ + public function testLeftOnAccount() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $piggyBankRepetition = $piggyBank->piggybankRepetitions()->first(); + $piggyBankRepetition->currentamount = rand(1, 100); + $piggyBankRepetition->save(); + $this->be($piggyBank->account->user); + + + $result = $this->object->leftOnAccount($piggyBank->account, new Carbon); + + $this->assertEquals($piggyBankRepetition->currentamount * -1, $result); + + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::openingBalanceTransaction + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testOpeningBalanceTransaction() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $journal1 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal2 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + + // dates + $one = new Carbon('2013-01-15'); + $two = new Carbon('2015-01-15'); + + // journal 1 will match: + $journal1->date = $one; + $journal1->user_id = $account->user_id; + $journal2->date = $two; + $journal2->user_id = $account->user_id; + + // add account things: + $journal1->transactions[0]->account_id = $account->id; + $journal1->transactions[1]->account_id = $account->id; + $journal2->transactions[0]->account_id = $account->id; + $journal2->transactions[1]->account_id = $account->id; + $journal1->transactions[0]->save(); + $journal1->transactions[1]->save(); + $journal2->transactions[0]->save(); + $journal2->transactions[1]->save(); + + + $journal1->save(); + $journal2->save(); + + + $this->be($account->user); + + $result = $this->object->openingBalanceTransaction($account); + $this->assertEquals($journal1->id, $result->id); + $this->assertEquals($journal1->description, $result->description); + $this->assertEquals(100, $result->amount); + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::openingBalanceTransaction + */ + public function testOpeningBalanceTransactionNull() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $this->be($account->user); + + $result = $this->object->openingBalanceTransaction($account); + $this->assertNull($result); + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::store + * @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount + * @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata + * @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance + * + */ + public function testStore() + { + $user = FactoryMuffin::create('FireflyIII\User'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($user); + + $data = [ + 'accountType' => 'expense', + 'user' => $user->id, + 'name' => 'Test account #' . rand(1, 100), + 'active' => true, + 'accountRole' => 'testAccount', + 'openingBalance' => 100, + 'virtualBalance' => 0, + 'openingBalanceCurrency' => $currency->id, + 'openingBalanceDate' => '2015-01-01', + ]; + + + $account = $this->object->store($data); + + $this->assertEquals($data['name'], $account->name); + + } + + /** + * This function should give a big fat error, but it doesnt. + * + * @covers FireflyIII\Repositories\Account\AccountRepository::store + * @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount + * @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata + * @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance + */ + public function testStoreWithExistingAccount() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); // expense + FactoryMuffin::create('FireflyIII\Models\AccountType'); // revenue + FactoryMuffin::create('FireflyIII\Models\AccountType'); // asset + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // deposit + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // transfer + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($account->user); + + + $data = [ + 'accountType' => 'expense', + 'user' => $account->user->id, + 'name' => $account->name, + 'active' => $account->active, + 'accountRole' => 'testAccount', + 'openingBalance' => 0, + 'virtualBalance' => 0, + 'openingBalanceCurrency' => $currency->id, + 'openingBalanceDate' => '2015-01-01', + ]; + + $newAccount = $this->object->store($data); + + $this->assertEquals($account->name, $newAccount->name); + $this->assertEquals($account->id, $newAccount->id); + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::store + * @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount + * @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata + * @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance + * @expectedException Symfony\Component\HttpKernel\Exception\HttpException + */ + public function testStoreWithInvalidAccountData() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($account->user); + + + $data = [ + 'accountType' => 'expense', + 'user' => $account->user->id + 12, + 'name' => $account->name, + 'active' => $account->active, + 'accountRole' => 'testAccount', + 'openingBalance' => 0, + 'virtualBalance' => 0, + 'openingBalanceCurrency' => $currency->id, + 'openingBalanceDate' => '2015-01-01', + ]; + + $this->object->store($data); + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::store + * @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount + * @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata + * @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance + */ + public function testStoreWithNegativeInitialBalance() + { + $user = FactoryMuffin::create('FireflyIII\User'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($user); + + $data = [ + 'accountType' => 'expense', + 'user' => $user->id, + 'name' => 'Test account #' . rand(1, 100), + 'active' => true, + 'accountRole' => 'testAccount', + 'openingBalance' => -100, + 'virtualBalance' => 0, + 'openingBalanceCurrency' => $currency->id, + 'openingBalanceDate' => '2015-01-01', + ]; + + + $account = $this->object->store($data); + + $this->assertEquals($data['name'], $account->name); + $this->assertEquals(-100, $account->transactions()->first()->amount); + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::sumOfEverything + */ + public function testSumOfEverything() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $this->assertEquals(0, $this->object->sumOfEverything()); + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::update + * @covers FireflyIII\Repositories\Account\AccountRepository::updateMetadata + * @covers FireflyIII\Repositories\Account\AccountRepository::updateInitialBalance + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testUpdate() + { + $user = FactoryMuffin::create('FireflyIII\User'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($user); + + $data = [ + 'accountType' => 'expense', + 'user' => $user->id, + 'name' => 'Test account #' . rand(1, 100), + 'active' => true, + 'accountRole' => 'testAccount', + 'openingBalance' => 100, + 'virtualBalance' => 0, + 'openingBalanceCurrency' => $currency->id, + 'openingBalanceDate' => '2015-01-01', + ]; + + + $account = $this->object->store($data); + + // now update that same account: + $data = [ + 'name' => 'New account name' . rand(0, 100), + 'active' => 1, + 'virtualBalance' => 0, + 'openingBalance' => 50, + 'openingBalanceCurrency' => $currency->id, + 'openingBalanceDate' => '2015-02-02', + ]; + + $newAccount = $this->object->update($account, $data); + + $this->assertEquals($data['name'], $newAccount->name); + $this->assertEquals(50, $account->transactions()->first()->amount); + + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::update + * @covers FireflyIII\Repositories\Account\AccountRepository::updateMetadata + * @covers FireflyIII\Repositories\Account\AccountRepository::updateInitialBalance + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testUpdateDeleteOpeningBalance() + { + $user = FactoryMuffin::create('FireflyIII\User'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($user); + + $data = [ + 'accountType' => 'expense', + 'user' => $user->id, + 'name' => 'Test account #' . rand(1, 100), + 'active' => true, + 'accountRole' => 'testAccount', + 'openingBalance' => 100, + 'virtualBalance' => 0, + 'openingBalanceCurrency' => $currency->id, + 'openingBalanceDate' => '2015-01-01', + ]; + + + $account = $this->object->store($data); + + // now update that same account: + $data = [ + 'name' => 'New account name' . rand(0, 100), + 'active' => 1, + 'user' => $user->id, + 'accountRole' => 'testAccount', + 'virtualBalance' => 0, + 'openingBalance' => 0, + ]; + + $newAccount = $this->object->update($account, $data); + + $this->assertEquals($data['name'], $newAccount->name); + $this->assertEquals(0, $newAccount->transactions()->whereNull('deleted_at')->count()); + + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::update + * @covers FireflyIII\Repositories\Account\AccountRepository::updateMetadata + * @covers FireflyIII\Repositories\Account\AccountRepository::updateInitialBalance + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testUpdateNewOpeningBalance() + { + $user = FactoryMuffin::create('FireflyIII\User'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($user); + + $data = [ + 'accountType' => 'expense', + 'user' => $user->id, + 'name' => 'Test account #' . rand(1, 100), + 'active' => true, + 'accountRole' => 'testAccount', + 'openingBalance' => 0, + 'virtualBalance' => 0, + ]; + + + $account = $this->object->store($data); + + // now update that same account: + $data = [ + 'name' => 'New account name' . rand(0, 100), + 'active' => 1, + 'user' => $user->id, + 'virtualBalance' => 0, + 'accountRole' => 'testAccount', + 'ccMonthlyPaymentDate' => '2015-01-01', + 'openingBalance' => 51, + 'openingBalanceCurrency' => $currency->id, + 'openingBalanceDate' => '2015-02-02', + ]; + + $newAccount = $this->object->update($account, $data); + + $this->assertEquals($data['name'], $newAccount->name); + $this->assertEquals(51, $account->transactions()->first()->amount); + + + } +} diff --git a/tests/repositories/BillRepositoryTest.php b/tests/repositories/BillRepositoryTest.php new file mode 100644 index 0000000000..36356391f5 --- /dev/null +++ b/tests/repositories/BillRepositoryTest.php @@ -0,0 +1,467 @@ +object = new BillRepository; + parent::setUp(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + } + + public function testBillPaymentsInRange() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $start = Carbon::now()->startOfMonth(); + $end = Carbon::now()->endOfMonth(); + + // payment: + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->date = $start; + $journal->bill_id = $bill->id; + $journal->save(); + + + $this->object->billPaymentsInRange($bill, $start, $end); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::createFakeBill + */ + public function testCreateFakeBill() + { + $description = 'Fake bill ' . rand(10, 100); + $date = new Carbon('2013-01-01'); + $amount = 1200; + $bill = $this->object->createFakeBill($description, $date, $amount); + + $this->assertEquals($amount, $bill->amount_max); + $this->assertEquals($amount, $bill->amount_min); + $this->assertNull($bill->id); + $this->assertEquals($description, $bill->name); + + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::destroy + */ + public function testDestroy() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $accountId = $bill->id; + $this->object->destroy($bill); + + // cannot find bill: + $this->assertCount(0, Bill::whereId($accountId)->whereNotNull('deleted_at')->get()); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::getActiveBills + */ + public function testGetActiveBills() + { + $bill1 = FactoryMuffin::create('FireflyIII\Models\Bill'); + $bill2 = FactoryMuffin::create('FireflyIII\Models\Bill'); + + // update bills + $bill1->active = 1; + $bill2->active = 0; + $bill2->user_id = $bill1->user_id; + $bill1->save(); + $bill2->save(); + $this->be($bill1->user); + + $set = $this->object->getActiveBills(); + + $this->assertCount(1, $set); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::getBills + */ + public function testGetBills() + { + $bill1 = FactoryMuffin::create('FireflyIII\Models\Bill'); + $bill2 = FactoryMuffin::create('FireflyIII\Models\Bill'); + + // update bills + $bill1->active = 1; + $bill2->active = 0; + $bill2->user_id = $bill1->user_id; + $bill1->save(); + $bill2->save(); + $this->be($bill1->user); + + $set = $this->object->getBills(); + + $this->assertCount(2, $set); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::getJournals + */ + public function testGetJournals() + { + $bill1 = FactoryMuffin::create('FireflyIII\Models\Bill'); + + // update bills + $bill1->active = 1; + $bill1->save(); + $this->be($bill1->user); + + $set = $this->object->getJournals($bill1); + + $this->assertCount(0, $set); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::getJournalsInRange + */ + public function testGetJournalsInRange() + { + $bill1 = FactoryMuffin::create('FireflyIII\Models\Bill'); + + // update bills + $bill1->active = 1; + $bill1->save(); + $this->be($bill1->user); + + $set = $this->object->getJournalsInRange($bill1, new Carbon, new Carbon); + + $this->assertCount(0, $set); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::getPossiblyRelatedJournals + */ + public function testGetPossiblyRelatedJournals() + { + $bill1 = FactoryMuffin::create('FireflyIII\Models\Bill'); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $bill1->amount_min = 100; + $bill1->amount_max = 1000; + $account->user_id = $bill1->user_id; + $bill1->save(); + $account->save(); + + // create some transactions to match our bill: + for ($i = 0; $i < 8; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->user_id = $bill1->user_id; + $journal->save(); + $journal->transactions[0]->account_id = $account->id; + $journal->transactions[0]->save(); + } + $this->be($bill1->user); + + $set = $this->object->getPossiblyRelatedJournals($bill1); + $this->assertCount(8, $set); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::getRanges + */ + public function testGetRanges() + { + $bill1 = FactoryMuffin::create('FireflyIII\Models\Bill'); + $bill1->date = new Carbon('2012-01-01'); + $bill1->repeat_freq = 'monthly'; + $bill1->save(); + + $set = $this->object->getRanges($bill1, new Carbon('2012-01-01'), new Carbon('2012-12-31')); + + $this->assertCount(12, $set); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::lastFoundMatch + */ + public function testLastFoundMatch() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->bill_id = $bill->id; + $journal->user_id = $bill->user_id; + $journal->save(); + + $this->be($bill->user); + + $date = $this->object->lastFoundMatch($bill); + + $this->assertEquals($journal->date->format('Y-m-d'), $date->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::lastFoundMatch + */ + public function testLastFoundMatchNull() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + + $this->be($bill->user); + + $date = $this->object->lastFoundMatch($bill); + + $this->assertNull($date); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::nextExpectedMatch + */ + public function testNextExpectedMatch() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $bill->date = new Carbon('2012-01-07'); + $bill->repeat_freq = 'monthly'; + $bill->save(); + $this->be($bill->user); + + // journal: + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->date = Carbon::now()->format('Y-m-d'); + $journal->user_id = $bill->user_id; + $journal->bill_id = $bill->id; + $journal->save(); + + $next = $this->object->nextExpectedMatch($bill); + $today = Carbon::now()->endOfMonth()->addDay(); + $this->assertEquals($today->format('Y-m-d'), $next->format('Y-m-d')); + + + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::nextExpectedMatch + */ + public function testNextExpectedMatchInactive() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $bill->active = 0; + $bill->save(); + $this->be($bill->user); + + $this->assertNull($this->object->nextExpectedMatch($bill)); + + + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::nextExpectedMatch + */ + public function testNextExpectedMatchNoJournals() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $bill->date = new Carbon('2012-01-07'); + $bill->repeat_freq = 'monthly'; + $bill->save(); + + $this->be($bill->user); + + $next = $this->object->nextExpectedMatch($bill); + $today = Carbon::now()->startOfMonth(); + $this->assertEquals($today->format('Y-m-d'), $next->format('Y-m-d')); + + + } + + + /** + * One + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * + * @covers FireflyIII\Repositories\Bill\BillRepository::scan + * @covers FireflyIII\Repositories\Bill\BillRepository::doWordMatch + * @covers FireflyIII\Repositories\Bill\BillRepository::doAmountMatch + */ + public function testScanMatch() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $bill->date = new Carbon('2012-01-07'); + $bill->repeat_freq = 'monthly'; + $bill->match = 'jemoeder'; + $bill->amount_min = 90; + $bill->amount_max = 110; + $bill->save(); + $this->be($bill->user); + + // journal: + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->date = Carbon::now()->format('Y-m-d'); + $journal->description = 'jemoeder'; + $journal->user_id = $bill->user_id; + $journal->save(); + + $this->object->scan($bill, $journal); + $newJournal = TransactionJournal::find($journal->id); + + $this->assertEquals($bill->id, $newJournal->bill_id); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::scan + * @covers FireflyIII\Repositories\Bill\BillRepository::doWordMatch + * @covers FireflyIII\Repositories\Bill\BillRepository::doAmountMatch + */ + public function testScanNoMatch() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $bill->date = new Carbon('2012-01-07'); + $bill->repeat_freq = 'monthly'; + $bill->save(); + $this->be($bill->user); + + // journal: + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->date = Carbon::now()->format('Y-m-d'); + $journal->user_id = $bill->user_id; + $journal->save(); + + // two transactions: + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + Transaction::create( + [ + 'account_id' => $account1->id, + 'transaction_journal_id' => $journal->id, + 'amount' => 100, + ] + ); + Transaction::create( + [ + 'account_id' => $account2->id, + 'transaction_journal_id' => $journal->id, + 'amount' => 100, + ] + ); + + $this->object->scan($bill, $journal); + $newJournal = TransactionJournal::find($journal->id); + + $this->assertNull($newJournal->bill_id); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::scan + * @covers FireflyIII\Repositories\Bill\BillRepository::doWordMatch + * @covers FireflyIII\Repositories\Bill\BillRepository::doAmountMatch + */ + public function testScanNoMatchButAttached() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $bill->date = new Carbon('2012-01-07'); + $bill->match = 'blablabla'; + $bill->repeat_freq = 'monthly'; + $bill->save(); + $this->be($bill->user); + + // journal: + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->date = Carbon::now()->format('Y-m-d'); + $journal->user_id = $bill->user_id; + $journal->bill_id = $bill->id; + $journal->save(); + + // two transactions: + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + Transaction::create( + [ + 'account_id' => $account1->id, + 'transaction_journal_id' => $journal->id, + 'amount' => 100, + ] + ); + Transaction::create( + [ + 'account_id' => $account2->id, + 'transaction_journal_id' => $journal->id, + 'amount' => 100, + ] + ); + + $this->object->scan($bill, $journal); + $newJournal = TransactionJournal::find($journal->id); + + $this->assertNull($newJournal->bill_id); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::store + */ + public function testStore() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $data = [ + 'name' => 'Something', + 'match' => 'Something', + 'amount_min' => 100, + 'user' => $user->id, + 'amount_max' => 110, + 'date' => new Carbon, + 'repeat_freq' => 'monthly', + 'skip' => 0, + 'automatch' => 1, + 'active' => 1, + + ]; + $bill = $this->object->store($data); + + $this->assertEquals($data['name'], $bill->name); + } + + /** + * @covers FireflyIII\Repositories\Bill\BillRepository::update + */ + public function testUpdate() + { + $bill = FactoryMuffin::create('FireflyIII\Models\Bill'); + $data = [ + 'name' => 'new Name', + 'match' => $bill->match, + 'amount_min' => 100, + 'amount_max' => 110, + 'date' => new Carbon, + 'repeat_freq' => 'monthly', + 'skip' => 0, + 'automatch' => 1, + 'active' => 1, + + ]; + $newBill = $this->object->update($bill, $data); + + $this->assertEquals($data['name'], $newBill->name); + $this->assertEquals($bill->match, $newBill->match); + } +} diff --git a/tests/repositories/BudgetRepositoryTest.php b/tests/repositories/BudgetRepositoryTest.php new file mode 100644 index 0000000000..f172274f1e --- /dev/null +++ b/tests/repositories/BudgetRepositoryTest.php @@ -0,0 +1,399 @@ +object = new BudgetRepository; + parent::setUp(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::cleanupBudgets + * @covers FireflyIII\Providers\EventServiceProvider::boot + */ + public function testCleanupBudgets() + { + // create some budgets: + for ($i = 0; $i < 3; $i++) { + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $limit = FactoryMuffin::create('FireflyIII\Models\BudgetLimit'); + $limit->budget_id = $budget->id; + $limit->amount = 0; + $limit->save(); + } + + + $this->object->cleanupBudgets(); + + $this->assertCount(0, BudgetLimit::get()); + + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::destroy + */ + public function testDestroy() + { + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + + $this->object->destroy($budget); + + $this->assertCount(0, Budget::where('id', $budget->id)->whereNull('deleted_at')->get()); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::expensesOnDayCorrected + */ + public function testExpensesOnDayCorrected() + { + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + + $result = $this->object->expensesOnDayCorrected($budget, new Carbon); + + $this->assertEquals(0, $result); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getActiveBudgets + */ + public function testGetActiveBudgets() + { + $budget1 = FactoryMuffin::create('FireflyIII\Models\Budget'); + $budget2 = FactoryMuffin::create('FireflyIII\Models\Budget'); + $budget1->active = 1; + $budget2->active = 0; + $budget2->user_id = $budget1->user_id; + $budget1->save(); + $budget2->save(); + $this->be($budget1->user); + + $set = $this->object->getActiveBudgets(); + + $this->assertCount(1, $set); + $this->assertEquals($set->first()->id, $budget1->id); + + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getBudgetLimitRepetitions + */ + public function testGetBudgetLimitRepetitions() + { + $rep = FactoryMuffin::create('FireflyIII\Models\LimitRepetition'); + $limit = $rep->budgetlimit; + $limit->startdate = new Carbon('2015-02-02'); + $rep->startdate = new Carbon('2015-02-02'); + $rep->enddate = new Carbon('2015-02-28'); + $limit->save(); + $rep->save(); + + $set = $this->object->getBudgetLimitRepetitions($rep->budgetlimit->budget, new Carbon('2015-02-01'), new Carbon('2015-02-28')); + $this->assertCount(2, $set); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getBudgetLimits + */ + public function testGetBudgetLimits() + { + /** @var Budget $budget */ + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $set = $this->object->getBudgetLimits($budget); + + $this->assertCount(0, $set); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getBudgets + */ + public function testGetBudgets() + { + $budget1 = FactoryMuffin::create('FireflyIII\Models\Budget'); + $budget2 = FactoryMuffin::create('FireflyIII\Models\Budget'); + $budget1->active = 1; + $budget2->active = 0; + $budget2->user_id = $budget1->user_id; + $budget1->save(); + $budget2->save(); + $this->be($budget1->user); + + $set = $this->object->getBudgets(); + + $this->assertCount(2, $set); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getCurrentRepetition + */ + public function testGetCurrentRepetition() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + /** @var Budget $budget */ + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $rep = $this->object->getCurrentRepetition($budget, new Carbon); + $this->assertNull($rep); + + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getFirstBudgetLimitDate + */ + public function testGetFirstBudgetLimitDate() + { + /** @var BudgetLimit $budget */ + $limit = FactoryMuffin::create('FireflyIII\Models\BudgetLimit'); + $date = $this->object->getFirstBudgetLimitDate($limit->budget); + $this->assertEquals($date->format('Y-m-d'), $limit->startdate->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getFirstBudgetLimitDate + */ + public function testGetFirstBudgetLimitDateNull() + { + /** @var Budget $budget */ + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $date = $this->object->getFirstBudgetLimitDate($budget); + $ownDate = Carbon::now()->startOfYear(); + $this->assertEquals($date->format('Y-m-d'), $ownDate->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getInactiveBudgets + */ + public function testGetInactiveBudgets() + { + $budget1 = FactoryMuffin::create('FireflyIII\Models\Budget'); + $budget2 = FactoryMuffin::create('FireflyIII\Models\Budget'); + $budget1->active = 1; + $budget2->active = 0; + $budget2->user_id = $budget1->user_id; + $budget1->save(); + $budget2->save(); + $this->be($budget1->user); + + $set = $this->object->getInactiveBudgets(); + + $this->assertCount(1, $set); + $this->assertEquals($set->first()->id, $budget2->id); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getJournals + */ + public function testGetJournals() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $repetition = FactoryMuffin::create('FireflyIII\Models\LimitRepetition'); + + $set = $this->object->getJournals($repetition->budgetlimit->budget, $repetition); + $this->assertTrue($set instanceof LengthAwarePaginator); + $this->assertCount(0, $set); + $this->assertEquals(1, $set->currentPage()); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getLastBudgetLimitDate + */ + public function testGetLastBudgetLimitDate() + { + /** @var BudgetLimit $budget */ + $limit = FactoryMuffin::create('FireflyIII\Models\BudgetLimit'); + $date = $this->object->getLastBudgetLimitDate($limit->budget); + $this->assertEquals($date->format('Y-m-d'), $limit->startdate->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getLastBudgetLimitDate + */ + public function testGetLastBudgetLimitDateNull() + { + + /** @var Budget $budget */ + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $date = $this->object->getLastBudgetLimitDate($budget); + $ownDate = Carbon::now()->startOfYear(); + $this->assertEquals($date->format('Y-m-d'), $ownDate->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getLimitAmountOnDate + */ + public function testGetLimitAmountOnDate() + { + $rep = FactoryMuffin::create('FireflyIII\Models\LimitRepetition'); + + $amount = $this->object->getLimitAmountOnDate($rep->budgetlimit->budget, $rep->startdate); + + $this->assertEquals($rep->amount, $amount); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getLimitAmountOnDate + */ + public function testGetLimitAmountOnDateNull() + { + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + + $amount = $this->object->getLimitAmountOnDate($budget, new Carbon); + + $this->assertNull($amount); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getWithoutBudget + */ + public function testGetWithoutBudget() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $set = $this->object->getWithoutBudget(new Carbon, new Carbon); + $this->assertCount(0, $set); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::getWithoutBudgetSum + */ + public function testGetWithoutBudgetSum() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $sum = $this->object->getWithoutBudgetSum(new Carbon, new Carbon); + $this->assertEquals(0, $sum); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::spentInPeriodCorrected + * @covers FireflyIII\Repositories\Shared\ComponentRepository::spentInPeriod + */ + public function testSpentInPeriodCorrected() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + + $amount = $this->object->spentInPeriodCorrected($budget, new Carbon, new Carbon, false); + $this->assertEquals(0, $amount); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::spentInPeriodCorrected + * @covers FireflyIII\Repositories\Shared\ComponentRepository::spentInPeriod + */ + public function testSpentInPeriodCorrectedShared() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + + $amount = $this->object->spentInPeriodCorrected($budget, new Carbon, new Carbon, true); + $this->assertEquals(0, $amount); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::store + */ + public function testStore() + { + $user = FactoryMuffin::create('FireflyIII\User'); + + $data = [ + 'name' => 'new budget ' . rand(1, 100), + 'user' => $user->id + ]; + $result = $this->object->store($data); + + $this->assertTrue($result instanceof Budget); + $this->assertEquals($result->name, $data['name']); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::update + */ + public function testUpdate() + { + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + + $data = [ + 'name' => 'update budget ' . rand(1, 100), + 'active' => true + ]; + $result = $this->object->update($budget, $data); + + $this->assertTrue($result instanceof Budget); + $this->assertEquals($result->name, $data['name']); + $this->assertEquals($result->id, $budget->id); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::updateLimitAmount + */ + public function testUpdateLimitAmount() + { + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + + $result = $this->object->updateLimitAmount($budget, new Carbon, 100); + + $this->assertTrue($result instanceof BudgetLimit); + $this->assertEquals($result->amount, 100); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::updateLimitAmount + */ + public function testUpdateLimitAmountExisting() + { + $budgetLimit = FactoryMuffin::create('FireflyIII\Models\BudgetLimit'); + + $result = $this->object->updateLimitAmount($budgetLimit->budget, $budgetLimit->startdate, 100); + + $this->assertTrue($result instanceof BudgetLimit); + $this->assertEquals($result->amount, 100); + } + + /** + * @covers FireflyIII\Repositories\Budget\BudgetRepository::updateLimitAmount + */ + public function testUpdateLimitAmountZero() + { + $budgetLimit = FactoryMuffin::create('FireflyIII\Models\BudgetLimit'); + + $result = $this->object->updateLimitAmount($budgetLimit->budget, $budgetLimit->startdate, 0); + + $this->assertTrue($result instanceof BudgetLimit); + } +} diff --git a/tests/repositories/CategoryRepositoryTest.php b/tests/repositories/CategoryRepositoryTest.php new file mode 100644 index 0000000000..63ed85ee83 --- /dev/null +++ b/tests/repositories/CategoryRepositoryTest.php @@ -0,0 +1,290 @@ +object = new CategoryRepository; + parent::setUp(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::countJournals + */ + public function testCountJournals() + { + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $result = $this->object->countJournals($category); + + $this->assertEquals(0, $result); + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::destroy + */ + public function testDestroy() + { + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->object->destroy($category); + + $count = Category::where('id', $category->id)->whereNull('deleted_at')->count(); + $this->assertEquals(0, $count); + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::getCategories + */ + public function testGetCategories() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $cat1 = FactoryMuffin::create('FireflyIII\Models\Category'); + $cat2 = FactoryMuffin::create('FireflyIII\Models\Category'); + $cat1->name = 'BBBBB'; + $cat2->name = 'AAAAA'; + $cat1->user_id = $user->id; + $cat2->user_id = $user->id; + $cat1->save(); + $cat2->save(); + $this->be($user); + + $set = $this->object->getCategories(); + $this->assertEquals('AAAAA', $set->first()->name); + $this->assertCount(2, $set); + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::getCategoriesAndExpensesCorrected + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testGetCategoriesAndExpensesCorrected() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $type = FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); // something + + // some dates: + $start = new Carbon('2014-01-01'); + $end = new Carbon('2014-01-31'); + $inRange = new Carbon('2014-01-12'); + + // generate three categories + // with ten journals each: + for ($i = 0; $i < 3; $i++) { + // category: + /** @var Category $category */ + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $category->user_id = $user->id; + $category->save(); + + for ($j = 0; $j < 10; $j++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->user_id = $user->id; + $journal->transaction_currency_id = $currency->id; + $journal->transaction_type_id = $type->id; // make it a withdrawal + $journal->date = $inRange; + $journal->save(); + // connect to category: + $category->transactionjournals()->save($journal); + } + } + + $this->be($user); + $set = $this->object->getCategoriesAndExpensesCorrected($start, $end); + $this->assertCount(3, $set); + reset($set); + + // 10 journals of 100 each = 1000. + $this->assertEquals('1000', current($set)['sum']); + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::getFirstActivityDate + */ + public function testGetFirstActivityDate() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + /** @var Category $category */ + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $journal->user_id = $user->id; + $journal->date = new Carbon('2015-02-11'); + $category->user_id = $user->id; + $category->transactionjournals()->save($journal); + $journal->save(); + $category->save(); + + $this->be($user); + + $date = $this->object->getFirstActivityDate($category); + $this->assertEquals('2015-02-11', $date->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::getFirstActivityDate + */ + public function testGetFirstActivityDateNull() + { + /** @var Category $category */ + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->be($category->user); + + $date = $this->object->getFirstActivityDate($category); + $this->assertEquals(Carbon::now()->format('Y-m-d'), $date->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::getJournals + */ + public function testGetJournals() + { + + /** @var Category $category */ + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->be($category->user); + $set = $this->object->getJournals($category, 1); + + $this->assertEquals(0, $set->count()); + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::getLatestActivity + */ + public function testGetLatestActivity() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + /** @var Category $category */ + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $journal->user_id = $user->id; + $journal->date = new Carbon('2015-02-11'); + $category->user_id = $user->id; + $category->transactionjournals()->save($journal); + $journal->save(); + $category->save(); + + $this->be($user); + + $date = $this->object->getLatestActivity($category); + $this->assertEquals('2015-02-11', $date->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::getWithoutCategory + */ + public function testGetWithoutCategory() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $set = $this->object->getWithoutCategory(new Carbon, new Carbon); + $this->assertCount(0, $set); + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::spentInPeriodCorrected + * @covers FireflyIII\Repositories\Shared\ComponentRepository::spentInPeriod + */ + public function testSpentInPeriodSumCorrected() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $sum = $this->object->spentInPeriodCorrected($category, new Carbon, new Carbon, false); + + $this->assertEquals(0, $sum); + + + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::spentInPeriodCorrected + * @covers FireflyIII\Repositories\Shared\ComponentRepository::spentInPeriod + */ + public function testSpentInPeriodSumCorrectedShared() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $sum = $this->object->spentInPeriodCorrected($category, new Carbon, new Carbon, true); + + $this->assertEquals(0, $sum); + + + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::spentOnDaySumCorrected + */ + public function testSpentOnDaySumCorrected() + { + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $sum = $this->object->spentOnDaySumCorrected($category, new Carbon); + + $this->assertEquals(0, $sum); + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::store + */ + public function testStore() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $data = [ + 'name' => 'New category' . rand(1, 100), + 'user' => $user->id + ]; + $newCategory = $this->object->store($data); + + $this->assertEquals($data['name'], $newCategory->name); + } + + /** + * @covers FireflyIII\Repositories\Category\CategoryRepository::update + */ + public function testUpdate() + { + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $data = [ + 'name' => 'New category' . rand(1, 100), + ]; + $newCategory = $this->object->update($category, $data); + + $this->assertEquals($data['name'], $newCategory->name); + } + + public function testgetLatestActivityNull() + { + /** @var Category $category */ + $category = FactoryMuffin::create('FireflyIII\Models\Category'); + $this->be($category->user); + + $date = $this->object->getLatestActivity($category); + $this->assertNull($date); + } +} diff --git a/tests/repositories/CurrencyRepositoryTest.php b/tests/repositories/CurrencyRepositoryTest.php new file mode 100644 index 0000000000..a8d09b43f3 --- /dev/null +++ b/tests/repositories/CurrencyRepositoryTest.php @@ -0,0 +1,114 @@ +object = new CurrencyRepository; + parent::setUp(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + } + + /** + * @covers FireflyIII\Repositories\Currency\CurrencyRepository::countJournals + */ + public function testCountJournals() + { + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $count = $this->object->countJournals($currency); + $this->assertEquals(0, $count); + } + + /** + * @covers FireflyIII\Repositories\Currency\CurrencyRepository::get + */ + public function testGet() + { + FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + + $set = $this->object->get(); + $this->assertCount(3, $set); // EUR is already present. + } + + /** + * @covers FireflyIII\Repositories\Currency\CurrencyRepository::getCurrencyByPreference + */ + public function testGetCurrencyByPreference() + { + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->data = $currency->code; + $preference->save(); + $found = $this->object->getCurrencyByPreference($preference); + $this->assertEquals($currency->id, $found->id); + } + + /** + * @covers FireflyIII\Repositories\Currency\CurrencyRepository::getCurrencyByPreference + */ + public function testGetCurrencyByPreferenceNull() + { + FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->data = 'ABC'; + $preference->save(); + $found = $this->object->getCurrencyByPreference($preference); + $this->assertEquals(1, $found->id); // EUR is first and will be set. + } + + /** + * @covers FireflyIII\Repositories\Currency\CurrencyRepository::store + */ + public function testStore() + { + $data = [ + 'name' => 'Some Currency', + 'code' => 'ABC', + 'symbol' => 'S' + ]; + + $currency = $this->object->store($data); + $this->assertEquals($data['name'], $currency->name); + } + + /** + * @covers FireflyIII\Repositories\Currency\CurrencyRepository::update + */ + public function testUpdate() + { + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + + $data = [ + 'name' => 'Some Currency', + 'code' => 'ABC', + 'symbol' => 'S' + ]; + + $newCurrency = $this->object->update($currency, $data); + $this->assertEquals($data['name'], $newCurrency->name); + $this->assertEquals($currency->id, $newCurrency->id); + } +} diff --git a/tests/repositories/JournalRepositoryTest.php b/tests/repositories/JournalRepositoryTest.php new file mode 100644 index 0000000000..92dc3596f4 --- /dev/null +++ b/tests/repositories/JournalRepositoryTest.php @@ -0,0 +1,572 @@ +object = new JournalRepository; + parent::setUp(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::delete + * @covers FireflyIII\Providers\EventServiceProvider::boot + * @covers FireflyIII\Providers\EventServiceProvider::registerDeleteEvents + */ + public function testDelete() + { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $transaction = $journal->transactions[0]; + $this->object->delete($journal); + + $this->assertEquals(0, TransactionJournal::where('id', $journal->id)->whereNull('deleted_at')->count()); + $this->assertEquals(0, Transaction::where('id', $transaction->id)->whereNull('deleted_at')->count()); + + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::first + */ + public function testFirst() + { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $this->be($journal->user); + + $first = $this->object->first(); + + $this->assertEquals($journal->id, $first->id); + + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::getAmountBefore + */ + public function testGetAmountBefore() + { + // create some accounts: + $expense = FactoryMuffin::create('FireflyIII\Models\Account'); // expense + FactoryMuffin::create('FireflyIII\Models\Account'); // revenue + $asset = FactoryMuffin::create('FireflyIII\Models\Account'); // asset + + // transaction type + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + + // create five journals with incrementing dates: + $today = new Carbon('2015-01-01'); + $journal = null; + for ($i = 0; $i < 5; $i++) { + // create journal: + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->transaction_type_id = $withdrawal; + $journal->date = $today; + $journal->save(); + + // update transactions: + $journal->transactions[0]->amount = -100; + $journal->transactions[0]->account_id = $asset->id; + $journal->transactions[0]->save(); + + $journal->transactions[1]->amount = 100; + $journal->transactions[1]->account_id = $expense->id; + $journal->transactions[1]->save(); + + // connect to expense + $today->addDay(); + } + + + $before = $this->object->getAmountBefore($journal, $journal->transactions[1]); + // five transactions, we check the last one, so amount should be 400. + $this->assertEquals(400, $before); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::getJournalsOfType + */ + public function testGetJournalsOfType() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $type = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $this->be($user); + $result = $this->object->getJournalsOfType($type); + $this->assertCount(0, $result); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::getJournalsOfTypes + */ + public function testGetJournalsOfTypes() + { + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + $types = ['Withdrawal', 'Expense']; + $set = $this->object->getJournalsOfTypes($types, 0, 1); + + $this->assertTrue($set instanceof LengthAwarePaginator); + $this->assertEquals(1, $set->currentPage()); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::getTransactionType + */ + public function testGetTransactionType() + { + $type = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $otherType = $this->object->getTransactionType($type->type); + $this->assertEquals($type->id, $otherType->id); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::getWithDate + */ + public function testGetWithDate() + { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $this->be($journal->user); + + $found = $this->object->getWithDate($journal->id, $journal->date); + + $this->assertEquals($journal->id, $found->id); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::saveTags + */ + public function testSaveTags() + { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $tags = ['one', 'two', 'three']; + $this->be($journal->user); + + $this->object->saveTags($journal, $tags); + + $this->assertCount(3, $journal->tags()->get()); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::store + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeWithdrawalAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeDepositAccounts + */ + public function testStore() + { + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $user = FactoryMuffin::create('FireflyIII\User'); + $data = [ + 'description' => 'Some journal ' . rand(1, 100), + 'user' => $user->id, + 'what' => 'withdrawal', + 'amount_currency_id' => $currency->id, + 'account_id' => $account1->id, + 'expense_account' => 'Some expense account', + 'date' => '2014-01-01', + 'category' => 'Some category', + 'budget_id' => $budget->id, + 'amount' => 100, + 'tags' => ['one', 'two', 'three'] + + + ]; + + $journal = $this->object->store($data); + + $this->assertEquals($data['description'], $journal->description); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::store + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeWithdrawalAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeDepositAccounts + */ + public function testStoreDeposit() + { + for ($i = 0; $i < 4; $i++) { + FactoryMuffin::create('FireflyIII\Models\AccountType'); + } + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $user = FactoryMuffin::create('FireflyIII\User'); + $data = [ + 'description' => 'Some journal ' . rand(1, 100), + 'user' => $user->id, + 'what' => 'deposit', + 'amount_currency_id' => $currency->id, + 'account_id' => $account1->id, + 'revenue_account' => 'Some revenue account', + 'date' => '2014-01-01', + 'category' => 'Some category', + 'budget_id' => $budget->id, + 'amount' => 100, + 'tags' => ['one', 'two', 'three'] + + + ]; + + $journal = $this->object->store($data); + + $this->assertEquals($data['description'], $journal->description); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::store + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeWithdrawalAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeDepositAccounts + */ + public function testStoreExpenseWithCash() + { + for ($i = 0; $i < 4; $i++) { + FactoryMuffin::create('FireflyIII\Models\AccountType'); + } + + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $user = FactoryMuffin::create('FireflyIII\User'); + $data = [ + 'description' => 'Some journal ' . rand(1, 100), + 'user' => $user->id, + 'what' => 'withdrawal', + 'amount_currency_id' => $currency->id, + 'account_id' => $account1->id, + 'expense_account' => '', + 'date' => '2014-01-01', + 'category' => 'Some other category', + 'budget_id' => $budget->id, + 'amount' => 100, + 'tags' => ['one', 'two', 'three'] + + + ]; + + $journal = $this->object->store($data); + + $this->assertEquals($data['description'], $journal->description); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::store + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeWithdrawalAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeDepositAccounts + * @expectedException Symfony\Component\HttpKernel\Exception\HttpException + */ + public function testStoreInvalidFromAccount() + { + for ($i = 0; $i < 4; $i++) { + FactoryMuffin::create('FireflyIII\Models\AccountType'); + } + + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $user = FactoryMuffin::create('FireflyIII\User'); + $data = [ + 'description' => 'Some journal ' . rand(1, 100), + 'user' => $user->id, + 'what' => 'transfer', + 'amount_currency_id' => $currency->id, + 'account_from_id' => $account1->id, + 'account_to_id' => 17, + 'date' => '2014-01-01', + 'category' => 'Some other category', + 'budget_id' => $budget->id, + 'amount' => 100, + 'tags' => ['one', 'two', 'three'] + + + ]; + + $journal = $this->object->store($data); + + $this->assertEquals($data['description'], $journal->description); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::store + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeWithdrawalAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeDepositAccounts + * @expectedException Symfony\Component\HttpKernel\Exception\HttpException + */ + public function testStoreInvalidToAccount() + { + for ($i = 0; $i < 4; $i++) { + FactoryMuffin::create('FireflyIII\Models\AccountType'); + } + + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $user = FactoryMuffin::create('FireflyIII\User'); + $data = [ + 'description' => 'Some journal ' . rand(1, 100), + 'user' => $user->id, + 'what' => 'transfer', + 'amount_currency_id' => $currency->id, + 'account_from_id' => 17, + 'account_to_id' => $account1->id, + 'date' => '2014-01-01', + 'category' => 'Some other category', + 'budget_id' => $budget->id, + 'amount' => 100, + 'tags' => ['one', 'two', 'three'] + + + ]; + + $journal = $this->object->store($data); + + $this->assertEquals($data['description'], $journal->description); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::store + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeWithdrawalAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeDepositAccounts + */ + public function testStoreRevenueWithCash() + { + for ($i = 0; $i < 4; $i++) { + FactoryMuffin::create('FireflyIII\Models\AccountType'); + } + + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $user = FactoryMuffin::create('FireflyIII\User'); + $data = [ + 'description' => 'Some journal ' . rand(1, 100), + 'user' => $user->id, + 'what' => 'deposit', + 'amount_currency_id' => $currency->id, + 'account_id' => $account1->id, + 'revenue_account' => '', + 'date' => '2014-01-01', + 'category' => 'Some other category', + 'budget_id' => $budget->id, + 'amount' => 100, + 'tags' => ['one', 'two', 'three'] + + + ]; + + $journal = $this->object->store($data); + + $this->assertEquals($data['description'], $journal->description); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::store + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeWithdrawalAccounts + * @covers FireflyIII\Repositories\Journal\JournalRepository::storeDepositAccounts + */ + public function testStoreTransfer() + { + for ($i = 0; $i < 4; $i++) { + FactoryMuffin::create('FireflyIII\Models\AccountType'); + } + + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + $user = FactoryMuffin::create('FireflyIII\User'); + $data = [ + 'description' => 'Some journal ' . rand(1, 100), + 'user' => $user->id, + 'what' => 'transfer', + 'amount_currency_id' => $currency->id, + 'account_from_id' => $account1->id, + 'account_to_id' => $account2->id, + 'date' => '2014-01-01', + 'category' => 'Some other category', + 'budget_id' => $budget->id, + 'amount' => 100, + 'tags' => ['one', 'two', 'three'] + + + ]; + + $journal = $this->object->store($data); + + $this->assertEquals($data['description'], $journal->description); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::update + * @covers FireflyIII\Repositories\Journal\JournalRepository::updateTags + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testUpdate() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + for ($i = 0; $i < 4; $i++) { + FactoryMuffin::create('FireflyIII\Models\AccountType'); + } + + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + + // two transactions + Transaction::create( + [ + 'account_id' => $account1->id, + 'transaction_journal_id' => $journal->id, + 'amount' => 100, + ] + ); + Transaction::create( + [ + 'account_id' => $account1->id, + 'transaction_journal_id' => $journal->id, + 'amount' => -100, + ] + ); + + + $data = [ + 'amount_currency_id' => $currency->id, + 'description' => 'New description ' . rand(1, 100), + 'date' => '2015-01-01', + 'category' => 'SomenewCat', + 'amount' => 50, + 'user' => $journal->user_id, + 'budget_id' => $budget->id, + 'account_from_id' => $account1->id, + 'account_to_id' => $account2->id, + 'revenue_account' => 'Some revenue account', + 'expense_account' => 'Some expense account', + 'tags' => ['a', 'b', 'c'] + + ]; + + $result = $this->object->update($journal, $data); + + $this->assertEquals($result->description, $data['description']); + $this->assertEquals($result->amount, 50); + } + + /** + * @covers FireflyIII\Repositories\Journal\JournalRepository::update + * @covers FireflyIII\Repositories\Journal\JournalRepository::updateTags + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testUpdateNoTags() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + for ($i = 0; $i < 4; $i++) { + FactoryMuffin::create('FireflyIII\Models\AccountType'); + } + + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $budget = FactoryMuffin::create('FireflyIII\Models\Budget'); + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + + // two transactions + Transaction::create( + [ + 'account_id' => $account1->id, + 'transaction_journal_id' => $journal->id, + 'amount' => 100, + ] + ); + Transaction::create( + [ + 'account_id' => $account1->id, + 'transaction_journal_id' => $journal->id, + 'amount' => -100, + ] + ); + + + $data = [ + 'amount_currency_id' => $currency->id, + 'description' => 'New description ' . rand(1, 100), + 'date' => '2015-01-01', + 'category' => 'SomenewCat', + 'amount' => 50, + 'user' => $journal->user_id, + 'budget_id' => $budget->id, + 'account_from_id' => $account1->id, + 'account_to_id' => $account2->id, + 'revenue_account' => 'Some revenue account', + 'expense_account' => 'Some expense account', + 'tags' => [] + + ]; + + $result = $this->object->update($journal, $data); + + $this->assertEquals($result->description, $data['description']); + $this->assertEquals($result->amount, 50); + } + +} diff --git a/tests/repositories/PiggyBankRepositoryTest.php b/tests/repositories/PiggyBankRepositoryTest.php new file mode 100644 index 0000000000..79c9079c4e --- /dev/null +++ b/tests/repositories/PiggyBankRepositoryTest.php @@ -0,0 +1,196 @@ +object = new PiggyBankRepository; + parent::setUp(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + } + + /** + * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::createEvent + */ + public function testCreateEvent() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->object->createEvent($piggyBank, 100); + + $this->assertCount(1, $piggyBank->piggybankevents()->get()); + } + + + /** + * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::destroy + * @covers FireflyIII\Providers\EventServiceProvider::boot + * @covers FireflyIII\Providers\EventServiceProvider::registerDeleteEvents + */ + public function testDestroy() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + + $this->object->destroy($piggyBank); + + $this->assertCount(0, PiggyBank::where('id', $piggyBank->id)->whereNull('deleted_at')->get()); + + } + + /** + * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::getEventSummarySet + */ + public function testGetEventSummarySet() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $set = $this->object->getEventSummarySet($piggyBank); + + $this->assertCount(0, $set); + } + + /** + * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::getEvents + */ + public function testGetEvents() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $set = $this->object->getEvents($piggyBank); + + $this->assertCount(0, $set); + } + + /** + * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::getPiggyBanks + */ + public function testGetPiggyBanks() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $this->be($piggyBank->account->user); + $set = $this->object->getPiggyBanks(); + + $this->assertCount(1, $set); + } + + /** + * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::reset + */ + public function testReset() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $piggyBank->order = 4; + $piggyBank->save(); + $this->be($piggyBank->account->user); + $this->object->reset(); + + $this->assertCount(1, PiggyBank::where('order', 0)->get()); + } + + /** + * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::setOrder + */ + public function testSetOrder() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + $piggyBank->order = 4; + $this->be($piggyBank->account->user); + $piggyBank->save(); + + $this->object->setOrder($piggyBank->id, 3); + $newPiggy = PiggyBank::find($piggyBank->id); + $this->assertEquals(3, $newPiggy->order); + } + + /** + * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::store + * @covers FireflyIII\Providers\EventServiceProvider::boot + * @covers FireflyIII\Providers\EventServiceProvider::registerCreateEvents + */ + public function testStore() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + $data = [ + 'account_id' => $account->id, + 'name' => 'Some piggy', + 'targetamount' => 100, + 'order' => 1, + 'remind_me' => false, + 'reminder_skip' => 0, + + ]; + + $piggyBank = $this->object->store($data); + + $this->assertEquals(1, $piggyBank->id); + } + + /** + * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::store + * @covers FireflyIII\Providers\EventServiceProvider::boot + * @covers FireflyIII\Providers\EventServiceProvider::registerCreateEvents + */ + public function testStoreRedirect() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + $data = [ + 'account_id' => $account->id, + 'name' => 'Some piggy', + 'targetamount' => 100, + 'create_another' => 1, + 'order' => 1, + 'remind_me' => false, + 'reminder_skip' => 0, + + ]; + + $piggyBank = $this->object->store($data); + + $this->assertEquals(1, $piggyBank->id); + } + + /** + * @covers FireflyIII\Repositories\PiggyBank\PiggyBankRepository::update + */ + public function testUpdate() + { + $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank'); + + $data = [ + 'name' => 'Update piggy ' . rand(1, 100), + 'account_id' => $piggyBank->account_id, + 'targetamount' => 101, + 'targetdate' => new Carbon, + 'startdate' => new Carbon, + ]; + + $new = $this->object->update($piggyBank, $data); + + $this->assertEquals($data['name'], $new->name); + $this->assertEquals($piggyBank->id, $new->id); + } +} diff --git a/tests/repositories/TagRepositoryBasicTest.php b/tests/repositories/TagRepositoryBasicTest.php new file mode 100644 index 0000000000..b49857ed34 --- /dev/null +++ b/tests/repositories/TagRepositoryBasicTest.php @@ -0,0 +1,182 @@ +object = new TagRepository; + parent::setUp(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + } + /** + * @covers FireflyIII\Repositories\Tag\TagRepository::update + */ + public function testUpdate() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + + + $data = [ + 'tag' => 'Hello' . rand(1, 100), + 'date' => '2012-01-01', + 'description' => 'Some', + 'latitude' => 12, + 'longitude' => 13, + 'zoomLevel' => 4, + 'tagMode' => 'nothing' + ]; + $this->be($tag->user); + + $newTag = $this->object->update($tag, $data); + $this->assertEquals($data['tag'], $newTag->tag); + $this->assertEquals($tag->id, $newTag->id); + } + + /** + * @covers FireflyIII\Repositories\Tag\TagRepository::store + */ + public function testStore() + { + $user = FactoryMuffin::create('FireflyIII\User'); + + $data = [ + 'tag' => 'Hello' . rand(1, 100), + 'date' => '2012-01-01', + 'description' => 'Some', + 'latitude' => 12, + 'longitude' => 13, + 'zoomLevel' => 4, + 'tagMode' => 'nothing' + ]; + $this->be($user); + + $tag = $this->object->store($data); + $this->assertEquals($data['tag'], $tag->tag); + } + + + /** + * @covers FireflyIII\Repositories\Tag\TagRepository::destroy + */ + public function testDestroy() + { + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $this->object->destroy($tag); + + $this->assertCount(0, Tag::where('id', $tag->id)->whereNull('deleted_at')->get()); + + } + + /** + * @covers FireflyIII\Repositories\Tag\TagRepository::get + */ + public function testGet() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $tag1 = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag2 = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag1->tag = 'BBB'; + $tag2->tag = 'AAA'; + $tag1->user_id = $user->id; + $tag2->user_id = $user->id; + + $tag1->save(); + $tag2->save(); + + $this->be($user); + + $set = $this->object->get(); + + $this->assertCount(2, $set); + $this->assertEquals('AAA', $set->first()->tag); + } + + + /** + * @covers FireflyIII\Repositories\Tag\TagRepository::coveredByBalancingActs + */ + public function testCoveredByBalancingActs() + { + // create a user: + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // create transaction and account types: + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // deposit + $transfer = FactoryMuffin::create('FireflyIII\Models\TransactionType'); // transfer + FactoryMuffin::create('FireflyIII\Models\AccountType'); // expense + FactoryMuffin::create('FireflyIII\Models\AccountType'); // revenue + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); // asset + + // create two accounts: + $fromAccount = FactoryMuffin::create('FireflyIII\Models\Account'); // asset + $toAccount = FactoryMuffin::create('FireflyIII\Models\Account'); // asset + $fromAccount->account_type_id = $asset->id; + $toAccount->account_type_id = $asset->id; + $fromAccount->save(); + $toAccount->save(); + + + // create a tag + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'balancingAct'; + $tag->user_id = $user->id; + $tag->save(); + + // date + $today = new Carbon('2014-01-12'); + $start = new Carbon('2014-01-01'); + $end = new Carbon('2014-01-31'); + + // store five journals + for ($i = 0; $i < 5; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); // deposit! + // set date: + $journal->date = $today; + $journal->user_id = $user->id; + $journal->transaction_type_id = $transfer->id; + $journal->tags()->save($tag); + $journal->save(); + + // update accounts: + $journal->transactions[0]->account_id = $fromAccount->id; + $journal->transactions[0]->amount = '-100'; + $journal->transactions[0]->save(); + $journal->transactions[1]->account_id = $toAccount->id; + $journal->transactions[1]->amount = '100'; + $journal->transactions[1]->save(); + + } + + $amount = $this->object->coveredByBalancingActs($toAccount, $start, $end); + // five transactions, 100 each. + $this->assertEquals('500', $amount); + } + +} diff --git a/tests/repositories/TagRepositoryTest.php b/tests/repositories/TagRepositoryTest.php new file mode 100644 index 0000000000..bcd763cd00 --- /dev/null +++ b/tests/repositories/TagRepositoryTest.php @@ -0,0 +1,656 @@ +object = new TagRepository; + parent::setUp(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + } + + /** + * Already connected tag and transaction journal returns FALSE. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::connect + */ + public function testConnectAlreadyConnected() + { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $journal->tags()->save($tag); + + $result = $this->object->connect($journal, $tag); + $this->assertFalse($result); + + } + + /** + * A deposit cannot be connected to a balancing act. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::connect + * @covers FireflyIII\Repositories\Tag\TagRepository::connectBalancingAct + */ + public function testConnectBalancingOneDeposit() + { + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + + $journal->transaction_type_id = $deposit->id; + $tag->tagMode = 'balancingAct'; + + $tag->save(); + $journal->save(); + + $result = $this->object->connect($journal, $tag); + $this->assertFalse($result); + + } + + /** + * Connecting a single transfer to a balancing act is possible if there are no + * other transfers already connected. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::connect + * @covers FireflyIII\Repositories\Tag\TagRepository::connectBalancingAct + */ + public function testConnectBalancingOneTransfer() + { + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $transfer = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + + $journal->transaction_type_id = $transfer->id; + $tag->tagMode = 'balancingAct'; + + $tag->save(); + $journal->save(); + + $result = $this->object->connect($journal, $tag); + $this->assertTrue($result); + + } + + /** + * Connecting a single withdrawal to a balancing act is possible if there are + * not other withdrawals already connected. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::connect + * @covers FireflyIII\Repositories\Tag\TagRepository::connectBalancingAct + */ + public function testConnectBalancingOneWithdrawal() + { + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + + $journal->transaction_type_id = $withdrawal->id; + $tag->tagMode = 'balancingAct'; + + $tag->save(); + $journal->save(); + + $result = $this->object->connect($journal, $tag); + $this->assertTrue($result); + + } + + /** + * Default connection between a journal and a tag. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::connect + */ + public function testConnectDefault() + { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'nothing'; + $tag->save(); + + $result = $this->object->connect($journal, $tag); + $this->assertTrue($result); + + } + + /** + * Fallback for connect then the tag mode is unknown + * + * @covers FireflyIII\Repositories\Tag\TagRepository::connect + */ + public function testConnectInvalidType() + { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'Idontknow'; + $tag->save(); + + $result = $this->object->connect($journal, $tag); + $this->assertFalse($result); + + } + + /** + * Once one or more journals have been accepted by the tag, others must match the asset account + * id. For this to work, we must also create an asset account, and a transaction. + * + * Connecting a deposit to a tag that already has a withdrawal. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::connect + * @covers FireflyIII\Repositories\Tag\TagRepository::connectAdvancePayment + * @covers FireflyIII\Repositories\Tag\TagRepository::matchAll + */ + public function testConnectPaymentMultipleMatch() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); + + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + + + $journal1 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal2 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + + // transactions for both: + $journal1->transactions[0]->account_id = $account->id; + $journal2->transactions[0]->account_id = $account->id; + $journal1->transactions[1]->account_id = $account->id; + $journal2->transactions[1]->account_id = $account->id; + $journal1->transactions[0]->save(); + $journal2->transactions[0]->save(); + $journal1->transactions[1]->save(); + $journal2->transactions[1]->save(); + + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + + $journal1->transaction_type_id = $withdrawal->id; + $journal2->transaction_type_id = $deposit->id; + $tag->tagMode = 'advancePayment'; + $account->account_type_id = $asset->id; + + $tag->save(); + $journal1->save(); + $journal2->save(); + $account->save(); + // connect journal1: + + $journal1->tags()->save($tag); + + $result = $this->object->connect($journal2, $tag); + + $this->assertTrue($result); + + } + + /** + * Once one or more journals have been accepted by the tag, others must match the asset account + * id. For this to work, we must also create an asset account, and a transaction. + * + * This covers the advance payment + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @covers FireflyIII\Repositories\Tag\TagRepository::connect + * @covers FireflyIII\Repositories\Tag\TagRepository::connectAdvancePayment + * @covers FireflyIII\Repositories\Tag\TagRepository::matchAll + */ + public function testConnectPaymentNoMatch() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); + + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + + + $journal1 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal2 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + + // transactions for both: + $journal1->transactions[0]->account_id = $account1->id; + $journal2->transactions[0]->account_id = $account2->id; + $journal1->transactions[1]->account_id = $account1->id; + $journal2->transactions[1]->account_id = $account2->id; + $journal1->transactions[0]->save(); + $journal2->transactions[0]->save(); + $journal1->transactions[1]->save(); + $journal2->transactions[1]->save(); + + + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + + $journal1->transaction_type_id = $withdrawal->id; + $journal2->transaction_type_id = $deposit->id; + $tag->tagMode = 'advancePayment'; + $account1->account_type_id = $asset->id; + $account2->account_type_id = $asset->id; + + $tag->save(); + $journal1->save(); + $journal2->save(); + $account1->save(); + $account2->save(); + // connect journal1: + $journal1->tags()->save($tag); + + $result = $this->object->connect($journal2, $tag); + // account1 and account2 are different, so false: + $this->assertFalse($result); + + } + + /** + * Once one or more journals have been accepted by the tag, others must match the asset account + * id. For this to work, we must also create an asset account, and a transaction. + * + * This covers the advance payment + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @covers FireflyIII\Repositories\Tag\TagRepository::connect + * @covers FireflyIII\Repositories\Tag\TagRepository::connectAdvancePayment + * @covers FireflyIII\Repositories\Tag\TagRepository::matchAll + */ + public function testConnectPaymentNoMatchDeposit() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + $asset = FactoryMuffin::create('FireflyIII\Models\AccountType'); + + $account1 = FactoryMuffin::create('FireflyIII\Models\Account'); + $account2 = FactoryMuffin::create('FireflyIII\Models\Account'); + + + $journal1 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal2 = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + + // transactions for both: + $journal1->transactions[0]->account_id = $account1->id; + $journal2->transactions[0]->account_id = $account2->id; + $journal1->transactions[1]->account_id = $account1->id; + $journal2->transactions[1]->account_id = $account2->id; + $journal1->transactions[0]->save(); + $journal2->transactions[0]->save(); + $journal1->transactions[1]->save(); + $journal2->transactions[1]->save(); + + + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + + $journal1->transaction_type_id = $withdrawal->id; + $journal2->transaction_type_id = $deposit->id; + $tag->tagMode = 'advancePayment'; + $account1->account_type_id = $asset->id; + $account2->account_type_id = $asset->id; + + $tag->save(); + $journal1->save(); + $journal2->save(); + $account1->save(); + $account2->save(); + // connect journal1: + $journal2->tags()->save($tag); + + $result = $this->object->connect($journal1, $tag); + // account1 and account2 are different, so false: + $this->assertFalse($result); + + } + + /** + * An advance payment accepts no transfers. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::connect + * @covers FireflyIII\Repositories\Tag\TagRepository::connectAdvancePayment + * @covers FireflyIII\Repositories\Tag\TagRepository::matchAll + */ + public function testConnectPaymentOneTransfer() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $transfer = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + + $journal->transaction_type_id = $transfer->id; + $tag->tagMode = 'advancePayment'; + + $tag->save(); + $journal->save(); + + $result = $this->object->connect($journal, $tag); + $this->assertFalse($result); + + } + + /** + * An advance payment accepts only one withdrawal, not two. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::connect + * @covers FireflyIII\Repositories\Tag\TagRepository::connectAdvancePayment + * @covers FireflyIII\Repositories\Tag\TagRepository::matchAll + */ + public function testConnectPaymentOneWithdrawal() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + + $journal->transaction_type_id = $withdrawal->id; + $tag->tagMode = 'advancePayment'; + + $tag->save(); + $journal->save(); + + $result = $this->object->connect($journal, $tag); + $this->assertTrue($result); + + } + + /** + * An advance payment accepts only one withdrawal, not two. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::connect + * @covers FireflyIII\Repositories\Tag\TagRepository::connectAdvancePayment + * @covers FireflyIII\Repositories\Tag\TagRepository::matchAll + */ + public function testConnectPaymentTwoWithdrawals() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $otherJournal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + + $journal->transaction_type_id = $withdrawal->id; + $otherJournal->transaction_type_id = $withdrawal->id; + $tag->tagMode = 'advancePayment'; + + $tag->save(); + $journal->save(); + $otherJournal->save(); + $otherJournal->tags()->save($tag); + + $result = $this->object->connect($journal, $tag); + $this->assertFalse($result); + + } + + /** + * An advance payment accepts only one withdrawal, not two, even not + * if the accounts are the same + * + * @covers FireflyIII\Repositories\Tag\TagRepository::connect + * @covers FireflyIII\Repositories\Tag\TagRepository::connectAdvancePayment + * @covers FireflyIII\Repositories\Tag\TagRepository::matchAll + */ + public function testConnectPaymentTwoWithdrawalsSameAccounts() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $otherJournal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + + $journal->transaction_type_id = $withdrawal->id; + $otherJournal->transaction_type_id = $withdrawal->id; + + // match up accounts: + $otherJournal->transactions[0]->account_id = $journal->transactions[0]->account_id; + $otherJournal->transactions[1]->account_id = $journal->transactions[1]->account_id; + $otherJournal->transactions[0]->save(); + $otherJournal->transactions[1]->save(); + + + $tag->tagMode = 'advancePayment'; + + $tag->save(); + $journal->save(); + $otherJournal->save(); + $otherJournal->tags()->save($tag); + + $result = $this->object->connect($journal, $tag); + $this->assertFalse($result); + } + + + /** + * By default, any tag can become an advancePayment + * + * @covers FireflyIII\Repositories\Tag\TagRepository::tagAllowAdvance + */ + public function testTagAllowAdvance() + { + // create a tag + $user = FactoryMuffin::create('FireflyIII\User'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'nothing'; + $tag->user_id = $user->id; + $tag->save(); + + $result = $this->object->tagAllowAdvance($tag); + $this->assertTrue($result); + } + + /** + * If the tag has one transfer, it can NOT become an advance payment. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::tagAllowAdvance + */ + public function testTagAllowAdvanceWithTransfer() + { + // create a tag + $user = FactoryMuffin::create('FireflyIII\User'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // deposit + $transfer = FactoryMuffin::create('FireflyIII\Models\TransactionType'); // transfer + $tag->tagMode = 'nothing'; + $tag->user_id = $user->id; + $tag->save(); + + // create withdrawal: + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->transaction_type_id = $transfer->id; + $journal->save(); + $journal->tags()->save($tag); + + $result = $this->object->tagAllowAdvance($tag); + $this->assertFalse($result); + } + + /** + * If the tag has one withdrawal, it can still become an advance payment. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::tagAllowAdvance + */ + public function testTagAllowAdvanceWithWithdrawal() + { + // create a tag + $user = FactoryMuffin::create('FireflyIII\User'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'nothing'; + $tag->user_id = $user->id; + $tag->save(); + + // create withdrawal: + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->tags()->save($tag); + + $result = $this->object->tagAllowAdvance($tag); + $this->assertTrue($result); + } + + /** + * If the tag has two withdrawals, it CANNOT become an advance payment. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::tagAllowAdvance + */ + public function testTagAllowAdvanceWithWithdrawals() + { + // create a tag + $user = FactoryMuffin::create('FireflyIII\User'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $withdrawal = FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $tag->tagMode = 'nothing'; + $tag->user_id = $user->id; + $tag->save(); + + // create withdrawals + for ($i = 0; $i < 2; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->transaction_type_id = $withdrawal->id; + $journal->save(); + $journal->tags()->save($tag); + } + + $result = $this->object->tagAllowAdvance($tag); + $this->assertFalse($result); + } + + /** + * By default, an empty tag can become a balancing act. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::tagAllowBalancing + */ + public function testTagAllowBalancing() + { + // create a tag + $user = FactoryMuffin::create('FireflyIII\User'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'nothing'; + $tag->user_id = $user->id; + $tag->save(); + + $result = $this->object->tagAllowBalancing($tag); + $this->assertTrue($result); + } + + /** + * When the tag has one deposit, it can NOT become a balancing act. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::tagAllowBalancing + */ + public function testTagAllowBalancingDeposit() + { + + // create a tag + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + $deposit = FactoryMuffin::create('FireflyIII\Models\TransactionType'); // deposit + $user = FactoryMuffin::create('FireflyIII\User'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'nothing'; + $tag->user_id = $user->id; + $tag->save(); + + // create three journals and connect them: + for ($i = 0; $i < 1; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->transaction_type_id = $deposit->id; + $journal->save(); + $journal->tags()->save($tag); + } + + $result = $this->object->tagAllowBalancing($tag); + $this->assertFalse($result); + } + + /** + * When the tag has more than 2 transactions connected to it, it cannot become abalancing act. + * + * @covers FireflyIII\Repositories\Tag\TagRepository::tagAllowBalancing + */ + public function testTagAllowBalancingManyJournals() + { + // create a tag + $user = FactoryMuffin::create('FireflyIII\User'); + $tag = FactoryMuffin::create('FireflyIII\Models\Tag'); + $tag->tagMode = 'nothing'; + $tag->user_id = $user->id; + $tag->save(); + + // create three journals and connect them: + for ($i = 0; $i < 3; $i++) { + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $journal->tags()->save($tag); + } + + $result = $this->object->tagAllowBalancing($tag); + $this->assertFalse($result); + } +} diff --git a/tests/support/AmountSupportTest.php b/tests/support/AmountSupportTest.php new file mode 100644 index 0000000000..63631e0578 --- /dev/null +++ b/tests/support/AmountSupportTest.php @@ -0,0 +1,300 @@ +object = new Amount; + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + */ + public function tearDown() + { + parent::tearDown(); + } + + /** + * @covers FireflyIII\Support\Amount::format + * @covers FireflyIII\Support\Amount::getCurrencySymbol + */ + public function testFormat() + { + $amount = '123'; + $result = $this->object->format($amount, true); + $this->assertTrue(str_contains($result, $amount)); + } + + /** + * @covers FireflyIII\Support\Amount::formatJournal + */ + public function testFormatJournalColouredTransfer() + { + + + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // deposit + /** @var \FireflyIII\Models\TransactionJournal $journal */ + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $symbol = $journal->transactionCurrency->symbol; + + $result = $this->object->formatJournal($journal, true); + + // transfer is blue: + $this->assertTrue(str_contains($result, '')); + // transfer contains currency code: + $this->assertTrue(str_contains($result, $symbol)); + // all amounts are 100. + $this->assertTrue(str_contains($result, '100')); + } + + /** + * @covers FireflyIII\Support\Amount::formatJournal + */ + public function testFormatJournalUncolouredTransfer() + { + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // deposit + /** @var \FireflyIII\Models\TransactionJournal $journal */ + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $symbol = $journal->transactionCurrency->symbol; + + $result = $this->object->formatJournal($journal, false); + + // transfer is not blue: + $this->assertFalse(str_contains($result, '')); + // transfer contains currency code: + $this->assertTrue(str_contains($result, $symbol)); + // all amounts are 100. + $this->assertTrue(str_contains($result, '100')); + } + + /** + * @covers FireflyIII\Support\Amount::formatJournal + */ + public function testFormatJournalWithSymbol() + { + FactoryMuffin::create('FireflyIII\Models\TransactionType'); // withdrawal + /** @var \FireflyIII\Models\TransactionJournal $journal */ + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $symbol = $journal->transactionCurrency->symbol; + $journal->symbol = $symbol; + + $result = $this->object->formatJournal($journal, true); + + // transfer is not blue: + $this->assertFalse(str_contains($result, '')); + // transfer contains currency code: + $this->assertTrue(str_contains($result, $symbol)); + // all amounts are 100. + $this->assertTrue(str_contains($result, '100')); + } + + /** + * @covers FireflyIII\Support\Amount::formatJournal + */ + public function testFormatJournalWithdrawal() + { + /** @var \FireflyIII\Models\TransactionJournal $journal */ + $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal'); + $symbol = $journal->transactionCurrency->symbol; + + $result = $this->object->formatJournal($journal, true); + + // transfer is not blue: + $this->assertFalse(str_contains($result, '')); + // transfer contains currency code: + $this->assertTrue(str_contains($result, $symbol)); + // all amounts are 100. + $this->assertTrue(str_contains($result, '100')); + } + + /** + * @covers FireflyIII\Support\Amount::formatTransaction + */ + public function testFormatTransaction() + { + // is a withdrawal. + $transaction = FactoryMuffin::create('FireflyIII\Models\Transaction'); + $transaction->amount = -100; + $transaction->save(); + $result = $this->object->formatTransaction($transaction, true); + + + // withdrawal is red: + $this->assertTrue(str_contains($result, '')); + // all amounts are 100. + $this->assertTrue(str_contains($result, '100')); + } + + /** + * @covers FireflyIII\Support\Amount::formatWithSymbol + */ + public function testFormatWithSymbolColouredAboveZero() + { + $amount = 123; + $symbol = 'top'; + $coloured = true; + + $result = $this->object->formatWithSymbol($symbol, $amount, $coloured); + + // has colour: + $this->assertTrue(str_contains($result, '')); + // has symbol: + $this->assertTrue(str_contains($result, $symbol)); + // has amount: + $this->assertTrue(str_contains($result, '123')); + } + + /** + * @covers FireflyIII\Support\Amount::formatWithSymbol + */ + public function testFormatWithSymbolColouredBelowZero() + { + $amount = -123; + $symbol = 'top'; + $coloured = true; + + $result = $this->object->formatWithSymbol($symbol, $amount, $coloured); + + // has colour: + $this->assertTrue(str_contains($result, '')); + // has symbol: + $this->assertTrue(str_contains($result, $symbol)); + // has amount: + $this->assertTrue(str_contains($result, '123')); + } + + /** + * @covers FireflyIII\Support\Amount::formatWithSymbol + */ + public function testFormatWithSymbolColouredZero() + { + $amount = 0.0; + $symbol = 'top'; + $coloured = true; + + $result = $this->object->formatWithSymbol($symbol, $amount, $coloured); + + // has colour: + $this->assertTrue(str_contains($result, '#999')); + // has symbol: + $this->assertTrue(str_contains($result, $symbol)); + // has amount: + $this->assertTrue(str_contains($result, '0')); + } + + /** + * @covers FireflyIII\Support\Amount::formatWithSymbol + */ + public function testFormatWithSymbolNotColoured() + { + $amount = 0; + $symbol = 'top'; + $coloured = false; + + $result = $this->object->formatWithSymbol($symbol, $amount, $coloured); + + // has symbol: + $this->assertTrue(str_contains($result, $symbol)); + // has amount: + $this->assertTrue(str_contains($result, '0')); + } + + /** + * @covers FireflyIII\Support\Amount::getAllCurrencies + */ + public function testGetAllCurrencies() + { + $size = TransactionCurrency::count(); + $list = $this->object->getAllCurrencies(); + $this->assertCount($size, $list); + } + + /** + * @covers FireflyIII\Support\Amount::getCurrencyCode + */ + public function testGetCurrencyCode() + { + $code = $this->object->getCurrencyCode(); + $this->assertEquals('EUR', $code); + } + + /** + * @covers FireflyIII\Support\Amount::getCurrencyCode + */ + public function testGetCurrencyCodeNoSuchCurrency() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + // delete any currency preferences: + Preference::where('user_id', $user->id)->delete(); + + // delete transaction currencies: + foreach (TransactionCurrency::get() as $c) { + $c->delete(); + } + + $preference = FactoryMuffin::create('FireflyIII\Models\Preference'); + $preference->user_id = $user->id; + $preference->name = 'currencyPreference'; + $preference->data = 'SOM'; + $preference->save(); + + Preferences::shouldReceive('get')->withArgs(['currencyPreference', 'EUR'])->andReturn($preference); + + $code = $this->object->getCurrencyCode(); + $this->assertEquals('EUR', $code); + } + + /** + * @covers FireflyIII\Support\Amount::getCurrencySymbol + */ + public function testGetCurrencySymbol() + { + // will the the euro: + $eur = TransactionCurrency::whereCode('EUR')->first(); + + $result = $this->object->getCurrencySymbol(); + $this->assertEquals($eur->symbol, $result); + } + + /** + * @covers FireflyIII\Support\Amount::getDefaultCurrency + */ + public function testGetDefaultCurrency() + { + // will the the euro: + $eur = TransactionCurrency::whereCode('EUR')->first(); + + $result = $this->object->getDefaultCurrency(); + $this->assertEquals($eur->id, $result->id); + } + + +} diff --git a/tests/support/ExpandedFormTest.php b/tests/support/ExpandedFormTest.php new file mode 100644 index 0000000000..cd15532dd0 --- /dev/null +++ b/tests/support/ExpandedFormTest.php @@ -0,0 +1,202 @@ +object = new ExpandedForm; + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + } + + /** + * @covers FireflyIII\Support\ExpandedForm::amount + * @covers FireflyIII\Support\ExpandedForm::label + * @covers FireflyIII\Support\ExpandedForm::expandOptionArray + * @covers FireflyIII\Support\ExpandedForm::getHolderClasses + * @covers FireflyIII\Support\ExpandedForm::fillFieldValue + */ + public function testAmount() + { + $result = $this->object->amount('abcde', '12.23', ['label' => 'Some Label']); + + $this->assertTrue(str_contains($result, '23')); + $this->assertTrue(str_contains($result, 'abcde_holder')); + } + + /** + * @covers FireflyIII\Support\ExpandedForm::balance + * @covers FireflyIII\Support\ExpandedForm::label + */ + public function testBalance() + { + $result = $this->object->balance('abcde', '12.23', []); + + $this->assertTrue(str_contains($result, '23')); + $this->assertTrue(str_contains($result, 'abcde_holder')); + } + + /** + * @covers FireflyIII\Support\ExpandedForm::checkbox + * @covers FireflyIII\Support\ExpandedForm::getHolderClasses + */ + public function testCheckbox() + { + // add error to session for this thing: + $errors = new MessageBag; + $errors->add('abcde', 'Some error here.'); + $this->session(['errors' => $errors]); + + $result = $this->object->checkbox('abcde', 1, true); + + + $this->assertTrue(str_contains($result, 'checked="checked"')); + $this->assertTrue(str_contains($result, 'abcde_holder')); + } + + /** + * @covers FireflyIII\Support\ExpandedForm::date + * @covers FireflyIII\Support\ExpandedForm::fillFieldValue + */ + public function testDate() + { + $preFilled = [ + 'abcde' => '1998-01-01' + ]; + $this->session(['preFilled' => $preFilled]); + + $result = $this->object->date('abcde'); + + $this->assertTrue(str_contains($result, '1998')); + $this->assertTrue(str_contains($result, 'type="date"')); + $this->assertTrue(str_contains($result, 'abcde_holder')); + } + + /** + * @covers FireflyIII\Support\ExpandedForm::integer + */ + public function testInteger() + { + $result = $this->object->integer('abcde', 12345); + + $this->assertTrue(str_contains($result, '12345')); + $this->assertTrue(str_contains($result, 'type="number"')); + $this->assertTrue(str_contains($result, 'step="1"')); + $this->assertTrue(str_contains($result, 'abcde_holder')); + } + + /** + * @covers FireflyIII\Support\ExpandedForm::location + */ + public function testLocation() + { + $result = $this->object->location('abcde'); + + $this->assertTrue(str_contains($result, 'id="clearLocation"')); + $this->assertTrue(str_contains($result, 'id="map-canvas"')); + $this->assertTrue(str_contains($result, 'abcde_holder')); + } + + /** + * @covers FireflyIII\Support\ExpandedForm::makeSelectList + */ + public function testMakeSelectList() + { + $collection = new Collection; + for ($i = 0; $i < 5; $i++) { + $collection->push(FactoryMuffin::create('FireflyIII\Models\Account')); + } + $result = $this->object->makeSelectList($collection, true); + + $this->assertCount(6, $result); + } + + /** + * @covers FireflyIII\Support\ExpandedForm::multiRadio + */ + public function testMultiRadio() + { + $list = [ + 'some' => 'BlaBla', + 'other' => 'ThisIsCool' + ]; + + $result = $this->object->multiRadio('abcde', $list); + + $this->assertTrue(str_contains($result, 'ThisIsCool')); + $this->assertTrue(str_contains($result, 'other')); + $this->assertTrue(str_contains($result, 'abcde_holder')); + } + + /** + * @covers FireflyIII\Support\ExpandedForm::optionsList + */ + public function testOptionsList() + { + $result = $this->object->optionsList('update', 'MotorCycle'); + $this->assertTrue(str_contains($result, 'MotorCycle')); + } + + /** + * @covers FireflyIII\Support\ExpandedForm::select + */ + public function testSelect() + { + $list = [ + 'some' => 'BlaBla', + 'other' => 'ThisIsCool' + ]; + + $result = $this->object->select('abcde', $list); + $this->assertTrue(str_contains($result, 'ThisIsCool')); + $this->assertTrue(str_contains($result, 'abcde_holder')); + } + + /** + * @covers FireflyIII\Support\ExpandedForm::tags + */ + public function testTags() + { + $result = $this->object->tags('abcde', 'some,tags'); + $this->assertTrue(str_contains($result, 'data-role="tagsinput"')); + $this->assertTrue(str_contains($result, 'abcde_holder')); + } + + /** + * @covers FireflyIII\Support\ExpandedForm::text + */ + public function testText() + { + $result = $this->object->text('abcde', 'MotorBike!'); + $this->assertTrue(str_contains($result, 'MotorBike!')); + $this->assertTrue(str_contains($result, 'abcde_holder')); + } + + /** + * @covers FireflyIII\Support\ExpandedForm::textarea + */ + public function testTextarea() + { + $result = $this->object->textarea('abcde', 'Pillow fight!'); + $this->assertTrue(str_contains($result, 'Pillow fight!')); + $this->assertTrue(str_contains($result, 'abcde_holder')); + } +} diff --git a/tests/support/NavigationTest.php b/tests/support/NavigationTest.php new file mode 100644 index 0000000000..7e93a09e69 --- /dev/null +++ b/tests/support/NavigationTest.php @@ -0,0 +1,287 @@ +object = new Navigation; + } + + /** + * @covers FireflyIII\Support\Navigation::addPeriod + */ + public function testAddPeriod() + { + $date = new Carbon('2015-01-01'); + + $result = $this->object->addPeriod($date, 'quarter', 0); + $this->assertEquals('2015-04-01', $result->format('Y-m-d')); + } + + /** + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage Cannot do addPeriod for $repeat_freq "something" + * @covers FireflyIII\Support\Navigation::addPeriod + */ + public function testAddPeriodError() + { + $date = new Carbon('2015-01-01'); + + $this->object->addPeriod($date, 'something', 0); + + } + + /** + * @covers FireflyIII\Support\Navigation::endOfPeriod + */ + public function testEndOfPeriod() + { + $date = new Carbon('2015-01-01'); + + $result = $this->object->endOfPeriod($date, '1D'); + $this->assertEquals('2015-01-02', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::endOfPeriod + */ + public function testEndOfPeriodModifier() + { + $date = new Carbon('2015-01-01'); + + $result = $this->object->endOfPeriod($date, 'quarter'); + $this->assertEquals('2015-03-31', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::endOfPeriod + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage Cannot do endOfPeriod for $repeat_freq "something" + */ + public function testEndOfPeriodModifierError() + { + $date = new Carbon('2015-01-01'); + + $this->object->endOfPeriod($date, 'something'); + } + + /** + * @covers FireflyIII\Support\Navigation::endOfX + */ + public function testEndOfX() + { + $date = new Carbon('2015-01-01'); + $maxEnd = new Carbon('2016-01-01'); + + $result = $this->object->endOfX($date, 'month', $maxEnd); + + $this->assertEquals('2015-01-31', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::endOfX + */ + public function testEndOfXBeyondMax() + { + $date = new Carbon('2015-01-01'); + $maxEnd = new Carbon('2015-01-15'); + + $result = $this->object->endOfX($date, 'monthly', $maxEnd); + + $this->assertEquals('2015-01-15', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::periodShow + */ + public function testPeriodShow() + { + $date = new Carbon('2015-01-01'); + $result = $this->object->periodShow($date, 'month'); + $this->assertEquals('January 2015', $result); + } + + /** + * @covers FireflyIII\Support\Navigation::periodShow + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage No date formats for frequency "something" + */ + public function testPeriodShowError() + { + $date = new Carbon('2015-01-01'); + $this->object->periodShow($date, 'something'); + } + + + /** + * @covers FireflyIII\Support\Navigation::startOfPeriod + */ + public function testStartOfPeriod() + { + $date = new Carbon('2015-01-15'); + $result = $this->object->startOfPeriod($date, 'month'); + $this->assertEquals('2015-01-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::startOfPeriod + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage Cannot do startOfPeriod for $repeat_freq "something" + */ + public function testStartOfPeriodError() + { + $date = new Carbon('2015-08-15'); + $this->object->startOfPeriod($date, 'something'); + } + + /** + * @covers FireflyIII\Support\Navigation::startOfPeriod + */ + public function testStartOfPeriodHalfYear() + { + $date = new Carbon('2015-01-15'); + $result = $this->object->startOfPeriod($date, 'half-year'); + $this->assertEquals('2015-01-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::startOfPeriod + */ + public function testStartOfPeriodHalfYearSecondHalf() + { + $date = new Carbon('2015-08-15'); + $result = $this->object->startOfPeriod($date, 'half-year'); + $this->assertEquals('2015-07-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::subtractPeriod + */ + public function testSubtractPeriod() + { + $date = new Carbon('2015-01-01'); + $result = $this->object->subtractPeriod($date, 'month'); + $this->assertEquals('2014-12-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::subtractPeriod + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage Cannot do subtractPeriod for $repeat_freq "something" + */ + public function testSubtractPeriodError() + { + $date = new Carbon('2015-01-01'); + $this->object->subtractPeriod($date, 'something'); + } + + /** + * @covers FireflyIII\Support\Navigation::subtractPeriod + */ + public function testSubtractPeriodQuarter() + { + $date = new Carbon('2015-01-01'); + $result = $this->object->subtractPeriod($date, 'quarter'); + $this->assertEquals('2014-10-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateEndDate + */ + public function testUpdateEndDate() + { + $date = new Carbon('2015-01-15'); + $result = $this->object->updateEndDate('1M', $date); + $this->assertEquals('2015-01-31', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateEndDate + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage updateEndDate cannot handle $range "something" + */ + public function testUpdateEndDateError() + { + $date = new Carbon('2015-01-15'); + $this->object->updateEndDate('something', $date); + } + + /** + * @covers FireflyIII\Support\Navigation::updateEndDate + */ + public function testUpdateEndDateHalf() + { + $date = new Carbon('2015-01-15'); + $result = $this->object->updateEndDate('6M', $date); + $this->assertEquals('2015-07-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateEndDate + */ + public function testUpdateEndDateSecondHalf() + { + $date = new Carbon('2015-08-15'); + $result = $this->object->updateEndDate('6M', $date); + $this->assertEquals('2015-12-31', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateStartDate + */ + public function testUpdateStartDate() + { + $date = new Carbon('2015-01-15'); + $result = $this->object->updateStartDate('1M', $date); + $this->assertEquals('2015-01-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateStartDate + */ + public function testUpdateStartDateHalf() + { + $date = new Carbon('2015-01-15'); + $result = $this->object->updateStartDate('6M', $date); + $this->assertEquals('2015-01-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateStartDate + */ + public function testUpdateStartDateSecondHalf() + { + $date = new Carbon('2015-09-15'); + $result = $this->object->updateStartDate('6M', $date); + $this->assertEquals('2015-07-01', $result->format('Y-m-d')); + } + + /** + * @covers FireflyIII\Support\Navigation::updateStartDate + * @expectedException FireflyIII\Exceptions\FireflyException + * @expectedExceptionMessage updateStartDate cannot handle $range "something" + */ + public function testUpdateStartDateError() + { + $date = new Carbon('2015-09-15'); + $this->object->updateStartDate('something', $date); + } + + +} \ No newline at end of file diff --git a/tests/unit.suite.yml b/tests/unit.suite.yml deleted file mode 100644 index 02dc9b1e0a..0000000000 --- a/tests/unit.suite.yml +++ /dev/null @@ -1,9 +0,0 @@ -# Codeception Test Suite Configuration -# -# Suite for unit (internal) tests. - -class_name: UnitTester -modules: - enabled: - - Asserts - - \Helper\Unit \ No newline at end of file diff --git a/tests/unit/_bootstrap.php b/tests/unit/_bootstrap.php deleted file mode 100644 index 8a88555806..0000000000 --- a/tests/unit/_bootstrap.php +++ /dev/null @@ -1,2 +0,0 @@ -